group.setVelocityEach()

Category:Groups

Sets the velocity in both the x and y direction for all the sprites in a group.

A positive x parameter will move the sprite to the right, a positive y parameter will move the sprite downward. Must be used with the draw() function containing a drawSprites(). You can also update the velocity of each sprite in a group using setSpeedAndDirectionEach() or by using setVelocityXEach() and setVelocityYEach().

The default velocity is 0 in both x and y directions.

Examples

// The gray dots have all the same velocity.
var group1 = createGroup();
for (var i = 0; i < 10; i++) {
  var sprite = createSprite(randomNumber(0, 400), 0, 5, 5);
  sprite.shapeColor='red';
  sprite.setVelocity(0, randomNumber(1, 10));
  group1.add(sprite);
}
var group2 = createGroup();
for (var i = 0; i < 10; i++) {
  var sprite = createSprite(randomNumber(0, 400), 0, 5, 5);
  group2.add(sprite);
}
group2.setVelocityEach(0, randomNumber(1,10));
function draw() {
  background("white");
  drawSprites();
}

Syntax

group.setVelocityEach(x, y)

Parameters

NameTypeRequired?Description
x Number The velocity of each sprite in the positive x direction, to the right.
y Number The velocity of each sprite in the positive y direction, downward.

Returns

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

Tips

  • Changing World.frameRate will affect the velocity.
  • 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.
  • If setColliderEach() is called for group before it is scaled, the collider is not scaled. But if a group of sprites is first scaled and then the collider set, the scale is also applied to the collider.

Found a bug in the documentation? Let us know at documentation@code.org