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:
group.setVelocityEach()
group.setVelocityXEach()
and group.setVelocityYEach()
group.setSpeedAndDirectionEach()
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.
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(); }
group.setSpeedAndDirectionEach(speed, angle)
Name | Type | Required? | 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. |
No return value. Changes output in the display after drawSprites() is called.
drawSprites()
is called.Found a bug in the documentation? Let us know at documentation@code.org