group.setRotateToDirectionEach()

Category:Groups

Lock the rotation property of each sprite in the group to the sprite's movement direction.

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

Examples

Swarm

Swarm of bees following the mouse.

// Swarm of bees following the mouse.
var group = createGroup();
for (var i = 0; i < 100; i++) {
  group.add(createSprite(randomNumber(150, 250), randomNumber(150, 250), 2, 2));
}
group.setAnimationEach("bee_1");
group.setScaleEach(0.5);
group.setRotateToDirectionEach(true);
function draw() {
  background("white");
  drawSprites();
  var direction=(180*Math.atan2(World.mouseY-200, World.mouseX-200))/Math.PI;
  group.setSpeedAndDirectionEach(10, direction);
}

Syntax

group.setRotateToDirectionEach(bool)

Parameters

NameTypeRequired?Description
bool Boolean Should each sprite in the group rotate to its movement direction or not.

Returns

No return value. Changes output in the display after drawSprites() is called.

Tips

  • Changing the sprite's location does not change the sprite's direction. Instead use the group velocity and direction functions: setVelocityXEach(), setVelocityYEach(), setVelocityEach(), setSpeedAndDirectionEach()
  • Groups of sprites all have the same functions and you use the dot notation (combining the name of the group, followed by a dot, with the function name) to call the function for that group of sprites.
  • 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