group.overlap()

Category:Groups

Makes each sprite and the target overlap when they touch each other. Neither the sprite and the target change how they are moving.

Most games will involve sprites colliding with each other. This is the fifth, and default, type of collision available in Game Lab. The collision blocks will cause a certain type of interaction between the sprite and its target and must be used within the draw function.

group.isTouching() is effectively the same, but is strictly a boolean check.

Examples

Shield or No Shield

The up key turns on the shield and makes the dots bounce off, otherwise they overlap.

                            // The up key turns on the shield and makes the dots bounce off, otherwise they overlap.
createEdgeSprites();
var block = createSprite(200, 200);
var group = createGroup();
for (var i = 0; i < 100; i++) {
  var sprite = createSprite(randomNumber(0, 400), randomNumber(0, 400), 5, 5);
  sprite.shapeColor='red';
  sprite.setVelocity(randomNumber(1, 5), randomNumber(1, 5));
  group.add(sprite);
}
function draw() {
  background("white");
  drawSprites();
  if (keyDown("up")) {
   group.bounceOff(block);
  }
  else {
    group.overlap(block);
  }
  group.bounceOff(topEdge);
  group.bounceOff(bottomEdge);
  group.bounceOff(leftEdge);
  group.bounceOff(rightEdge);   
}
                        

Syntax

group.overlap(target)

Parameters

NameTypeRequired?Description
target Sprite or Group The name of the target sprite or target group you want to check for a collision.

Returns

Boolean true or false. Changes output in the display after the sprites touch and drawSprites() is called.

Tips

  • Later created sprites will overlap earlier created sprites. You can change the overlap oder by updating the sprite.depth property.
  • All four of the collisions are similar to including an "if (sprite.isTouching(target))" in the draw function, and then depending on the collision type, updating the sprite and target velocityX and velocityY properties.
  • Only one of the four types of collisions should be specified for each pair of sprites.
  • To fine tune your collision detection use setCollider to change the shape and size of the collider area and set debug to true for the sprites.
  • A sprite that is not visible can still collide with other sprites and user mouse interactions.

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