# Issue with projectile overlapping enemy sprites and if statements

**URL:** <https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378>\
**Category:** Help\
**Created:** [January 6, 2021, 4:31pm UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378 "2021-01-06T16:31:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oblivionxyt](https://avatars.discourse-cdn.com/v4/letter/o/74df32/32.png) [@oblivionxyt](https://forum.makecode.com/u/oblivionxyt)\
**Post date:** [January 6, 2021, 4:31pm UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378/1 "2021-01-06T16:31:54Z")

</div>

While coding my game, I have found an issue in the code. This issue occurs when I cannot kill the enemies on the second floor, due to it not allowing me to place more than 2 if statements into the block where it says “projectile overlapping enemy sprites.” I would like to ask for help in finding if there is a fix to this situation.

Game: [https://makecode.com/\_bU02qeVjscoL](https://makecode.com/_bU02qeVjscoL)

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [January 7, 2021, 11:38pm UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378/2 "2021-01-07T23:38:51Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/147bdd7250ba7c91c08e3bae2ec6c3a838eca571.png)

There are a few flaws:

1. Overlap events  
For the ‘overlap’ events, you need to reference ‘sprite’ and ‘otherSprite’ variables in the overlap function (similar to how you reference ‘value’ in the for each value in the function ‘clearlevel’. I also changed the kind for ‘mySprite3’ (the Boss) to be ‘BossEnemy’. That way you can change the ‘BossHealth’ which is attached to ‘otherSprite’ (which is of kind ‘BossEnemy’).

2. On status bar zero status  
The variable ‘status’ represents the status bar that is attached to a sprite (or not). So for each event, you need to destroy the ‘sprite that _status_ is attached to’.

You might also what to consider changing the function ‘goo1hpbar’ to accept various parameters, if you are planning to create multiple enemies with the game. You could for instance edit the function to include values such as:

- tilemapColumn
- tilemapRow

The thing to keep in mind is, if you are planning to create similar sprites (of a certain kind), then you can group that functionality into a function, and call it multiple times. Even if you are creating multiple sprites with the same variable, you will need to find them later by searching by kind (or keeping them in an array). The variable of a sprite is just a reference, but if you find in later in a for each loop, or an event, you need to reference the ‘local’ values within that function.

> **[x lv. 1 beta 2 alpha (cosmos)](https://arcade.makecode.com/42054-70508-58242-17468)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

---

<div class="post-metadata">

**Author:** ![oblivionxyt](https://avatars.discourse-cdn.com/v4/letter/o/74df32/32.png) [@oblivionxyt](https://forum.makecode.com/u/oblivionxyt)\
**Post date:** [January 8, 2021, 4:03am UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378/3 "2021-01-08T04:03:29Z")

</div>

Thank you so much for your help! I understand it now and I’ll look to fix the issues in the code!

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [January 8, 2021, 8:27pm UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378/4 "2021-01-08T20:27:09Z")

</div>

You are welcome.

---

<div class="post-metadata">

**Author:** ![cosmoscowboy](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cosmoscowboy/32/1718_2.png) [@cosmoscowboy](https://forum.makecode.com/u/cosmoscowboy)\
**Post date:** [January 8, 2021, 10:54pm UTC](https://forum.makecode.com/t/issue-with-projectile-overlapping-enemy-sprites-and-if-statements/5378/5 "2021-01-08T22:54:33Z")

</div>

I put together a small demo of how you might spawn different types of sprites at different times. The goal of the game is to stay alive as long as you can.

> **[Creating multiple sprites](https://arcade.makecode.com/68996-97486-78631-18502)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

The math random function is used quite often, as with arrays, and also, IMHO, an unknown function of locating all sprites of a certain type (array of sprites of kind). You can find it at the bottom of the ‘Arrays’ section. Personally I think it should have been included in the ‘Sprites’ section.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/6c3a1e854a09e22a48a80c6493b3a905f6e24a1c.png)

Using the ‘Loops’ method, ‘for element value of list’, you can drag the ‘array of sprites of kind’ section to the ‘list’ variable, and loop through all the sprites of that kind. I use this within a ‘game update’ method to check if the ‘hero’ (player) is overlapping with an enemy or a fruit sprite. I prefer this type of programming, instead of using the ‘on overlap events’, as you might want to check other properties of the sprites during a game update.

Good luck and happy programming.
