Game Development Reference
In-Depth Information
We will go on to create a script to move the cannonball:
1.
Click on the cannonball sprite to open its script editor.
2.
The cannonball will also be triggered with a
when <green lag> clicked
block.
Its acions will also be contained inside a
forever
loop.
3.
Place a
move () steps
block inside the
forever
block. That's the basic necessity
to set the sprite in moion.
Now we build a few more controlling scripts, so the cannonball actually goes where we
want it to go:
1. Place a
go to ()
block on top of the
move () steps
command and select
cannon
.
This makes the cannonball hop back to its staring posiion.
2. Make the cannonball copy the cannon direcion with a
point in direcion ()
block
and by inseing the
direcion
variable. Note that this is our added variable (orange
background) from the
Data
category, not the built-in (blue background) variable
from the
Moion
category. The variable with the orange background is the saved
cannon posiion. In this case, the variable with the blue background is the current
direcion of the cannonball, which wouldn't change anything when applied to itself.
To make the cannonball move forward, instead of constantly reseing, we use another kind
of loop with a condiion:
1.
Put a
repeat unil ()
block around the
move
command.
2.
Then, place a
touching ()
condiion block in the vacant space and select
edge
.
Now the cannonball will angle itself in the same direcion as the cannon, and it will keep
moving forward unil it reaches the edge of the stage. At that point, the script repeats and the
cannonball is reset to its staring posiion in the cannon. The following is the completed script: