sprite.rotation

Category:Sprites

Clockwise rotation in degrees of the sprite.

A positive value will rotate clockwise, a negative value will rotate counterclockwise. The rotation usually will be a number between -360 and 360.

The default rotation is 0, which is in the positive x direction. All sprite properties can be both accessed and updated.

Examples

var sprite = createSprite(200, 200);
sprite.setAnimation("car_blue_1");
drawSprites();
sprite.rotation = 45;
drawSprites();

Skidding Car

Use slight random rotations to make a car skid.

// Use slight random rotations to make a car skid.
var sprite = createSprite(0, 200);
sprite.setAnimation("car_blue_1");
function draw() {
  background("white");
  sprite.x = sprite.x + 5;
  sprite.rotation = 90 + randomNumber(-3, 3);
  drawSprites();
}

Loop de Loop

Use polar coordinates and sprite.rotation to make an airplane perform loops.

// Use polar coordinates and sprite.rotation to make an airplane perform loops.
var startx=200, starty=200, angle=0, increment=10, radius=150;
var sprite = createSprite(startx, starty);
sprite.setAnimation("planeRed1_1");
sprite.mirrorX(-1);
function draw() {
  background("white");
  angle=angle+increment;
  sprite.x=startx+radius*Math.cos(angle * Math.PI/180);
  sprite.y=starty+radius*Math.sin(angle * Math.PI/180);
  sprite.rotation=60-angle;
  drawSprites();
}

Syntax

sprite.rotation

Returns

Clockwise rotation in degrees of the sprite.

Tips

  • This is not the sprite's movement direction, see getDirection instead.
  • Slight rotations in either direction make sprites look like they are jiggling or skidding while moving.
  • 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 documentation@code.org