Corona SDK: Creating a Scrolling Background

The Corona SDK makes game development for the iPhone, iPad, and Android easy. Corona uses the Lua programming language to create cross-platform apps. In this tutorial we will explore how to create a scrolling background with the Corona SDK.

Brief Overview

scrolling background Corona SDK

The Corona SDK makes it very easy to create dynamic effects with very few lines of code. Using the Corona SDK, we will create a scrolling 2D background with graphics that we create in Photoshop. This tutorial will not cover designing for a particular device. Instead, the scrolling background we create can be used for the iPhone, iPad, or Android platforms.

Create the Graphics

Using Photoshop, we are going to use the Custom Shape Tool to create stars for our background. While you can theoretically use any shape for the background this tutorial will demonstrate how to create a "starry night" background.

With Photoshop open, create a new document that is 45x45 pixels.

Select the Custom Shape Tool and select the 5 Point Star as your custom shape. If you cannot find the 5 Point Star, you may have to append the list with the Shapes list.

scrolling background Corona SDK

After you have selected the 5 Point Star, make sure your foreground color is white and draw a star on a new layer.

scrolling background Corona SDK

Exporting the Graphics

Now that we have drawn a star, use the Save for Web & Devices tool to save the star in 3 different sizes:

Star1.png 45x45 pixels
Star2.png 30x30 pixels
Star3.png 15x15 pixels

Code

With our graphics, we can start creating the scrolling background. Let’s get started by opening your preferred Lua editor and create a new document called main.lua.

Hide the Status Bar

Our first step is to hide the status bar. The status bar is a bar at the top of the screen that gives information to the user such as signal strength or battery level.

Variables

We will now set up some variables to be used throughout our program.

Set Up Star Table

After we have initialized some global variables, we are going to insert star objects into the starTable. In this table, we identify the image path of the stars and the movement speed of each star. Then we insert the star into starTable.

Movement Speed is calculated in milliseconds and will determine how fast it will take the star to move from the bottom of the screen to the top of the screen. The bigger stars will move slightly faster than the smaller stars to give the illusion of depth to the background.

Moving the Stars

The getRandomStar function returns a random star object from the starTable. Once the function gets the star, we set the image path, the name of the star and how fast the star will move. Here is what the complete function looks like.

Getting a Random Star

The first line gets a random star from the starTable and stores it in the local variable randomStar.

Star Image Path

After we retrieve a random star, we will set the image path of the star.

Star Name and Speed

Now, we will set the name of the star object to “star” and the movement speed will be set according to the speed of the random star that we pulled from the starTable.

Star Starting Point

The starting point of the star is going to be a random X position on the bottom of the screen. The star will also start off the screen for a smoother transition into the visible area of the screen. We also randomly rotate each star to add variance to the background.

Moving the Star

After the star has been set up, we can use the transition.to() method to move the star towards the top of the screen. The star object will then be removed from memory when it has reached its destination point.

Starting the Timers

This function sets up three timers that will call on the getRandomStar function to generate the star object and start the star moving towards the top of the screen.

The first parameter in timer.performWithDelay is the delay, which determines how many times the timer will be called. The second parameter is the function that will be called and the last parameter is the number of times the timer will be called. A value of “0” tells the timer to loop forever.

Start the App

Finally, we can run our app. The following code calls the functions that we have created throughout the tutorial to start the creating the stars, moving them towards the top of the screen and removing them when they have reached the sensor bar.

Conclusion

You can use a 2D scrolling background in a variety of games and utilizing a scrolling background is an easy way to make games more dynamic. Try swapping out the stars for clouds, leaves, or anything else that you can think of!

Thank you for reading!

Tags:

Comments

Related Articles