Makes the sprite push the target as long as they are touching each other. The sprite keeps moving normally.
Most games will involve sprites colliding with each other. There are four types of collisions available in Game Lab: bounce
, bounceOff
, collide
and displace
. These blocks will cause a certain type of interaction between the sprite and its target and must be used within the draw
function.
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; function draw() { background("white"); sprite1.displace(sprite2); drawSprites(); }
Demonstrate all four types of collisions.
// Demonstrate all four types of collisions. var spriteDisplace1 = createSprite(75, 50, 50, 50); spriteDisplace1.setAnimation("displace"); spriteDisplace1.scale=0.75; spriteDisplace1.velocityX=2; var spriteDisplace2 = createSprite(250, 50, 50, 50); spriteDisplace2.setAnimation("displaceTarget"); spriteDisplace2.scale=0.75; var spriteCollide1 = createSprite(75, 150, 50, 50); spriteCollide1.setAnimation("collide"); spriteCollide1.scale=0.75; spriteCollide1.velocityX=2; var spriteCollide2 = createSprite(250, 150, 50, 50); spriteCollide2.setAnimation("collideTarget"); spriteCollide2.scale=0.75; var spriteBounce1 = createSprite(75, 250, 50, 50); spriteBounce1.setAnimation("bounce"); spriteBounce1.scale=0.75; spriteBounce1.velocityX=2; var spriteBounce2 = createSprite(250, 250, 50, 50); spriteBounce2.setAnimation("bounceTarget"); spriteBounce2.scale=0.75; var spriteBounceOff1 = createSprite(75, 350, 50, 50); spriteBounceOff1.setAnimation("bounceOff"); spriteBounceOff1.scale=0.75; spriteBounceOff1.velocityX=2; var spriteBounceOff2 = createSprite(250, 350, 50, 50); spriteBounceOff2.setAnimation("bounceOffTarget"); spriteBounceOff2.scale=0.75; function draw() { background("white"); spriteDisplace1.displace(spriteDisplace2); spriteCollide1.collide(spriteCollide2); spriteBounce1.bounce(spriteBounce2); spriteBounceOff1.bounceOff(spriteBounceOff2); drawSprites(); }
The umbrella displaces the raindrops.
// The umbrella displaces the raindrops. var umbrella = createSprite(200, 200); umbrella.setAnimation("umbrella"); umbrella.setCollider("circle", 0, 10); var raindrop = createSprite(200, 200); raindrop.setAnimation("raindrop"); raindrop.scale = 0.1; raindrop.setCollider("circle"); raindrop.x = randomNumber(100, 300); raindrop.y = 0; raindrop.velocityY = 10; function draw() { background("white"); umbrella.displace(raindrop); drawSprites(); if (raindrop.y > 400) { raindrop.x = randomNumber(100, 300); raindrop.y = 0; } }
sprite.displace(target)
Name | Type | Required? | Description |
---|---|---|---|
target | Sprite or Group | The name of the target sprite or target group you want to check for a collision. |
Boolean true or false. Changes output in the display after the sprites touch and drawSprites() is called.
draw
function, and then depending on the collision type, updating the sprite and target velocityX
and velocityY
properties.setCollider
to change the shape and size of the collider area and set debug
to true for the sprites.Found a bug in the documentation? Let us know at documentation@code.org