aka "so, you want to write a game, do you?"
© 2008 PeeJay
© 2008 PeeJay
Warning: Undefined array key "keywords_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 234 Warning: Undefined array key "description_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 235 Warning: Undefined array key "commercials" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 261
FOREACH en IN enemies[] IF en.status=0 FOREACH bull IN bullets[] IF ANIMCOLL(1,en.status,en.enx,en.eny,2,0,bull.bulx,bull.buly) en.status=1 DELETE bull ENDIF NEXT ENDIF NEXTInitially, we are examining each enemy in turn, so, to start with, we look at the first enemy. Remember that status flag we haven't used - we're going to use it now! If the status is 0, the rock hasn't been shot - if it is more than 0, it has been shot, and we will increment the number to show how many cycles ago it happened, so we can display an animated "death" routine - more on this later. So, the next section only applies if the status is 0, that is, the rock hasn't already been shot. Now we look at the first bullet (but we will check every bullet). Then the fabulous line:
IF ANIMCOLL(1,en.status,en.enx,en.eny,2,0,bull.bulx,bull.buly)This looks at 2 imagestrips, namely the imagestrip held in enemy, at co-ordinates enx,eny and image 0 in the strip (or the first picture, and compares it to bullet, at co-ords bulx,buly and image 0. Ah, but wait - the image held in bullet isn't an animated image - it isn't an imagestrip, it is just one image! That is correct - for single images you still have to put an image number if using ANIMCOLL, so use 0. The ANIMCOLL checks to see whether the images collide, and ignores the transparent pixels when doing the check, so it makes for pixel perfect checking. So, what happens if the images do overlap, that is to say, the bullet has hit the rock? Well, we are going to set the rock status to 1, to say it has been hit, and we are going to delete that bullet - after all, we don't want one bullet to be able to hit multiple rocks, now do we? Then, we check the Next bullet and repeat, until all the bullets have been checked. Then we move on to the next enemy, and check every bullet against that, and continue until every bullet has been checked against every enemy. Now you would be forgiven for thinking this would take a long time to do if there are a lot of graphics on screen. Well, you'd be wrong, due to the incredible speed of GL Basic, this check is done extremely quickly - so quick, in fact, that your machine won't even miss a frame! Have another look in the DrawTheScreen function, where we draw the enemies. Here you will see that we set things up so the image that is drawn is that held in status - so next time it is called, the hit enemies will have status as 1, so the second image of the strip will be drawn instead of the first. Now take a look in the UpdateEnemy function. You will notice that after we move the enemies, we now check each enemy again for the status, to see whether it has been hit - whether the status is more than 0. If it is, we add one to the status, so the first time this routine is called from an enemy being hit, it will move the status on to 2. It also checks to see whether the status is more than 3 - if it is, it will delete that enemy. Now take another look at the enemy imagestrip. You will notice that the second, third and fourth images have the enemy disintegrating. That is why we update that status flag - so when it draws the enemy according to the status, the rock is seen to disintegrate. There is something else to take note of - the order things are being done:
The status is updated Check the rocks for being hit Draw the rocks loop backWhy is this important? Well, if we checked the rocks being hit, set the status flag to 1, and then updated the status, it would add 1 and make the status 2 before any image had been drawn, so during the drawing process the status could never equal 1, and as such we would miss out a frame of the disintegration. Hopefully that makes sense. We can see from this, it is just as important to do things in the correct order as it is to program the steps in the first place - you don't switch the kettle on before putting water in it, now do you?