Many games require you to collect all the objects in order to move to the next level. You can not continue until you do. For these types of games, you have to keep track of each object and if it has been picked up by the user. Then when all the objects have been picked up, you need to let the user move on. You do this either by keeping a number count or by keeping track of each object. Let's assume you have a game where the user has to get 3 apples to continue on with the game. We will look at the code to do each way. Collecting 3 Apples: 1. Keeping Track of Each Apple In this way, we need to create a variable for each apple to remember if it was picked up or not. Then when any apple is picked up, we check if all of them have been picked up and let the user move on. If not, the game continues...
put false into got1 put false into got2 put false into got3 end openCard When the user runs into an apple, we have to set that variable to true. We also want to hide it and disable it (so the user can not pick it up twice).
put true into got1 hide button "apple1" disable button "apple1" end if
put true into got1 hide button "apple1" disable button "apple1" if got1 and got2 and got3 then go to card "level2" end if end if
put false into got1 put false into got2 put false into got3 show button "apple1" enable button "apple1" ...same for apples 2 and 3 2. Keeping Count of the Number of Apples Picked In this one, we may have many apples on the screen and the user only needs 3 to move on. So we just need to keep count, not have to track each and every apple on the screen We need a variable to keep track of how many apples the user picked up. We will decaler that at the beginning and set it to zero (This will be something like a score-keeper. There is another lesson on the home page how to keep score. This one is a simpler case)
put 0 into numApples put 0 into the field "numApples" --if you want to show the user in a field on the screen end openCard When the user runs into an apple, we have to add one to the numApples. We also want to hide it and disable it (so the user can not keep going over it to rack up more points).
if enabled of button "apple1" then add 1 to numApples add 1 to the field "numApples" -- to show the user on the screen end if hide button "apple1" disable button "apple1" end if
hide button "apple1" disable button "apple1" if numApples = 3 then go to card "level2" end if end if
put 0 into numApples put 0 into the field "numApples" --if you want to show the user in a field on the screen show button "apple1" enable button "apple1" ...same for the rest of the apples on the page Instead of apples, you can have prizes, diamonds, bonuses, bombs (subtract points) or whatever you like |
Home >