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.
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(); }
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(); }
sprite.rotateToDirection
Boolean true or false.
sprite.velocityX
, sprite.velocityY
, setVelocity()
, sprite.setSpeedAndDirection()
Found a bug in the documentation? Let us know at documentation@code.org