Velocity (not to be confused with gravity) is defined as speed in a given direction, and can therefore be negative, unlike speed alone (which is directionless and always positive). It is often used in projects for physics effects. Using velocity is a far smoother and aesthetically pleasing method than traditional scrolling.

How to program velocity in Scratch

A common method used to program velocity is by maintaining its value as a variable and using that to change the sprite's position:

if <key [left arrow v] pressed?> then
change [Velocity v] by (-1) //reduce velocity for accelerating towards the left
if <key [right arrow v] pressed?> then
change [Velocity v] by (1) //increase velocity for accelerating towards the right
set [Velocity v] to ((Velocity) * (0.9)) //gradually lose speed (regardless of direction)
change x by (Velocity) //change the sprite's position based on the updated velocity

The above technique is simple, efficient and contained within a single script. The numbers can be changed to affect how quickly the sprite can change its velocity. It should be noted that the number in the last set variable block must be between 0 and 1, or the sprite will speed up rather than slowing down gradually.

Example Uses

Velocity has many uses — it can be used almost anywhere. A few of the many uses are below:

  • Racing and driving games — Vehicles with velocity are more realistic to drive with.
  • Platformers — Velocity makes the movement more realistic.
  • Physics simulations — For example, a bouncing ball can use this.
  • Scrolling projects — Scrollers with velocity are popular[citation needed].

In general, anywhere that a sprite is moving — simple on-off movement — can easily be enhanced with velocity.

Example Projects

Below are some projects that use velocity:

See Also

External Links

  • This page was last modified on 23 July 2016, at 15:34.