![]() |
This page or section is in question as to whether it is useful or not. You can discuss whether you think it should be deleted or not on its talk page. (February 2021) Reason: Content is in other pages |
Controlling a sprite is one of the most basic and important things in platformers and other interactive projects. Sprite controls can be seen in just about any game in Scratch. Sprite scripts can be simple, such as the examples shown, or complex.
Basic Scripts for Controlling Sprites
Here is a basic script that can move a sprite in any of four directions:
when gf clicked forever if <key (right arrow v) pressed?> then change x by (10) end if <key (left arrow v) pressed?> then change y by (-10) end if <key (up arrow v) pressed?> then change y by (10) end if <key (down arrow v) pressed?> then change y by (-10) end
Here is a variation that causes a sprite to face the direction that it is moving:
when gf clicked forever if <key (right arrow v) pressed?> then point in direction (90) change x by (10) end if <key (left arrow v) pressed?> then point in direction (-90) change y by (-10) end if <key (up arrow v) pressed?> then point in direction (0) change y by (10) end if <key (down arrow v) pressed?> then point in direction (-180) change y by (-10) end
Here is another which is mainly used in simple platformers:
when green flag clicked forever if <key (up arrow v) pressed?> then repeat (10) // makes sprite jump change y by (20) end repeat (10) // moves sprite back down change y by (-20) end end if <key (right arrow v) pressed?> then change x by (10) end if <key (left arrow v) pressed?> then change y by (-10) end end
Controlling Sprites in Other Games
There are many more unique ways to make controls for different sprites for different kinds of games. Some games such as Platformers only move sideways and usually allows the sprite jump over obstacles. Others such as Tycoons allow sprites to make income for a player. Sometimes, sprites can play multiple roles in a project, such as combining RPGs and Platformers into one.