Lives decreasing too fast

Ive set up a gui health bar that looks like this:
image
When the player hits an enemy the player script detects the collision and sends a message to the gui script to cover up 1/3 of the life bar to show that there are only 2 lives remaining. My problem is that I know the collision message between the player and enemy is made every frame which is a problem becuse it means that it only takes 3 frames to run out of lives. Do you know how i can only have it so that my player only loses 1 life per collision. I have no idea how i would code/impliment this so any hept is greatly appreciated

Basically almost exactly this:

When the collision starts, send the damage message and set a flag to prevent further messages being sent. When the collision ends, unset the flag.

how do i know when the collision ends?

Have you followed the example I linked to in the other post?

1 Like

i did but it kept running, i think this is becuase it only stopped the player for 1 frame which is such a small amount of time. Is there any other way?

No, that’s not what britzl’s example was designed to do. Properly implemented, it should trigger only once when the collision starts. It should then not repeat until after the collision clears.

I don’t think you should just give up and ask for another solution. Take your time and work through the logic. Try to understand what you are doing. Refer to the manuals and the API. If at that point you are still stuck, then ask a question - but make sure to really clearly explain what you have done (including all relevant code snippets). We are here to help, but not write your projects for you.

5 Likes

Having looked at some of the code i’ve already made i have a built in timer that times how long it takes the player to complete the level. I’m thinkint about making a new timer that will start when the player and enemy collide then counts to 3 seconds. If a another collision message is made with in that 3 seconds it will not take a life away from the player and after that 3 seconds the timer resets. I’m designing how this would work now, but from your experiance is this sort of thing done/worth doing?

Yes, granting temporary invincibility after being hit is a common mechanic. It would also serve to circumvent your issue, because it would be fair enough to lose a life every 3 seconds if you stay within the bounds of the enemy. I would still encourage you to understand britzl’s example, however I would also say that it’s a good thing you’re thinking creatively about alternate solutions! I do that myself all the time in order to avoid getting bogged down with technical things I’m not comfortable with.

1 Like

I did impliment britzl’s solution but it didn’t work for me, im sure it was an issue on my end from not coding a variable or something properly. I’l have another go at it tomorrow, definatly not today after the England loss in the six nations :sleepy:

1 Like

Perfect time to celebrate with some coding!

(Greetings from Scotland)

2 Likes