group.setSpeedAndDirectionEach()

Category:Groups

Set the speed and direction of every sprite in the group.

The velocity, their speed and direction, of all the sprites in a group can be set in many ways:

Positive speeds move the sprite in the direction of the angle, negative speeds move the sprite in a direction opposite the angle. The default direction angle is 0, to the right, and clockwise increasing. Usually a number from -360 and 360.

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);
}
                        

var group = createGroup();
group.add(createSprite(200, 200, 100, 100));
group.add(createSprite(200, 100, 50, 50));
group.setSpeedAndDirectionEach(randomNumber(-3, 3), randomNumber(-45, 45));
function draw() {
  background("white");
  drawSprites();
}

Syntax

group.setSpeedAndDirectionEach(speed, angle)

Parameters

NameTypeRequired?Description
speed Number The rate of change in movement of each sprite per framerate.
angle Number The direction angle of the movement of each sprite relative to 0 angle which is to the right. If the direction angle is not supplied, the current direction angle is maintained. If the direction angle is not supplied and there is no current velocity, the current rotation angle used for the direction angle.

Returns

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

Tips

  • 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