Unit 3: Lesson 20: Project Help

My Project

Hi Team! I have been trying to make my health only go down by 1 when Megaman hits the enemy, but am having trouble figuring out the code. Each time that my Megaman hits the enemy, his health continuously goes down. I understand why this happens but I am not sure that I know how to fix it. I know my students are going to ask how to do this, so I want to make sure I have an answer!

Please help! :slight_smile:

1 Like

@ncranga,

Happy to help! As Iโ€™m sure you guessed, the trick to this is to prevent Megaman from losing one health point for every frame of the draw loop when it is in contact with the enemy.

There are several different ways it could work. Sometimes, if you want the collision to result in the enemy disappearing, you could just change the x and y values of the enemy to be somewhere off the screen right upon collision and then subtract the health point. This way, they will instantly be separated and then the health counter is decreased.

in your situation, it just looks like you want the enemy to rotate and keep moving. So in this one, you will probably want to create a counter. The general idea is that you create a counter variable at the beginning of the program and set it to = 0.

Then, upon collision, you increase the counter by 1, but you donโ€™t subtract the health points yet. Then, right below that line of code, you use a second conditional and you need to see if the counter has reached a specific number yet. In your case, your health appears to decrease by 25 points, so you may want to check if the counter is greater than 20 (or so) โ€“ may have to experiment with that number. Then, if it is greater than 20, you would decrease the health points and reset the counter to zero.

So, the first twenty times through the draw loop when MegaMan is in contact with the enemy, the counter is working and then on the 21st frame, the health decreases. For a few more frames, the two are still colliding and your counter starts over, but then the collision passes and you should see your health points only decrease by one.

Hopefully this makes sense, but if you try it and arenโ€™t getting it to work, feel free to come back again and check in with us!

Mike

2 Likes

Great! I think I got it, but I would have never thought of that on my own! Thank you