sprite.velocityY

Category:Sprites

Velocity in the positive y direction of the sprite in pixels per frame.

A positive value will move the sprite downward, a negative value will move upward. Must be used with the draw() function containing a drawSprites().

The default velocityY is 0. All sprite properties can be both accessed and updated.

Examples

Zig-Zag

Move a sprite in a zig-zag pattern by alternating velocityY positive and negative.

// Move a sprite in a zig-zag pattern by alternating velocityY positive and negative.
var sprite = createSprite(200, 200);
sprite.velocityX = 1;
sprite.velocityY = 1;
function draw() {
  background("white");
  drawSprites();
  if (sprite.y>225) {
    sprite.velocityY = -1;
  }
  if (sprite.y<175) {  
    sprite.velocityY = 1;
  }
}

var sprite = createSprite(200, 200);
sprite.velocityY = -1;
function draw() {
  background("white");
  drawSprites();
}

Syntax

sprite.velocityY

Returns

Velocity in the positive y direction, downward.

Tips

  • velocityY can be used similarly to using the "counter pattern" on sprite.y within the draw() function.
  • Changing World.frameRate will affect the velocityY.
  • Sprites all have the same properties and you use the dot notation (combining the name of the sprite, followed by a dot, with the label of the property) to both access and update the property for that sprite.
  • Any changes to the properties of a sprite will not be seen until after drawSprites() is called.

Found a bug in the documentation? Let us know at documentation@code.org