how to constrain a health bar to zero over time?

I want the health bar to decrease its "width" (and the speed that its decreasing) over time during the gameplay. The player can add health though, so interpolate did not work for me. any solutions?
Thanks!

Comments

  • KillerPenguinStudiosKillerPenguinStudios Member Posts: 1,291

    Hey @Approw,

    Interpolate won't work properly for a number of reasons. What you need to do is constrain the health bar width to a number. I will try and help explain it here and hopefully it will make sense. :D

    • First create a game real attribute and call it something like health time.

    Lets say you want it to reach 0 after 60 seconds. (Change according to what you want)

    • Set the game attribute you just made, game.health time to 60 (or what ever you need)

    Next we need to determine what the max length of the health bar will be. For my example I will say the length will be 450 pixels.

    Now go into the health bar actor:

    • Constrain attribute self.size.width = game.health time * 7.5

    How I got the 7.5?

    So the maximum health time allowed is 60 second, so 450 / 60 = 7.5. We used 450 because that is the width of the time bar we determined we wanted. If you wanted your time bar to be 250 just do 250 / 60 = 4.16. Then you would replace the 7.5 with 4.16, etc.

    Then in an actor in the scene, have a rule that says;

    • When game.health bar is greater than or equal to 0
    • every 1 second (change according to your game and how often you want the health bar to go down) change game.health time to game.health time - 1. According to this rule, it will change the width of the time bar.

    Have the next following rules (these are to be on the safe side just in case)

    • When game.health time is greater than 60 (or what ever number you need for your game that you used above)
    • Change game.health time to 60 (or what ever number you need for your game that you used above)

    • When game.health time is less than 0

    • Change game.health time to 0

    I believe that is everything. Hopefully this works out for you and one last thing.

    When restarting your game, level, new scene, or anything along those lines, change game. health time back to 60 (or what ever number you need for your game that you used above)

  • ApprowApprow Member Posts: 703

    @KillerPenguinStudios Thanks for your reply! does this method make the health bar decrease smoothly? because I had something working, but it jumped to the width instead of a smooth animation. I hope you can understand what I'm trying to say haha. thanks

Sign In or Register to comment.