Post by Jonathan Cauldwell on Apr 28, 2012 12:43:10 GMT
Suppose you want to the player to progress to the next level once he has collected all the collectable objects on a screen, rather like Manic Miner. This is simple to do, using a variable to count the objects on to the screen and off again as they are collected.
Let's say we use variable I for Items collected. We initialise that variable when we start a new screen, so in the Restart screen event we put in the following line of code:
LET I = 0
So we have zero in the variable I before the screen is drawn.
Next, we set up a sprite type specifically for collectable objects. Say sprite type 2, for the sake of argument. When this sprite type is drawn on the screen we need to increment the value of I. So we need to put this code into the Initialise sprite event:
IF TYPE = 2
ADD 1 TO I
ENDIF
Now we need to decrement the value of I when an item is collected. In the event for sprite type 2 we need code which looks like this:
IF COLLISION 0
REMOVE
SUBTRACT 1 FROM I
IF I = 0
NEXTLEVEL
ENDIF
ENDIF
If this collectable sprite is in collision with sprite type 0 (the player), we subtract 1 from the number of items on the screen. When we reach zero, we proceed to the next level because there are no more collectable sprites on the screen. That is basically all there is to it.
Of course, we don't have to go straight to the next level, we could require that the player makes his way to an exit first. That would involve removing the check for I = 0 from the Sprite type 2 event, and placing it elsewhere.
You could set up an exit constructed from CUSTOM blocks and position those on each screen. Sprite type 0 would then need code like this:
IF CUSTOM
IF I = 0
NEXTLEVEL
ENDIF
ENDIF
Alternatively, you might want to use an animated sprite as an exit, and position one on every screen. Let's say we use sprite type 3. This code will leave the sprite dormant until all the items have been collected, whereupon it will become animated and check for a collision with the player sprite. If the player touches the animated sprite, he proceeds to the next level:
IF I = 0
ANIMATE
IF COLLISION 0
NEXTLEVEL
ENDIF
ENDIF
Quite simple really.