sprite.overlap()

Category:Sprites

Makes the 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.

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

Examples

var sprite1 = createSprite(75, 250, 50, 50);
sprite1.velocityX=2;
sprite1.velocityY=-1;
var sprite2 = createSprite(250, 250, 50, 50);
sprite2.velocityX=-1;
sprite2.velocityY=-0.5;
// since sprite2 was creasted second it has a higher depth number and will overlap sprite1.
function draw() {
  background("white");
  sprite1.overlap(sprite2);	// Since overlap is the default collision this code would work the same without this statement.
  drawSprites();
}

Syntax

sprite.overlap(target)

Parameters

Name Type Required? 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 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 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