Home‎ > ‎

Collecting Objects

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...

  • We assume that there already are 3 apples (button "apple1", button "apple2" and button "apple3" on the card.
  • We need a local variable for each apple that the user gets. At the top of the card script add:
                local got1, got2, got3

  • We need to set them false when we open the card. If you already have an openCard handler, just add the code to it. If not, create the openCard handler that is called when the card is first opened. Add this to the card script:
            on openCard
                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).
  • In the code where you move your player, add this:
                if intersect (button "box" , button "apple1" , "pixels") then
                        put true into got1
                        hide button "apple1"
                        disable button "apple1"
                end if


  • Add a check in the intersect command to check if the user now has all of them
                if intersect (button "box" , button "apple1" , "pixels") then
                        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


  • Do the same for the other 2 apples
Note:  If you allow the user to replay the level and have a "Start" or a "Reset" button, you need to add the following code to reset everything

                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)

  • Add a variable for keeping track of the apples at the top of the card script:
            local numApples

  • If you want to show the user the number of apples so far, add a field "numApples" to the card
  • We need to set it to zero when we open the card. (If you already have an openCard handler, just add the code to it. If not, create the openCard handler that is called when the card is first opened.) Add this to the card script:
            on openCard
                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).
  • In the code where you move your player, add this:
                if intersect (button "box" , button "apple1" , "pixels") then
                        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


  • Add a check in the intersect command to check if the user now has all of them
                if intersect (button "box" , button "apple1" , "pixels") then
                        hide button "apple1"
                        disable button "apple1"

    
                    if numApples = 3 then
                                go to card "level2"
                        end if
                end if


  • Do the same for the other apples
Note:  If you allow the user to replay the level and have a "Start" or a "Reset" button, you need to add the following code to reset everything

                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 

Comments