Lifebar questions

GamepencilerGamepenciler Artist/Game DeveloperMember, PRO Posts: 326

Hello guys, I have a question about lifebars.
I want to have a character that has life that levels up
like:
Lv: 1 = 50 HP
Lv: 2 = 75 HP
Lv: 3 = 100 HP

But i want to constrain the value to a fixed with of 100 pixels
I can't find any tutorial that relates to my question. The one I found was when the life changes the width increase.
How can it should stay 100 pixels even if the life value changes?

Best Answer

  • ArmellineArmelline Posts: 5,415
    Accepted Answer

    This demo I made for someone else allows you to easily change the max value of the life bar without the bar itself increasing in size.

Answers

  • SummationSummation Member, PRO Posts: 476

    @ecanhoj said:
    Hello guys, I have a question about lifebars.
    I want to have a character that has life that levels up
    like:
    Lv: 1 = 50 HP
    Lv: 2 = 75 HP
    Lv: 3 = 100 HP

    But i want to constrain the value to a fixed with of 100 pixels
    I can't find any tutorial that relates to my question. The one I found was when the life changes the width increase.
    How can it should stay 100 pixels even if the life value changes?

    If you have 100 pixels then I would translate your HP to percentage so that the health bar will always display the correct percentage of health.

  • GamepencilerGamepenciler Artist/Game Developer Member, PRO Posts: 326

    Ok, how do i convert it into percentage?

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    To clarify, are you wanting the HP bar to start at 100 pixels no matter the HP value and go down towards zero pixels as HP decreases? As @Summation suggested you'd constrain the width to game.HPremaining/game.HP*100.

  • SummationSummation Member, PRO Posts: 476
    edited May 2015

    @ecanhoj said:
    Ok, how do i convert it into percentage?

    Something like...

    game.currentHP/game.maxHP

    This will give you the percent, right?
    For example, if my max is 100 and my current is 25, I have 25%.
    The thing is... 25/100=0.25

    So, the formula needs one modification.
    If you multiply the result times 100...

    (game.currentHP/game.maxHP) times 100
    (25/100) times 100=25

    If you have a health bar with 100 pixels and you display 25, you are displaying 25%.

  • SummationSummation Member, PRO Posts: 476

    @tatiang said:
    To clarify, are you wanting the HP bar to start at 100 pixels no matter the HP value and go down towards zero pixels as HP decreases? As Summation suggested you'd constrain the width to game.HPremaining/game.HP*100.

    You may also consider rounding the result either up or down to the next whole number just to make sure (but this probably isn't necessary...?)

  • ArmellineArmelline Member, PRO Posts: 5,415
    edited May 2015

    Resposting the demo for anyone who wants it, as accepting an answer hides attached files. ( @ForumNinja anything you can do about that?) Glad it could help though!

  • GamepencilerGamepenciler Artist/Game Developer Member, PRO Posts: 326

    Thanks guys: @Armelline @Summation @tatiang i think all of you have the same logic.. I will try this out soon...

Sign In or Register to comment.