Creating an interactive object using Lua

On the Forums, there is a user by the handle of ggortva, this user asked for some help outlined here . I am taking that idea and creating a tutorial around it, that will help this user and others to understand how to get things done.

Right, so the brief that this user had was to have a flame on the screen, the intensity of this flame can be controlled by a slider. Then there is another object on screen, a plastic army man, this can be put into the fire, based on the intensity of the flame and the time it is on the fire, it can start to melt. So when ever you move the object bask, it will remain in that state (molten or whole). This was an interesting idea for a game (which I think he is making) but it triggered in capital letters, "TUTORIAL"... so here it is.

First Steps

First before we jump to creating anything, let us look at how are we going to approach this issue.We shall be creating two objects on the screen, the fire and the plastic soldier. The fire shall have an attribute of intensity which can be increased or decreased. The soldier shall also have an attribute that shall be health. The idea is that while the soldier is placed on the fire, the health of the soldier shall reduce by the intensity of the fire. So when the health of the soldier is 0, the soldier is totally a molten plastic mess. By using this technique, we can use toy soldiers of different material that have a varying degree of resistance to the fire. A metallic toy soldier would take a long time before it starts to melt and the paper soldier will kind of instantly burn.

What can I see?

Now we can work that out in code easy, but it is no fun if we do not have a graphical feedback. Now I am *not* a graphic artist, so this is outside of my forte, (I need some artists to help me on the graphics for the tutorials) Anyways, let us assume we have them, the way we could manage it is with using sprites. Why sprites, if you ask, the reason is simple, sprites can be used for frame by frame display, now if we do not play the animation, we can set the frame number and change the image of the object. If we were to use the newImage technique, we would have to create/remove/re-create objects which would be an unnecessary bother. So we would say that we have 5 states of each soldier, the 100%, the totally burnt 0% and then the in-between states at 20% 40% 60% and 80%.And for the fire, we will have 5 intensities too, which would be from a match stick to a Blow Torch.

Logic

We shall have the entire applications running by the ticks, not the little insects that cause a whole lot of problems for pets. This is more like a beat, a heartbeat/Clock tick. We get that from CoronaSDK when we set a listener on the enterFrame event on the Runtime. So what we do is on every Nth tick, we reduce the health of the toy soldier and then if the health is in the percentile that we have setup for images, we change the soldier's image frame number based on the health.

Easy, isn't it? Now, for once, let us have this like a competition and see how many of you come up with a good implementation of this idea. This will teach you something as in practice and at the same time it will also let us see a variety of ways that people approach an issue.

So, please send in your codes to me on dev [at] oz-apps [dot] com by the 15th of October.

Comments

Popular Posts