Scratch/Lessons/Movement

Smooth MovementEdit

Smooth movement in Scratch Projects is difficult to achieve when you use a "when (key) key pressed" from the control tab. To make smooth movement you need to use "forever" loops, from control, and "<key (key) pressed?>", from sensing. For example, this script:

Scratch Screenshot, Smooth Move Script.png

works far more efficiently than this script:

Screen shot 2010-01-01 at 4.11.55 PM.png

In all of the tutorials below you will use movement techniques based around this idea.

Side View PlatformersEdit

In side view platformers it is typical to use "velocity" to control the movement of the sprite. This is your guide to advanced programming of movement with the Velocity variable. "Velocity" is just the name of a variable, just because it is named velocity doesn't make a difference. However, the concept of velocity adds a complex and advanced motion system to your project.

Y VelocityEdit

Using yVelocity will help you create a semi-realistic and dynamic gravity system. To start this session of testing arrange the stage like this:

Scratch Screenshot, Stage.gif

Then in the Cat sprite make this script:

Scratch Screenshot, Gravity Script.png

Text of the comment block (if you can't read it): This is a basic jumping script. We'll start by breaking it down. The first if statement with the set yVelocity to 0 block in it is there so the cat will not fall through the ground. The next if with the set yVelocity to 15 block in it will function if the cat is on the ground and the up arrow is pressed and will make the cat jump. Out of that set of ifs the change y by yVelocity will make him fall or rise depending on the velocity variable. And the change yVelocity by -1 is gravity.

Remember that you can change the 15 in the "set [yVelocity] to [15]" in the script to make the cat jump higher.

That covers Y Velocity. Now it is time for X Velocity.

X VelocityEdit

"xVelocity" will help you make movement along the X Axis seem more realistic. Start by arranging your stage like this:

Scratch Screenshot, Stage2.gif

Then in the Cat sprite make the following script:

Scratch Screenshot, Running Script.png

Text of the comment block (if you couldn't read it): This is the basic script needed to move a sprite along the X Axis with velocity. maxSpeed can be any number you want depending on how fast you want your sprite to go. The first two if statements will change the xVelocity variable by certain numbers. These numbers can change if you want the sprite to move faster. The next if-else will make the sprite point in the direction he is moving. The block: change x by xVelocity will make him move depeding on how fast he is going. The last block is friction and will make the sprite eventually slow to a halt

Now if you combine these two scripts you have perfect jumping and running. I hope this helped answer your questions. If you need any further information post your question and I'll reply as soon as possible.

Last modified on 11 June 2013, at 13:48