HOW TO MOVE YOUR GAME OBJECT LIKE LUNAR LANDER PART 2/2

Lunar Lander

In the first part of this tutorial we’ve learned how to move an object like a lunar lander. Now we will add some simple visual effects to make our lunar lander more eye-catching.

Adding new particle module

To make my code more organized, i will create a new lua module whose name is particles and then i will require it in my main.lua

If you want more information about creating external modules may be you can start from here.

In Particles module i will basically create new circles and give them some action by adding enterframe event to the runtime.

Let’s look at the Particles.lua

Particle module

Particles is actually a table with a function attached to it. In the first line i’ve defined that Particles is a table and in the third line i’ve added newParticle function to it. The last line is mandatory when you work with external modules because when you require it in the main.lua, it has to be returned.

As you can see in line 3, newParticle function has three parameters x,y and direction. x and y parameters will be the initial position of our circle and direction will determine which direction our circles will move after they’ve been created.

In the newParticle function at line 5 i’ve created my display object circle and at line 26 i’ve added an enterFrame event to runtime and i used that circle as listener of enterFrame event.

At lines 8-25 i’ve defined an instance method of particle with the name enterframe so this method will be called at every frame.

What this method basically do in every frame is to decrease alpha, xscale and yscale values of circle while it is moving according to its direction parameter.

At lines 20-24, if our circle’s xscale value is smaller than 0.01 then i remove the enterframe event added to runtime, remove the display object and make it’s variable equal to nil to free the memory completely.

My plan is this: When i call newParticle function a circle will be added to scene and it will start to move to a certain direction while it is getting smaller and disappearing. After the circle is completely disappeared it will be removed from the memory.So the question is when i should call newParticle function and what should x,y and direction parameters be.

Well my lunar lander has three rocket engine, one is at bottom and the others are at the left and right sides. So i will call newParticle when these engines are working and x,y and direction parameters will be determined according to engines’ positions.

In main.lua first i’ve required my Particles module to use it at the following lines.

require

After the maximumLeft variable i’ve added local tm variable to hold a timer later.

tm

I’ve added lines 72,73 and 78 to moveUp function. At line 72 i am calling a timer function. It’s listener function is createParticle, delay is 100 and iteration is 0. That means createParticle function will be called endlessly in every 100 milisecond if i dont cancel timer function. At line 73 i am adding a direction parameter to my timer to use it later. Since i am in the moveUp function that means my lunar lander will go upward, so my circles should move downwards. That’s why direction is “down” in this case. And finally at line 78, when touch event is ended i cancel the timer function to stop creating more circles.

moveUp

I’ve added same lines to moveRight and moveLeft functions but only changed the direction parameters to “left” and “right” instead of “down”

6

Lastly, let’s look at createParticle function which is being called in every 100 milisecond when upButton, leftButton or rightButton is touched.

create particle

In createParticle function first i am controlling the direction parameter. After i find the direction, i can be sure where to put my circles when they are created. I use this coordinates as x and y parameters of Particles.newParticle function. Remember this function is defined in the Particles module and it takes 3 parameters x,y and direction. Once the particle is created the other parts is done by the code written in the newParticle function.

And the result is

Sample gif

I wish i could help you a little. Please don’t hesitate to ask me question in your comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s