Issue with projectile overlapping enemy sprites and if statements

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

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.

1 Like

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

You are welcome.

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.

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.

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.