Document.png Please expand this article. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (August 2021)
A sprite blurring with movement.

Movement is the action of changing an object's position. It is used to show action. Scratch provides various blocks that can be used to create movement, including the Change X by (), Change Y by (), and Move () Steps blocks; most of these blocks are located in the Motion category.

Following the Mouse

This script can be used to make a sprite follow the mouse-pointer.

when gf clicked // starts the script
forever // makes it so the movement will keep going
point towards (mouse-pointer v) // aims for the mouse
move (10) steps // moves

You can also use the following script to make a sprite follow the mouse-pointer but stay in a 90-degree direction.

when gf clicked//starts the script
forever//this makes sure that the sprite keeps moving
go to (mouse-pointer v) // moves sprite to the mouse-pointer

Arrow Keys Movement

This script gives more control over the movement, and it uses the arrow keys rather than the mouse. However, this script only allows for movement horizontally and vertically.

when green flag clicked // Starts the script.
forever // lets the script repeat
if <key (up arrow v) pressed?> then // if key is pressed
repeat until <not <key (up arrow v) pressed?>> // repeats until key is not pressed
change y by (10) // moves in wanted direction
end
end
if <key (down arrow v) pressed?> then
repeat until <not <key (down arrow v) pressed?>>
change y by (-10)
end
end
if <key (right arrow v) pressed?> then
repeat until <not <key (right arrow v) pressed?>>
change x by (10)
end
end
if <key (left arrow v) pressed?> then
repeat until <not <key (left arrow v) pressed?>>
change x by (-10)

This script is similar to the previous one, but it allows diagonal movement as well. It works since the forever loop detects all keys and is not looping each individual key press.

when green flag clicked // starts the script
forever // lets the script repeat
if <key (up arrow v) pressed?> then // if key is pressed
change y by (10) // moves in wanted direction
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
end

Alternatively, the When key pressed block can also be used:

when [up arrow v] key pressed // if key is pressed
change y by (10) // move in wanted direction
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)

However, this has the limitation of requiring the player to hold the key for some time before moving continuously.

Velocity Movement

This script creates a smoother form of movement. It works by gradually increasing or decreasing the amount that the sprite moves each loop through the use of a variable.

when gf clicked // starts the script
forever // loops the movement script
if <key (right arrow v) pressed> then // detects an input
change [x velocity v] by (1) // changes the velocity variable
end
if <key (left arrow v) pressed> then
change [x velocity v] by (-1)
end
set [x velocity v] to ((x velocity) * (0.9)) // slowly decreases velocity
change x by (x velocity) // makes the sprite move based on the velocity variable

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.