3 Movement & Scoring
We need to make the moles stick their heads up at random intervals, wait a wee while before hiding away again. The player will get a point if they click on the mole when it is visible above the barrel.
Start with a Create Event in the mole object window. Drag in a Set Alarm action and fill in the pop-up window as follows,
The expression here is designed to choose a random number between 0 and 150. The floor() function will round this value down to the nearest number. This alarm will be the start of the mole movement process.
Also in the Create Event, we set a variable to control when the mole is clickable. We will start off with this variable set to 0. When it is set to 1, the mole can be clicked. Drag in a Set Variable action to do this, filling in the pop-up as shown.
Add an Alarm Event for Alarm0. Drag in a Move Fixed icon and fill it in to make the mole move up the screen at a speed of 3. Follow this with another Set Alarm action, filling in this pop-up as follows,
So far, the movement works by setting an alarm for a random interval. When this alarm fires, it starts the mole moving upwards and sets another alarm (Alarm1) to fire in 30 steps.
This event stops the mole moving upwards, makes it possible for the player to click on it and then sets another alarm (Alarm2) to start it moving back down after a short pause.
- First drag in a Set Vertical Speed action and set the speed to 0.
- Then add a Set Variable action and set the value of the variableclickable to 1. The player can now click on the mole.
- Finally, drag in a Set Alarm action and set Alarm2 to fire in 20 steps.
This event starts the mole moving back down to the bottom of the barrel and sets the value of the variable, clickable back to 0 so that the mole cannot be clicked.
- Start with a Move Fixed action, set the mole to move down the screen with a speed of 3.
- Add a Set Variable action and set the value of the variable clickable to 0.
- Now drag in a Set Alarm action and set Alarm3 to fire in 30 steps.
This is the last of our 4 alarms and will fire when the mole has returned to its starting position. We will need to stop the mole from moving and set Alarm0 to fire after a random interval.
- Start by dragging in a Set Vertical Speed action and fill it in to set the speed to 0.
- Follow this with a Set Alarm action, exactly like the one we started with (shown below).
Left Button Pressed Event
This event increases the score when the player clicks on a mole which is poking its head out. We need to check the value of the variable clickable so that we know that the mole is in the correct position to be whacked. If so, we increase the score and set the variable back to 0 so that the player can't double whack the mole. You can do this with the following actions,
Save your work and test the basic game.