We are going to use a global variable to count the number of tiles which are in the correct position. The global variable will start at 0, and we will add 1 each time a puzzle tile is returned to the correct position.
We can then tell the system that when the global variable = the number of puzzle pieces the game is over.
1/ Make a Global Variable
Right click in the event page and select ‘Add Global Variable’.
As with the instance variable, give this variable a name and we suggest starting it with g_ so it is easily recognizable (Note: Although the name in the image below is g_angle, we later changed it to g_angle0 which you will see in future screenshots).
We want the variable to start at 0, so click OK to close the window.
2/ Count When The Tiles Are at O
We want to add 1 to the global variable each time a tile is returned to 0.
Therefore, we need to create a sub event which says that when a tile has an instance variable iv_angle of 0, add 1 to the global variable.
Introducing Sub Events
A sub event is an event which happens within another event. We want it to happen only after another event has started. In this situation, we want the system to add to the global variable only after a tile has been clicked. Therefore we need to make our new event a sub event.
Create a new sub event, by right clicking under the main Tile Touch event and select ‘Add Sub Event’.
And add a Tile condition which compares the instance variable to 0 as we did in the previous lesson.
Now create an Action with the System object. Select ‘system’
and then look for ‘Add To’ under the global variable options.
Now we need to tell the system to add 1 to the global variable every time this condition is met.
The finished sub event should look like this:
3/ Restart Game
We want something to happen when all of the puzzle pieces are in the correct position.
We will add some bells and whistles later, so for now, we’ll simply wait for one second and then restart the game.
To do this, we need to check for when the global variable which is recording the number of tiles which are at angle 0 matches the total number of tiles. When the total number of tiles which are at angle 0 are the same as the total number of tiles, we know the game is complete.
The first thing we are going to do is create a new global variable called g_tiles. And at the Start of Laytout we want it to set the number of tiles in your game. In this instance there are 12:
Next, we want to create an event which will compare the number of tiles in the correct position with the number of tiles available.
Set up a new event: This time, select the System object and look for ‘Compare Two Values’.
Now we want to set the parameters so the system triggers the action when the global variable = the number of tiles
And now we need to set the action which says what we want to happen when all of the tiles are in the correct position.
Add a new action and we’ll ask the system to pause for one second. Therefore select the System option and look for ‘Wait’ under the time section.
As we want the system to wait for one second, we can leave the setting as it is.
Now we want to tell the system to restart, using the same command we used when we added the restart button. Select New Action > System > Restart Layout.
4/ One Last Thing – Reset the Global Variable
There’s one last thing to do in this section and that’s to add a new action to the On Start of Layout event which sets the Global Variable back to 0 at the start of each game.
The global variable won’t get reset unless we tell it to and we need it to always start each game at 0 in order for it to count correctly.
Therefore, add a new action to the Start of Layout event. Choose the System object and then under Global Variables, look for Set Value. Set the value to 0 and drag this to the top of the action list.
Save and test the puzzle game