How do I make enemies with health bars?

0 favourites
From the Asset Store
Health, Weapon & Rage Bars for your Games. Easy to use & Customize.
  • Hello guys I know there are already many tutorials/discussions on how to create a health bar for all enemies on the layout but when I read them I still don't understand... If you have time could you explain step by step & with images if possible?

    The help i need with is:

    Currently I have enemy on the layout from the start of the game but how to make for each enemy it's own healthbar so if I shoot 1 of the enemy it will only subtract health from 1 enemy not all of them. and if one dies play death animation only on the enemy that has < less then 1 Health.

    Thank you very much for any help! Much appreciated!

  • give the healthbar an instance variable with the value of the enemys UID. That's one way how to connect them.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Fireche how do I set an UID to the enemies?

  • Frap1K Every object has a UID (Unique ID) after spawning. You don't need to set it.

  • Fireche

    Is this correct?

    And how/where do I put value of the enemies UID?

  • Frap1K create a new instance variable for the hpbar. When a new enemy spawns you spawn aswell a new hpbar and set its instance variable to the UID of the enemy UID.

  • Fireche But that works with every tick >> spawn object? Is it the same as if I just place them on the map/overlay? like it's shown above on the image?

  • yes..you can simply place it that will work aswell. Or you can make a code which will look like this: "pick every enemy -> enemy spawn hpbar -> hpbar set instance variable to enemy.UID"

  • Rather than doing it using UID, use containers from the properties bar on the left and add a single healthbar object into the container of the enemy/unit. Then you can just do something like this capx: https://dl.dropboxusercontent.com/u/471 ... thBar.capx (pretty much the same as the Construct Classic one actually <img src="{SMILIES_PATH}/icon_razz.gif" alt=":-P" title="Razz"> )

    To learn more about containers check this manual page: https://www.scirra.com/manual/150/containers

    If you only wanted enemy healthbars to appear when the enemy is being hurt, make it visible/invisible rather than creating and destroying it (as objects in containers are created and destroyed together automatically).

    Hope that helps! <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

  • Jayjay Trying this now thank you for replying!

  • Containers are definitly the better way but i figured that using the UID is easier to understand for beginners.

  • Fireche & Jayjay Thanks a a lot for helping I don't know what I would do with out you!

    I made 2 sprites on the zombie - 1 is Head, 2 is body.

    • When the bullet collides with the Body subtract 20 from health.
    • When the bullet collides with the Head kill instantly.

    But the clone ( copied zombie ) still dies if i kill one of them .... Please help

    here is the image of the event sheet... Direct Link - upload.ee/image/4715414/testing.png

    Thank you for any future help! It really helps me!

  • add zombieheadbox and zombiebodybox into the container for the zombie, and make sure there is only one of each type these objects in the layout: zombie_Health, zombieHeadBox, zombieBodyBox.

    The system will automatically create those afterward when the game is started/running for each zombie, so you can place or create or destroy zombie objects and the other stuff will be added and removed for you <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    That also means that the actions you made for sub-events 42 and 43 can be moved straight into the "for each Zombie" in event 41. In fact, you can copy the actions from event 44 into 41 as well and delete that.

    That way there is only:

    41 For Each Zombie -> All the actions from events 42, 43, 44

    42 (was 45) Bullet: On collision with zombieBodyBox -> Zombie subtract 20 from Health

    43 (was 46) Zombie: Health < 1 -> Set animation to ....

    However, that last event might be better as a sub-event to 41 you might have to test for that

    Hope that helps! Otherwise, please upload your capx to Dropbox or Google Drive (free) and then share a link here (to get past the link filter replace the .'s with (dot)'s eg: http://www.google.com/ becomes http://www(dot)google(dot)com/)

  • Jayjay Thank you so much for explaining to me this mess! Will try to make it now, but I guess ill get stuck on the 41 as Im unsure what this means :

    (zombieBodyBox.Health / zombieBodyBox.MaxHealth) * zombie_Health.MaxWidth

    It devides the full health from health ? and times it by it's width ? If you have time could you explain this to me ?

    Thank you for your attention!

  • Sure thing Frap1K, here's a break-down of how that works:

    MaxWidth is a variable I gave to the healthbar that is literally a width value for when health is at its max size (it shrinks as health decreases). This could be any number, but usually should probably be the width of the object it represents (so full health has the bar the size of the object), so it could be 32px let's say.

    MaxHealth is a variable to represent what full health is (eg: 20)

    Health is the current health (eg: 20 to start)

    The formula is taking a ratio of current health to max health to make it a number between 0% and 100% (0 and 1).

    It then multiplies that by the max width, to represent how full the bar is.

    So in our examples it would be like this at the start:

    Healthbar width = (20 / 20) * 32 = (1) * 32 = 32px

    But at half health it would be:

    Healthbar width = (10/20) * 32 = (0.5) * 32 = 16px

    At 0 health it's:

    Healthbar width = (0/20) * 32 = (0) * 32 = 0px

    Hope that helps!

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