sprite.velocityX
Velocity in the positive x direction of the sprite in pixels per frame.
A positive value will move the sprite to the right, a negative value will move to the left. Must be used with the draw() {}
function containing a drawSprites()
.
The default velocityX is 0. All sprite properties can be both accessed and updated.
Examples
Vibration
Make a sprite vibrate by alternating velocityX positive and negative.
// Make a sprite vibrate by alternating velocityX positive and negative.
var sprite = createSprite(200, 200);
sprite.velocityX = 5;
sprite.velocityY = 1;
function draw() {
background("white");
drawSprites();
if (sprite.velocityX>0) {
sprite.velocityX = -5;
}
else {
sprite.velocityX = 5;
}
}
var sprite = createSprite(200, 200);
sprite.velocityX = 1;
function draw() {
background("white");
drawSprites();
}
Syntax
sprite.velocityX
Returns
Tips
- velocityX can be used similarly to using the "counter pattern" on
sprite.x
within thedraw() {}
function. - Changing
World.frameRate
will affect the velocityX. - 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 support@code.org.