Makes each sprite in the group push the target as long as they are touching each other. The sprites keep 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 each sprite and its target and must be used within the draw
function.
Use the left and right keys to avoid the raindrops or get pushed off the screen.
// Use the left and right keys to avoid the raindrops or get pushed off the screen. var umbrella = createSprite(200, 300); umbrella.setAnimation("umbrella"); umbrella.scale = 0.1; var group = createGroup(); for (var i = 0; i < 100; i++) { var sprite = createSprite(randomNumber(0, 400), randomNumber(-200, 0), 10, 10); sprite.setAnimation("raindrop"); sprite.scale=0.03; sprite.velocityY=randomNumber(1, 5); group.add(sprite); } function draw() { background("white"); group.displace(umbrella); drawSprites(); if (keyDown("right")) { umbrella.x = umbrella.x+10; } if (keyDown("left")) { umbrella.x = umbrella.x-10; } }
group.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