This tutorial explains how one can make a game or interactive animation that moves sprites with the arrow keys.

X-Y Method

This is the easiest way to do this. First, go to the sprite you want to make move. Then, add these scripts:

when [up arrow v] key pressed
change y by (10)
when [down arrow v] key pressed
change y by (-10)
when [right arrow v] key pressed
change x by (10)
when [left arrow v] key pressed
change x by (-10)

The sprite will move when the arrow keys are pressed however it will point the same direction.

Steps Method

Using these scripts, the sprite will turn around while it moves. This is not recommended in a sprite that must turn for other reasons.

when [up arrow v] key pressed
point in direction (0)
move (10) steps
when [down arrow v] key pressed
point in direction (180)
move (10) steps
when [right arrow v] key pressed
point in direction (90)
move (10) steps
when [left arrow v] key pressed
point in direction (-90)
move (10) steps

Now your sprite will turn when it moves.

Loop Method

Both methods above can also be written as such below (note in this example the X-Y method is used but the steps method will work with this code too if changing direction is desired):

when gf clicked
forever
if <key (up arrow v) pressed?> then
change y by (10)
end
if <key (down arrow v) pressed?> then
change y by (-10)
end
if <key (right arrow v) pressed?> then
change x by (10)
end
if <key (left arrow v) pressed?> then
change x by (-10)
end

Velocity Method (Advanced)

This is a more advanced but smoother movement method. Two variables called 'x velocity' and 'y velocity' (shortened to xv and yv for this tutorial) will need to be created.

when green flag clicked
set (xv) to (0)
set (yv) to (0)
forever
if <key (up arrow v) pressed?> then
change (yv) by (1)
end
if <key (down arrow v) pressed?> then
change (yv) by (-1)
end
if <key (left arrow v) pressed?> then
change (xv) by (-1)
end
if <key (right arrow v) pressed?> then
change (xv) by (1)
end
set (xv) to ((xv) * (0.9))
set (yv) to ((yv) * (0.9))
change x by (xv)
change y by (yv)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.