World.allSprites

Category:Game Lab

Returns an array containing all the active sprites.

Sometimes you need to perform some actions on all the active sprites. Instead of coding multiples statements, one for each active sprite, you can use allSprites which creates an array, an indexed collection, of sprites that you can process one at a time with a loop.

Examples

var sprite1 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
var sprite2 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
var sprite3 = createSprite(randomNumber(0, 400), randomNumber(0, 400));
var list = World.allSprites;
function draw() {
  background("white");
  drawSprites();
  for (var i = 0; i < list.length; i++) {
    list[i].height=randomNumber(10, 50);
    list[i].width=randomNumber(10, 50);
  }
}

Syntax

World.allSprites

Returns

An array containing all the active sprites.

Tips

  • If you want to perform some action on a subset of active sprites create a Group instead of using allSprites.

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