sprite.setVelocity()

Category:Sprites

Sets the sprite velocity in both the x and y direction.

A positive x parameter will move the sprite to the right, a positive y parameter will move the sprite downward. Must be used with the draw() function containing a drawSprites(). You can also update the velocity of a sprite using setSpeedAndDirection() or by updating the sprite properties velocityX and velocityY.

The default velocity is 0 in both x and y directions.

Examples

var sprite = createSprite(200, 200);
sprite.setVelocity(2,2);
function draw() {
  background("white");
  drawSprites();
}

Syntax

sprite.setVelocity(x, y)

Parameters

NameTypeRequired?Description
x Number The velocity of the sprite in the positive x direction, to the right.
y Number The velocity of the sprite in the positive y direction, downward.

Returns

No return value. Changes output in the display after drawSprites() is called.

Tips

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