ONE Health Bar for each Enemy

This forum is currently in read-only mode.
From the Asset Store
Give Sound to the enemies that are part of your game! :)
  • Welcome all, and sorry for my english (in advance).

    Question is:

    How to do one HP Bar, which is assigned for each of the enemy (something like in Diablo 2)

    Value of HP Bar would be depending on who (of enemy) was hit at a time and show his own HP. When sombody else get hit, then this same HP Bar will replace that value on new one.

    I did HP bars for each enemy separately, however when is only one HP Bar for all of them, then I have a problem to make it works.

    I hope that the question is understandable.

  • Oh, I know what you mean. I got 2 solutions. First requires one hp bar for each different kind of enemies. The second is one hp bar for all enemies.

    1. Make your enemy, make your hpbar. Click on the enemy, left is your properties bar, seek for the container option and add the hp bar for it.

    If you spawn your enemy, the hpbar will spawn also, linked tot he enemy.

    Make a private variable "hp". And then:

    "condition when player hits enemy:

    subtract 'value' from enemy('hp')

    set hpbar.width to (100/enemy_max_hp) * enemy('hp')"

    Replace the 100 for the with of the hpbar.

    2. I'm not sure with that one. I would try a "for each" loop.

    For each family(enemy):

    condition when player hits enemy:

    set hpbar.width to the private variable 'hp' of the family, again like in step one, with the rule of three.

    But I'm not very sure with this one^^. Someone has to reply/improve/add this one, so that it's right

    ps: I'm not one of the advanced users here, this the way I would do it, maybe someone else has better solutions :)

  • Thanks for reply.

    I made first of Your solution and when I hit one of the enemy and then hit other one, his HP Bar decrease but only to HP value of the first one that I hit.


    I hit ENEMY1 for 20, his HP decrease to 80

    I hit ENEMY2 for 20, his HP decrease to 80, I hit him again for 20, but his HP still 80

    So it's close but still something wrong.


    Second solution do this same. I hit somebody, HP Bar show how his HP decrease - but when I hit next enemy, HP Bar decrease and stop at the value of HP that first enemy had left.

    Some others idea?

  • The solution is correct, you are making something wrong Eternit.

    It's a simple rule of 3 equation, it can't be wrong.

    You need to put a fixed maximum value to the bar width when the Enemy HP are at 100%. And replace the number 100 to that maximum width value on the equation:

    set hpbar.width to (100/enemy_max_hp) * enemy('hp')"

  • "Set hpbar.width to (100/enemy_max_hp) * enemy('hp')" - this work great, problem is in something else.

    I noticed that the HP Bar overlap each other when I hit more than one opponent. Which means that I always see enemy HP who was hit already and also has the highest value of HP. I should see only one HP Bar at a time, not all of them overplaping each other - and that's the problem now.

  • Create a variable to the family Enemy to keep track of the last enemy hited.

    Always set all the bars to be invisible, and when you hit an enemy set the current variable of that enemy to keep track of its HP bar(value=1) and set all others to don't keep track of its HP bar (value=0).

    Now make visible only the current HP bar enemy variable that is keep tracking (value=1).

  • Probably I doing something wrong because it still not working. I don't know how to do ...

    ... and when you hit an enemy set the current variable of that enemy to keep track of its HP bar(value=1) and set all others to don't keep track of its HP bar (value=0).

    How to set to don't keep track others HP bar, where they are the same enemy but just duplicate? Enemy have HP container so it's should work, right? I don't know ... my mind is just blow away.

    Can I ask You for more detailed reply?

  • it's a clearer solution in my point of view if you using a global variable("enemy_hit_bar" for example), and a private variable("id"), assign this to every enemy.

    When you hit the enemy, save it's pv(id) to the globalvar(enemy_hit_bar), after this, you can easily define wich is your target like this:

    for each enemy:

    if enemy.value('id') == globalvar('enemy_hit_bar'):

    <the code where you set the hp bar>

    this will pick the correct instance, and you can debug it a lot easier beacuse you don't have to check every instance by hand if something went wrong.

    Edit: and don't put the hp bar in the container when you only want to display one.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I just unnecessarily add container when I do zyblade first solution. I remove container, like You Pecek said in "Edit" and it's work great. Now it's so simple ...

    So if someone will have a similar problem, just do what zyblade said, but NOT add the container for HP bar.

    Thanks zyblade, Pecek and Metal_X for help!

  • No problem, the container makes it just easier to pick if they're more than just 1 hpbar/enemy^^ But you can do it without using the container. Hope to see some demo in the forum of your game. ;)

  • Hi, I know this thread is old but can I have a sample cap for this ?

    I'am using construct classic for our THESIS, can't use C2 becuase of it is not free.

  • can someone please post a capx of this with multiple health bars...I am fairly new and I cant seem to get it working. thanx.

  • thanks Jay, but unfortunately the capx is in version 120. i only have 119. is there a way you upload in this format? i appreciate it.

  • Potzero, sorry I haven't had much free time lately. The code is nearly identical to the free Construct Classic version if you can install version R2 of that:

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)