Advertisement
  1. Code
  2. Mobile Development
Code

Create a Plane Fighting Game in Corona: Gameplay

by
Difficulty:IntermediateLength:MediumLanguages:
This post is part of a series called Create a Plane Fighting Game in Corona.
Create a Plane Fighting Game in Corona: Interface
Create a Plane Fighting Game in Corona: More Gameplay
Final product image
What You'll Be Creating

Introduction

In the first part of this series, we managed to get the start screen showing and were able to transition to the gamelevel screen. In this tutorial, we continue where we left of and start implementing the gameplay.

1. Local Variables

Open gamelevel.lua, the file we created in the first tutorial, and add the following below the line local scene = storyboard.newScene().

Most of these are self-explanatory, but I've included comments for clarification. From here on out, all code should be inserted above the line return scene.

2. createScene

Start by adding the createScene function to main.lua. The createScene function is called when the scene's view doesn't yet exist. We'll add the game's display objects in this function.

3. setupBackground

In setupBackground, we create a blue background using the Display object's newRect method. The setFillColor method takes RGB values, as percentages. Invoke the setupBackground function in createScene as shown below.

4. setupGroups

The setupGroups function instantiates the islandGroup and planeGroup groups, and inserts them into the scene's view. The GroupObject is a special type of display object into which you can add other display objects. It's important to first add the islandGroup to the view to make sure the islands are below the planes.

Invoke the setupGroups function in createScene as shown below.

5. setupDisplay

The setupDisplay function draws a black rectangle at the bottom of the screen and inserts dpad and plane images into the view.

Again, invoke this function in createScene as shown below.

6. setupPlayer

The setupPlayer function simply adds the player image to the screen. The Display object comes with two read-only properties, contentWidth and contentHeight, representing the original width and height of the content in pixels. These values default to the screen width and height, but may have other values if you're using content scaling in config.lua. We use these properties to align the player in the  scene.

Invoke the setupPlayer function in createScene.

7. setupLivesImages

The setupLivesImages function sets up six life images and positions them at the top left of the screen. We then insert these images into the livesImages table, so that we're able to reference them later. Lastly, we make sure that only the first three images are visible.

The setupLivesImages function is also invoked in the createScene function.

8. setupDPad

The setupDPad function sets up the four rectangles rectUp, rectDown, rectLeft, and rectRight. We carefully position them on top of the dpad image, configure them to not be visible, and make sure the isHitTestable property is set to true.

If you set display objects to not be visible, you're initially unable to interact with them. However, by setting the isHitTestable property to true, this behavior is overridden.

You've guessed it. This function is also invoked in createScene.

9. resetPlaneGrid

The resetPlaneGrid function resets the planeGrid table and inserts eleven zeros. The planeGrid table imitates eleven spots across the x axis, in which an enemy plane can be positioned. This will make more sense once we start generating enemy planes.

Invoke this function in createScene.

10. enterScene

Now that all the display objects are in place, it's time to add event listeners, timers, etc. If you recall from the previous part of this tutorial, the enterScene function is a good place to set these up. Start by inserting the following code snippet.

11. Removing the Previous Storyboard

When we enter this scene, we need to remove the previous scene. Add the following code to the enterScene function to do this.

When you enter a new scene, the previous scene you were on can be referenced by calling getPrevious on the storyboard object. We remove it completely from the storyboard by calling removeScene on the storyboard object.

12. Add Event Listeners to Dpad Rectangles

Add the following code below the code you entered in the previous step. This code snippet adds touch listeners to each of the rectangles, invoking movePlane with every touch. Let's take a look at this movePlane function in the next step.

13. movePlane

The movePlane function is responsible for setting the planes speed. We check if the touch event's phase is equal to began, which means the player has touched down but not lifted their finger back up. If this is true, we set the speed and direction according to which rectangle was touched. If the touch event's phase is equal to ended, then we know the player has lifted their finger, which means we set the speed to 0.

14. PlaneSound

Let's add some sound to our game. Add the following code snippet to the enterScene function. It loads and plays planesound.mp3. By setting the loops property to -1, the sound will loop forever. If you want to learn more about audio in Corona, be sure to check out the documentation.

15. enterFrame Event

We also add a runtime event listener named enterFrame that will call the gameLoop function. The frequency with which the enterFrame event occurs depends on the frames per second (FPS) value you set in config.lua. In our example, it will be called 30 times per second. Add this event listener in the enterScene function.

16. gameLoop

In the gameLoop function we update the sprite positions and perform any other logic that needs to take place every frame. If you are interested in reading more on the topic of game loops, Michael James Williams wrote a great article that explains how a common game loop works. Add the following code snippet.


17. movePlayer

The movePlayer function manages the moving of the player's plane. We move the plane according to the playerSpeedX and playerSpeedY values, which will either be 7 or 0, depending on whether the player is touching on the DPad or not. Refer back to the movePlane function if this is unclear. We also do some bounds checking, making sure the plane cannot move off-screen.

If you test the game now, you should be able to navigate the plane around the screen using the DPad.

Conclusion

This brings the second tutorial of this series to a close. In the next installment of this series, we will continue with the gameplay. Thanks for reading and see you there.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.