group.pointToEach()

Category:Groups

Rotate every sprite ionthe group to face the (x,y) coordinate.

In some animations or games you need sprites to point in the direction of a mouse action or another sprite. You can get the x and y coordinates from the mouse or sprite.

Examples

var group = createGroup();
for (var i = 0; i < 20; i++) {
  group.add(createSprite(randomNumber(0, 400), randomNumber(0, 400), 20, 20));
}
function draw() {
  background("white");
  drawSprites();
  group.pointToEach(mouseX, mouseY);
}

Syntax

sprite.pointToEach(x,y)

Parameters

Name Type Required? Description
y Number The y-coordinate you want the sprites to turn towards. Usually a number from 0 to 400, but smaller or larger numbers off the display area will also work.
x Number The x-coordinate you want the sprites to turn towards. Usually a number from 0 to 400, but smaller or larger numbers off the display area will also work.

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