# I need help with this code

**URL:** https://forum.code.org/t/i-need-help-with-this-code/34032
**Category:** All Courses: Pedagogy and Best Practices
**Created:** [January 12, 2021, 6:04pm UTC](https://forum.code.org/t/i-need-help-with-this-code/34032 "2021-01-12T18:04:39Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![cherylbelaval](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@cherylbelaval](https://forum.code.org/u/cherylbelaval)
#### Post date: [January 12, 2021, 6:04pm UTC](https://forum.code.org/t/i-need-help-with-this-code/34032/1 "2021-01-12T18:04:40Z")

</div>

My student would want the planes to crash and an explosion appear if the planes come in contact. This is his code. Any help is very much appreciated!

var backround = createSprite(200, 200);  
backround.setAnimation(“desk\_1”);  
var spotlight = createSprite(0, 200);  
spotlight.setAnimation(“spotlight”);  
spotlight.visible = false;  
var gift = createSprite(125, 202);  
gift.setAnimation(“xmasemoji\_12\_1”);  
gift.scale = 0.3;  
var laptop = createSprite(125, -100);  
laptop.setAnimation(“laptop\_2”);  
laptop.scale = 0.3;  
var game = createSprite(200, 200);  
game.setAnimation(“background\_landscape07\_1”);  
game.visible = false;  
var plane = createSprite(0, 200);  
plane.setAnimation(“planeGreen1”);  
plane.visible = false;  
var red = createSprite(512, randomNumber(20, 390));  
red.setAnimation(“planeRed”);  
red.visible = false;  
var red2 = createSprite(-40, randomNumber(20, 390));  
red2.setAnimation(“planeRed2”);  
red2.visible = false;  
var red3 = createSprite(490, randomNumber(20, 390));  
red3.setAnimation(“planeRed3”);  
red3.visible = false;  
function draw() {  
if (mousePressedOver(gift)) {  
laptop.y = laptop.y + 2;  
gift.setAnimation(“open”);  
gift.scale = 1;  
gift.x = 50;  
gift.y = 214;  
spotlight.visible = true;  
spotlight.x = 200;  
spotlight.y = 66;  
}  
if (laptop.y \>= -98) {  
laptop.y = laptop.y + 2;  
}  
if (laptop.y \>= 216) {  
laptop.y = 216;  
}  
if (mousePressedOver(laptop)) {  
laptop.visible = false;  
gift.visible = false;  
backround.visible = false;  
game.visible = true;  
plane.visible = true;  
red.visible = true;  
red2.visible = true;  
red3.visible = true;  
}  
if (keyDown(“right”)) {  
plane.x = plane.x + 3;  
plane.setAnimation(“planeGreen1”);  
}  
if (keyDown(“left”)) {  
plane.x = plane.x - 3;  
plane.setAnimation(“left”);  
}  
if (keyDown(“up”)) {  
plane.y = plane.y - 3;  
plane.setAnimation(“up”);  
}  
if (keyDown(“down”)) {  
plane.y = plane.y + 3;  
plane.setAnimation(“down”);  
}  
if (keyDown(“up”) && keyDown(“right”)) {  
plane.setAnimation(“ne”);  
}  
if (keyDown(“right”) && keyDown(“down”)) {  
plane.setAnimation(“ew”);  
}  
if (keyDown(“left”) && keyDown(“up”)) {  
plane.setAnimation(“leftup”);  
}  
if (keyDown(“left”) && keyDown(“down”)) {  
plane.setAnimation(“leftdown”);  
}  
if (plane.y \> 369) {  
plane.setAnimation(“explosion”);  
plane.scale = 0.4;  
}  
if ((keyWentDown(“up”) || keyWentDown(“down”)) || (keyWentDown(“left”) || keyWentDown(“right”))) {  
red.x = red.x - 3;  
}  
if (red.x \<= 510) {  
red.x = red.x - 2;  
red3.x = red3.x - 5;  
red2.x = red2.x + 7;  
}  
if (plane.x == (((red.x - 41.5) \<= plane.x && red.x + 41.5 \>= plane.x) && ((red.y - 35) \<= plane.y && red.y + 35 \>= plane.y))) {  
plane.setAnimation(“explosion”);  
plane.x = 0.4;  
}  
drawSprites();  
}

---

<div class="post-metadata">

### Author: ![mmathews](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mmathews/32/4419_2.png) [@mmathews](https://forum.code.org/u/mmathews)
#### Post date: [January 14, 2021, 6:31am UTC](https://forum.code.org/t/i-need-help-with-this-code/34032/2 "2021-01-14T06:31:03Z")

</div>

> [@cherylbelaval](#):
>
> if (plane.x == (((red.x - 41.5) \<= plane.x && red.x + 41.5 \>= plane.x) && ((red.y - 35) \<= plane.y && red.y + 35 \>= plane.y))) {  
> plane.setAnimation(“explosion”);  
> plane.x = 0.4;  
> }

This is probably where you need to focus. Have you considered using the `sprite.isTouching(target)` block? It might be easier to use in this case.

```auto
if(plane.isTouching(red)){
   plane.setAnimation(“explosion”);
   plane.x = 0.4;
}

```

---

<div class="post-metadata">

### Author: ![cherylbelaval](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@cherylbelaval](https://forum.code.org/u/cherylbelaval)
#### Post date: [January 14, 2021, 2:55pm UTC](https://forum.code.org/t/i-need-help-with-this-code/34032/3 "2021-01-14T14:55:17Z")

</div>

Thank you!! Very much appreciated. I will try it today.
