group.setLifetimeEach()

Category:Groups

Sets the lifetime, cycles before sprite self removal, for every sprite in the group.

Usually set before the draw() cycle to initiate a countdown. sprite.lifetime is reduced by 1 in every draw() cycle. At 0 it will call sprite.remove() for every sprite in the group. The default sprite lifetime is forever.

Examples

var group1 = createGroup();
for (var i = 0; i < 20; i++) {
  group1.add(createSprite(randomNumber(0, 400), randomNumber(0, 400),randomNumber(10, 20),randomNumber(10, 20)));
}
var group2 = createGroup();
for (var i = 0; i < 20; i++) {
  group2.add(createSprite(randomNumber(0, 400), randomNumber(0, 400),randomNumber(10, 20),randomNumber(10, 20)));
}
group1.setLifetimeEach(50);
group2.setLifetimeEach(100);
function draw() {
  background("white");
  drawSprites();
}

Syntax

group.setLifetimeEach(lifetime)

Parameters

NameTypeRequired?Description
lifetime Number The lifetime of the sprites in cycles of the draw loop.

Returns

No return value.

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