Sprite Interactions

Most games will involve sprites colliding with each other. There are four types of collisions available in Game Lab: displace, collide, bounce, and bounceOff. These blocks will cause a certain type of interaction between the sprite and its target and must be used within the draw function.

Displace

displace makes a sprite push another sprite (the target) as long as they are touching each other.

function draw() {

  background("skyblue");
  bunny.displace(carrot);
  drawSprites();

}

Collide

collide makes a sprite stop when it runs into things. You can use it when you want your sprite to be blocked by obstacles.

function draw() {

  background("black");
  alien.collide(monster);
  drawSprites();

}

Bounce

bounce makes the sprite and the target bounce when they touch each other. Both the sprite and the target change how they are moving.

function draw() {

  background("lightgreen");
  giraffe.bounce(hippo);
  elephant.bounce(pig);
  snake.bounce(monkey);
  drawSprites();
}

BounceOff

bounceOff makes the sprite bounce off the target when they touch each other. The target keeps moving as before.

function draw() {

  background("pink");
  note.bounceOff(guitar);
  drawSprites();

}

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