In this video you'll use the arrow keys
to make the hero move through the world.
Right now the hero's walking,
but it's just walking in place.
Nothing else is moving.
If you've ever played Mario, Sonic the Hedgehog,
or another side scrolling game,
you know that when you walk forward
the character on screen doesn't actually move forward.
Instead the background moves backward.
You'll use this trick with your game today.
If you click on the ground sprite
you'll see a good bit of code is already in there.
This code uses the block you see in the sprite preview
to create the ground all the way across the stage.
It even places random gaps in the blocks
that the hero has to jump over.
The ground sprite contains three event blocks.
When flag clicked, when I receive forward,
and when I start as a clone.
Click the when I start as a clone stack.
You'll see a little block peek out,
when it doesn't go anywhere.
Click the when I receive forward block stack.
The block moves a little.
The forward message is the key
to making the background move.
Broadcast this forward message
each time the hero moves forward.
In the hero sprite, you'll build out to your motion loop.
The motion loop will control all of the hero's movements;
Jumping up, going forward, and falling down.
Drag out another forever block.
Add a comment to this one, also,
by right-clicking add comment.
Type motion loop.
Inside this loop, you'll check whether the
right arrow key is pressed.
Then each time it is, tell the ground to move backwards.
This will make it look like the hero is moving forwards,
which is the trick to making side scrolling games work.
Dd an if block to the loop.
Then add a key pressed sensing block as the condition.
Pick right arrow from the drop down.
Finally, add a broadcast block to the if block.
Make sure forward is selecting and test it out.
Click the block stack, then press the right arrow key.
Did the ground start moving?
Your character now walks forward.
One final step.
Give the hero a little runway to start the game.
Drag out a repeat loop,
and snap a broadcast forward block inside it.
If this stack runs after the flag is clicked,
it'll move the ground forward.
Now, snap the stack just above the motion loop and
cap it off with a when flag clicked.
So, the hero is sure there's some ground to land on.
Make sure to test and tinker with the value
in the repeat loop to control
how much the ground moves forward at the start.
In the next video, you'll add gravity and jumping.
Now it's your turn.
Place an if block inside a forever loop to check
if the arrow key is pressed.
If it is, broadcast the forward message.
Sign in to save your project and track your progress.