In this add on, you'll make the object appear
in a random place, each time the user plays the game.
This makes the game less predictable and more fun.
In the core project, the object shows
when the game switches to a specific backdrop.
Since the object should in a random backdrop,
select the object, detach the when backdrop
switches to block and delete it.
Click the state then the Backdrops tab.
There are four different backdrops.
One for the town and one for each of the three locations.
Each of these backdrops has a number.
Open the Looks menu.
Find the backdrop number and click it to see what it does.
This block outputs the current backdrop's number.
Add this block to the object sprite
by dragging it on top.
Then, click the object sprite.
Cool!
The backdrop number block appears in the scripts area.
Click Make a Variable.
A variable holds a value.
In this case, this variable will hold
a random backdrop number where the object will appear.
Name the variable object location, then click OK.
Set this variable to a random number.
Drag up a set object location block
then a place a pick random block from Operators into it.
Type two and four in the values places
because backdrops two, three, and four
represent the three possible locations
where the object can be found.
Test this block by clicking on it a few times.
The value next to object location on the state
changes to a random number between two and four each time.
Great!
Place this block above the forever block
so the program decides where the object will hide
at the start of the game.
Next, build a conditional statement that checks
if the current backdrop equals the object location.
Drag out an if block.
Place an equals block inside the condition slot.
Drag the object location block into one slide
and the backdrop number block into the other.
If the statement is true, make the object show
then detect if the player is touching it.
Put this conditional
around the touching character conditional
and move the show block into the if statement.
Add a when flag clicked block on top
so this code runs when the flag is clicked.
Finally, uncheck the box next to object location.
This hides the variable display on the stage
so the player can't see the answer.
This code now reads, when the flag is clicked
set the object location to a random number
between two and four.
If the current backdrop number is equal
to that random number, meaning the character
is at that location then the object shows.
Test it out by playing the game more than once.
The object now appears at a random place each time.
Now, it's your turn.
Select the object sprite, and discard
the when backdrop switches to block
for the touching character conditional.
Drag the backdrop number block from the stage
into the object sprite.
Create an object location variable,
and set it to a random number at the start of the program.
Build a conditional that checks if the backdrop number
equals the object location variable.
If the condition is true, show the object sprite,
and turn on the winning condition.