Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
上QQ阅读APP看书,第一时间看更新

Building the application

Now that we have configured our application to the landscape mode and set the display contents to scale on multiple devices, we're ready to start designing the game. Before we start writing some code for the game, we need to add in some art assets that will be displayed on the screen. You can find them in the Chapter 3 Resources folder. You can download the project files that accompany this book from the Packt Publishing website. The following are the files that you'll need to add to your Breakout project folder:

  • alertBox.png
  • bg.png
  • mmScreen.png
  • ball.png
  • paddle.png
  • brick.png
  • playbtn.png

Displaying groups

An important function that we'll be introducing in this game is display.newGroup(). Display groups allow you to add and remove child display objects and collect the related display objects. Initially, there are no children in a group. The local origin is at the parent object's origin; the anchor point is initialized to this local origin. You can easily organize your display objects in separate groups and refer to them by their group name. For example, in Breakout, we'll combine menu items such as the Title screen and Play button in a group called menuScreenGroup. Every time we access menuScreenGroup, any display object contained within the display group will be processed.

display.newGroup()

This function creates a group in which you can add and remove child display objects.

The syntax is display.newGroup().

For example:

local rect = display.newRect(0, 0, 150, 150)
rect:setFillColor(1, 1, 1)

local myGroup = display.newGroup()
myGroup:insert(rect)

Working with system functions

The system functions that we're going to introduce in this chapter will return information about the system (device information and current orientation) and control system functions (enabling multi-touch and controlling the idle time, accelerometer, and GPS). We'll be using the following system functions to return the environment information that our application will be running in and the response frequency for the accelerometer events.

system.getInfo()

This function returns information about the system on which the application is running.

The syntax is system.getInfo(param):

print(system.getInfo("name")) -- display the deviceID

Valid values for parameters are as follows:

  • "name": This returns the model name of the device. For example, on the iTouch, this would be the name of the phone as it appears in iTunes, such as "Pat's iTouch".
  • "model": This returns the device type. These include:
    • iPhone
    • iPad
    • iPhone Simulator
    • Nexus One
    • Droid
    • myTouch
    • Galaxy Tab
  • "deviceID": This returns a hash-encoded device ID of the device.
  • "environment": This returns the environment that the app is running on. These include:
    • "simulator": The Corona simulator
    • "device": iOS, Android device, and the Xcode simulator
  • "platformName": This returns the platform name (the OS name), which can be any one of the following:
    • Mac OS X (Corona simulator on Mac)
    • Win (Corona simulator on Windows)
    • iPhone OS (all iOS devices)
    • Android (all Android devices)
  • "platformVersion": This returns a string representation of the platform version.
  • "build": This returns the Corona build string.
  • "textureMemoryUsed": This returns the texture memory usage in bytes.
  • "maxTextureSize": This returns the maximum texture width or height supported by the device.
  • "architectureInfo": This returns a string that describes the underlying CPU architecture of the device you are running on.

system.setAccelerometerInterval()

This function sets the frequency of accelerometer events. On the iPhone, the minimum frequency is 10 Hz and the maximum is 100 Hz. Accelerometer events are a significant drain on battery, so only increase the frequency when you need faster responses, as in games. Always try to lower the frequency whenever possible to conserve battery life.

The syntax is system.setAccelerometerInterval( frequency ):

system.setAccelerometerInterval( 75 )

The function sets the sample interval in Hertz. Hertz is cycles per second, that is, the number of measurements to take per second. If you set the frequency to 75, then the system will take 75 measurements per second.

After you have the assets from the Chapter 3 Resources folder added in your project folder, we will begin writing some code!