sprite.rotateToDirection
Lock the rotation property of the sprite to the sprite's movement direction and vice versa.
Your 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
Tips
- Changing the sprite's location by updating sprite.x and sprite.y does not change the sprite's direction. Instead use the velocity and direction properties/functions:
sprite.velocityX
,sprite.velocityY
,setVelocity()
,sprite.setSpeedAndDirection()
Found a bug in the documentation? Let us know at support@code.org.