# sprite.bounceOff()

Category:Sprites

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

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.

## 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;
function draw() {
background("white");
sprite1.bounceOff(sprite2);
drawSprites();
}
```

### Four Collisions

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();
}
```

### Breakout

Keep the apple in the air by moving the paddle with the left and right arrows.

```// Keep the apple in the air by moving the paddle with the left and right arrows.
var paddle = createSprite(200, 375, 50, 10);
var apple = createSprite(200, 50);
apple.setAnimation("apple");
apple.velocityX=randomNumber(-1, -1);
apple.velocityY=randomNumber(5, 10);
apple.setCollider("circle",0,0,40);
function draw() {
background("white");
drawSprites();
if (keyDown("right")) {
}
if (keyDown("left")) {
}
if (apple.x<0 || apple.x>400) {
apple.velocityX=-1*apple.velocityX;
}
if (apple.y<0) {
apple.velocityY=-1*apple.velocityY;
}
}
```

## Syntax

```sprite.bounceOff(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

• 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