group.remove()

Category:Groups

Remove a sprite from the group.

In some animations or games you might need to remove a sprite from a group so it will act on its own instead of with the group. remove() does not delete sprite. Sprite.remove() will also remove the sprite from all the groups it belongs to.

Examples

// Press 1, 2 or 3 to remove a sprite from the group and have it stop following the mouse.
var box1 = createSprite(75, 200, 50, 50);
var box2 = createSprite(200, 200, 50, 50);
var box3 = createSprite(325, 200, 50, 50);
var group = createGroup();
group.add(box1);
group.add(box2);
group.add(box3);

function draw() {
  background("white");
  drawSprites();
  if (keyWentDown("1")) {
    group.remove(box1);
  }
  if (keyWentDown("2")) {
    group.remove(box2);
  }
  if (keyWentDown("3")) {
    group.remove(box3);
  }  
  var direction=(180*Math.atan2(World.mouseY-200, World.mouseX-200))/Math.PI;
  group.setSpeedAndDirectionEach(1, direction);
}

Syntax

group.remove(sprite)

Parameters

NameTypeRequired?Description
sprite Sprite The sprite to remove from the group.

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