Set the speed and direction of the sprite.
The velocity of the sprite, its speed and direction, can be set in many ways:
sprite.x
and sprite.y
properties within the draw()
functionsprite.velocityX
and sprite.velocityY
propertiessprite.setVelocity()
or sprite.setSpeedAndDirection()
Positive speeds move the sprite in the direction of the angle, negative speeds move the sprite in a direction opposite the angle. The default direction angle is 0, to the right, and clockwise increasing. Usually a number from -360 and 360.
var sprite = createSprite(200, 200); sprite.setSpeedAndDirection(2,45); function draw() { background("white"); drawSprites(); }
Move a sprite in a circle.
// Move a sprite in a circle. var sprite = createSprite(200, 100); function draw() { background("white"); drawSprites(); sprite.setSpeedAndDirection(2,sprite.getDirection()+1); }
sprite.setSpeedAndDirection(speed, angle)
Name | Type | Required? | Description |
---|---|---|---|
speed | Number | The rate of change in movement of the sprite per framerate. | |
angle | Number | The direction angle of the movement of the sprite relative to 0 angle which is to the right. If the direction angle is not supplied, the current direction angle is maintained. If the direction angle is not supplied and there is no current velocity, the current rotation angle used for the direction angle. |
No return value. Changes output in the display after drawSprites() is called.
Found a bug in the documentation? Let us know at documentation@code.org