sprite.setSpeedAndDirection()
Set the speed and direction of the sprite.
The velocity of the sprite, its speed and direction, can be set in many ways:
- by using the "counter pattern" on
sprite.x
andsprite.y
properties within thedraw()
function - by setting the
sprite.velocityX
andsprite.velocityY
properties - by calling
sprite.setVelocity()
orsprite.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.
Examples
var sprite = createSprite(200, 200);
sprite.setSpeedAndDirection(2,45);
function draw() {
background("white");
drawSprites();
}
Circling
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);
}
Syntax
sprite.setSpeedAndDirection(speed, angle)
Parameters
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. |
Returns
Found a bug in the documentation? Let us know at support@code.org.