How do I make a health bar that goes down over time?

0 favourites
  • 5 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • So I'm making an animal game where the health goes down and if it hits zero they die, so how do I make it go down over time? OH, and how do I make it go up? like if I feed them with food.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Give the object for the animal an instance variable health, make it 10 for this example.

    In the event sheet add system expression: every x seconds, make it 5.

    For action behind this every 5 seconds event add: animal object, subtract 1 health.

    Now it removes 1 health every 5 seconds.

    Give that every 5 seconds event a blank subevent, and add event: animal object, compare value: health = 0

    After the compare add actions that goes with your animal object being without health.

    As for gaining health, lets add some food when the animal bumps into it, add a food object/sprite.

    Add a new event: Animal object is colliding with food object, action: animal object add 1 to health, and another action: destroy food object.

    Voila, your animal object looses health every 5 seconds, and can gain health by getting some food.

  • Okay... So I think I got everything but where is the system expression? and how would I make it so the food comes back? If you could help with this I will be happy

  • Looks like lennaert's answer covers only the health variable itself. Here is how you would do a proper health bar:

    1. Just like lennaert suggested, add an instance variable "health" and give it some default value, e.g. 10

    2. Create a tiled background 1x1 which is just a red dot.

    3. (Do this step if there are multiple animals planned) Click on your animal object and in the left menu select "Container" Create. Select the red dot. Containers tie objects to each other, so when a new animal is created, it will have a red dot created as well and assigned to that particular instance.

    Let's say the sprite of animal is called "Animal" and the red-dot tiled background is "animalHealth".

    4. In the event sheet add a new event "For every" - select Animal; with two action, action 1: animalHealth - set position - X: Animal.X Y:Animal.Y; action 2: animalHealth - Set width - Animal.health variable*5. This way the health bar will always follow its animal and display its health.

    5. Add another event "Every X seconds"; action 1: Subtract from - Animal.health variable - 2.

    Afterwards it depends on how would you like to feed your animals. Perhaps, simply clicking on it would do the job.

    Event: Mouse - On object clicked - Animal; action: Animal - Add to - health variable - 5.

  • if you add an event or action, the first object in the top left, is "System".

    And for creating new food, that kinda depends on your game, at this level, I would suggest using another every X seconds event, and have it create a food object somewhere on the map, perhaps random.

    event: every 3 seconds

    action: system-> create object Food at position x: random(LayoutWidth), y: random(LayoutHeight);

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