sprite.rotateToDirection

Category:Sprites

Lock the rotation property of the sprite to the sprite's movement direction and vice versa.

You animations and games are more realistic if the sprites turn in the direction they are moving.

Examples

Pacing

Use sprite.rotateToDirection with sprite.velocityX to make a sprite pace back and forth.

// Use sprite.rotateToDirection with sprite.velocityX to make a sprite pace back and forth.
var sprite = createSprite(200, 200, 50, 50);
sprite.setAnimation("bunny1_1");
sprite.rotation = 180;
sprite.scale = 0.5;
sprite.rotateToDirection=true;
sprite.velocityX = 5;
function draw() {
  background("white");
  if (sprite.x>300) {
    sprite.velocityX = -5;
  }
  if (sprite.x<100) {
    sprite.velocityX = 5;
  } 
  drawSprites();
}

Walking in a Circle

Use sprite.rotateToDirection with sprite.rotation to make a sprite walk in a circle.

// Use sprite.rotateToDirection with sprite.rotation to make a sprite walk in a circle.
var sprite = createSprite(200, 200, 50, 50);
sprite.setAnimation("bunny1_1");
sprite.rotation = 180;
sprite.scale = 0.5;
sprite.rotateToDirection=true;
sprite.velocityX = 5;
function draw() {
  background("white");
  sprite.rotation=sprite.rotation+5;
  drawSprites();
}

Syntax

sprite.rotateToDirection

Returns

Boolean true or false.

Tips

Found a bug in the documentation? Let us know at documentation@code.org