sprite.setSpeedAndDirection()

Category:Sprites

Set the speed and direction of the sprite.

The velocity of the sprite, its speed and direction, can be set in many ways:

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

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);
}

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

Syntax

sprite.setSpeedAndDirection(speed, angle)

Parameters

NameTypeRequired?Description
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.
speed Number The rate of change in movement of the sprite per framerate.

Returns

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