sprite.setVelocity()
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 sprite.velocityX
and sprite.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
Name | Type | Required? | 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
Tips
- Changing
World.frameRate
will affect the velocity.
Found a bug in the documentation? Let us know at support@code.org.