Characters with health bars – Corona SDK

In this tutorial I’m going to show you how to implement a sprite class with health-bar functionality (sounds like a very weird bar). The end result should be something like this

Screen Shot 2014-12-16 at 07.39.58

Whilst we are going to use OOP techniques (Object Oriented Programming) this is not an Lua OOP tutorial, there are many of those out there already.

Let’s start by creating a new Corona SDK project. Go ahead and do that and be ready with your favourite editor. Create a new Lua file and name it “Character”. At the top of the file declare a table like this

Next we are going to define a constructor so next type

I’ve called the constructor “new” but really could have called it anything, I just think it’s a good convention. Now insert the following into the function

Replace “Chilli.png” with an image or your choice. At this point we can test that everything works. Open up your main.lua file and type the following

Now run the project. If everything worked correctly you should end up with something like this

Screen Shot 2014-12-16 at 08.14.25

 

Next we are going to implement the health-bar. Rather than build this into our Character class it makes sense to create a separate class for the health-bar so that it can be reused in other things. Create a new Lua file and call it HealthBar.lua. As we did for the Character class we are going to create a table and a constructor so add the following code

The rest of this tutorial will follow shortly…

2 thoughts on “Characters with health bars – Corona SDK

Leave a Reply

Your email address will not be published. Required fields are marked *