This add-on is slightly
more challenging than some of the others.
After you finish programming this add-on
the user will be able to control
the movements of the main character.
Also, the main character will only notice
the secret door once it touches it.
First, program the character to move with the arrow keys.
Drag out a move 10 steps block and click on it.
The character will probably move right.
Place a when key pressed event
above the move 10 steps block and click
the drop-down and select right arrow.
Test this, press the right arrow key.
The character should move right, great.
Now create similar code to make the character move left.
To do this just right-click on the code
to move right and then click duplicate.
Change the drop-down to say When Left Arrow Clicked.
Negative numbers in Scratch do the opposite
of positive numbers.
If move 10 steps moves the sprite right,
move negative 10 steps moves it left.
Add a negative sign in front of the 10
in move 10 steps to code for the left arrow key.
Test this code.
Pressing the left arrow should move the sprite left,
and now pressing the right arrow
should move the sprite right.
This is a basic way to make characters move.
If you'd like more information
about how to make it look like sprites
are actually walking, watch the add-on Walking.
Instead of putting the walking code
inside an if-else statement, just put it
underneath the appropriate events.
Next, make sure the sprite starts
in the same spot each time the story is told
regardless of where it gets moved during the story.
To do this, move the character to where
you want it to start in the story.
Then, place a go to XY block just underneath
the when green flag clicked block.
Now, the main character can walk
but it still immediately mentions the secret door
rather than waiting until it is touching
the door to notice it.
To make your program wait for a condition,
use the wait until block.
Now fill out the condition.
The sprite can see the door when it's touching it.
The door is part of the background.
However, it is the only part of the background that's black.
The character can tell it's touching the door
if it's touching the color black.
So drag out the touching color condition
and put it into the wait until block.
Next, make sure the color in the condition
matches the color of the door.
To do this, you need to click on the color
then click on the door.
Now, the block should read
"Wait until touching color black".
Finally, figure out where to place the wait until block.
Watch what happens when the wait until block
is placed immediately after the
when green flag is clicked block.
The character doesn't reset its position
and the scene doesn't reset to the brick wall.
That happened because when the green flag is clicked,
the program waits until the character
is touching black before doing anything else.
So, instead try moving the wait block after all the setup.
Placing it before should I go through the secret door.
Test the code.
The character begins in its starting position
and the background starts as the brick wall.
Move the character around.
As soon as it touches the black door
it asks if it should go through it, great.
Next, tell the user how to make your program work
using the right and left arrow keys.
To do this, add a say block after the go to block.
In this example, the character goes to its starting
position then says, "Use the arrow keys to make me move!"
Then, once it touches the black door,
it asks the user if it should go through the door.
Computer scientists rarely program what
they want correctly the first time.
They have to be persistent and keep trying
until they solve the problem.
When you keep working at a problem
to figure out how to fix it rather than giving up
you're acting like a computer scientist.
Now it's your turn.
Use the move and when key pressed event blocks
to make the character move when the arrow keys are pressed.
Use the wait until block to make the character wait
until it touches the door before asking
if it should go through it.
Make the condition and the wait until block
touching color black.
Tell the user to press the arrow keys
to move your sprite by adding
a say block after the go to block.