2014-01-15

In this tutorial, I’ll be showing you how to create an obstacles avoiding game using the Corona SDK. You’ll learn more about touch controls, collision detection, and physics. The objective of the game is to avoid as many obstacles as you can to obtain the highest score. Let’s get started.

1. Application Overview

Using ready-made graphics, we’ll create an entertaining game using the Lua programming language and the Corona SDK APIs. The player will be able to move a helicopter on the screen and needs to avoid any obstacles it comes across. You can modify the parameters in the code to customize the game.

2. Target Device

The first thing we have to do is select the platform we want to run our application on so we’re able to choose the size for the images we’ll use.

The iOS platform has the following requirements:

iPad 1/2/Mini: 1024px x 768px, 132 ppi

iPad Retina: 2048px x 1536px, 264 ppi

iPhone/iPod Touch: 320px x 480px, 163 ppi

iPhone/iPod Retina: 960px x 640px, 326 ppi

iPhone 5/iPod Touch: 1136px x 640px, 326 ppi

Because Android is an open platform, there are many different devices and resolutions. A few of the more common screen characteristics are:

Asus Nexus 7 Tablet: 800px x 1280px, 216 ppi

Motorola Droid X: 854px x 480px, 228 ppi

Samsung Galaxy SIII: 720px x 1280px, 306 ppi

In this tutorial, we’ll be focusing on the iOS platform in terms of graphics. In particular, we’ll be developing for the iPhone and iPod Touch. However, the code of this tutorial can also be used in you target the Android platform.

3. Interface

We’ll use a simple user interface involving multiple shapes, buttons, bitmaps, and more. The graphics that we’ll use for this tutorial can be found in the project included with this tutorial.

4. Export Graphics

Depending on the device you’ve selected, you may need to convert the graphics to the recommended resolution (ppi), which you can do in your favorite image editor. I used the Adjust Size… option in the Tools menu in the Preview application on OS X. Remember to give the images a descriptive name and save them in your project folder.

5. Application Configuration

We’ll use a configuration file, config.lua, to make the application go full screen across devices. The configuration file shows the original screen size and the method used to scale the content in case the application is run on a different resolution.

6. main.lua

Let’s write the actual application. Open your preferred Lua editor. Any plain text editor will work, but it is recommended to use a text editor that has syntax highlighting. Create a new file and save it as main.lua in your project folder.

7. Code Structure

We’ll structure our code as if it were a class. If you’re familiar with ActionScript or Java, you should find the project structure familiar.

8. Hide Status Bar

This code snippet hides the status bar. The status bar is the bar at the top of the device’s screen that shows the time, signal, and other indicators.

9. Import Physics

We’ll use the physics library to handle collisions. Import the library using the code snippet shown below.

10. Background

A simple background for the application’s user interface. The code snippet below draws the background to the screen.

11. Title View

This is the title view. It’s the first interactive screen to appear in our game. These variables store its components.

12. Credits View

The credits view shows the credits and copyright of the application. This variable is used to store it.

13. Instructions Message

A message with instructions will appear at the start of the game and it will disappear after the first tap.

14. Helicopter

This is the helicopter graphic. The player will control the helicopter using touch controls.

15. Blocks

The blocks are the obstacles the player needs to avoid.

16. Alert

The alert is displayed when the player misses the ball and the game is over. It displays a message and ends the game.

17. Sounds

To liven up the game, we’ll use sound effects. The sounds used in the game were obtained from as3soundfxr. I found the background music on playonloop.

18. Variables

The following code snippet shows the variables that we’ll use. Read the comments to understand what each variable is used for.

19. Declare Functions

Declare all functions as local at the start.

20. Constructor

Next, we create the function that will initialize the game logic.

21. Add Title View

We start by placing the title view in the stage and call a function that will add tap listeners to the buttons.

22. Start Button Listeners

The following function adds the necessary listeners to the TitleView‘s buttons.

23. Show Credits

The credits screen is shown when the user taps the about button. A tap listener is added to the credits view to dismiss it when the user taps it.

24. Hide Credits

When the user taps the credits view, it is animated out of the stage and remove.

25. Show Game View

When the play button is tapped, the title view is animated off the screen and the game is revealed. There are a number of moving parts so we’ll take a closer look at each of them.

26. Instructions Message

The following code snippet adds the instructions message.

27. Score TextField

The following code snippet creates a text field that shows the player’s current score in the top-right of the stage.

28. Helicopter

Next, it is time to add the helicopter graphic to the stage as shown below.

29. Walls

In the following code snippet, we use the Corona graphics API to create several lines that we’ll add to the physics simulation a bit later in this tutorial. They will be used to detect collisions with the helicopter.

30. Physics

Next, we need to add the necessary physics to each object.

31. Start Game

We create a group for the blocks, invoke the gameListeners function, and start the background music.

32. Game Listeners

The following code snippet may seem complex, but it simply adds a few listeners to start the game logic.

33. Create a Block

As its name indicates, the createBlock function creates a block and draws it to the screen. The resulting object is added to the physics engine to check for collisions.

34. Move Function

In the movePlayer function, we update the up variable. As long as its value is equal to true, the update function moves the helicopter up.

35. Increase Speed

To make the game more interesting, a timer increases the speed every five seconds. An icon is displayed to alert the player of change in speed.

36. Move Helicopter

The update function checks the value of up and moves the helicopter up if it is equal to true.

37. Move Blocks

Next, it is time to move the blocks. We use the speed variable to determine how many pixels the blocks should be moved every frame.

38. Update Score

The algorithm to update the score is simple. The score is incremented by one every frame for as long as the helicopter hasn’t crashed. Simple. Right?

39. Collisions

The onCollision function verifies if the helicopter has collided with a block. If a collision was detected, we play a sound effect, an explosion, remove the graphics, and show an alert to the player.

40. Alert

The alert function creates an alert view and shows it to the user. The game then ends.

41. Invoke Main Function

In order to start the game, the Main function needs to be invoked. With the rest of the code in place, we do that here.

42. Loading Screen

On the iOS platform, the file named Default.png is displayed while the application is launching. Add this image to your project’s source folder, it will be automatically added by the Corona compiler.

43. Icon

Using the graphics you created earlier, you can now create a nice icon. The dimensions of the icon size for a non-retina iPhone are 57px x 57px, while the retina version needs to be 114px x 114px. The artwork for iTunes is required to be 1024px x 1024px. I suggest creating the iTunes artwork first and then creating the smaller sized images by scaling the iTunes artwork down to the correct dimensions. There is no need to make the application icon glossy or add rounded corners as this is taken care of by the operating system for you.

44. Testing in Simulator

It’s time to test our application in the simulator. Open the Corona Simulator, browse to your project folder, and click Open. If everything works as expected, you’re ready for the final step.

45. Build Project

In the Corona Simulator, go to File > Build and select the target device. Fill out the required fields and click Build. Wait a few seconds and your application is ready to test on a device and/or to be submitted for distribution.

Conclusion

In this tutorial, we’ve learned about touch listeners, collision detection, physics as well as a few other skills that can be useful in a wide number of games. Experiment with the final result and try to modify the game to create your own version of the game. I hope you liked this tutorial and found it helpful. Thank you for reading.

Show more