Make a Legend of Zelda Styled Health Bar with Hearts! (in 14 events!!)

6

Index

Attached Files

The following files have been attached to this tutorial:

.capx

hearts-tut.capx

Download now 173.59 KB

Stats

12,282 visits, 35,073 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 2 Apr, 2014. Last updated 19 Feb, 2019

Saving the heart creation position

Now, let's create another Global Variable called lastHeartPositionX. It will store the X position of the last heart you create.

Let's make a loop that will create hearts. In fact, we will create maxLives hearts.

- I set lastHeartPositionX on 16 to start because that's where my first heart is. That is important for my algorithm, but can be easily changed.

- I didn't comment until now because there was only 1 event, but notice that I will be commenting everything from now on - AND SO SHOULD YOU!

Let's try and understand what we're doing here:

1) We already have 1 heart in our Layout that will be there anyway. This means we need to create 4 more (since our maxLives is 5). That means we want to repeat our heart object creation maxLives - 1 times.

2) Inside this loop, we are going to Create a new object, our heart object. Choose whatever layer you are using right now, but check our X and Y coordinates:

    X = lastHeartPositionX + heart.Width + 2
    Y = heart.Y

Let me break this for you:

a) lastHeartPositionX is the left side of the first heart (because that's where the Origin is). It's value right now is 16.

b) heart.Width is how much to the left we want to put our new heart. Without this, we'd have our new heart above the old one. My heart sprite is 32x32, so this value is 32.

c) 2 is just to make the new heart a little more away from the first one. You can put some bigger number, but this works just fine for now.

IMPORTANT AND CRUCIAL STUFF!!!

After that, notice that we set lastHeartPositionX to heart.X, and I can see you asking why. If you think this will get the X position for the very first heart (or any other), you are VERY wrong.

Since we created a heart in the line above this one, we are referencing that particular instance. That means we are setting, well, the X position of the last heart to the lastHeartPositionX variable.

See? Pretty easy, huh?

I know, loops can be confusing, but when you get the hang of it, they are really useful!

If you did everything right, you should have something like this:

  • 0 Comments

Want to leave a comment? Login or Register an account!