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

Runtime configuration

All project files not only contain a main.lua file, but other .lua and related assets as needed for your project. Some Corona projects are configured using a config.lua file that is compiled into your project and accessed at runtime. This allows you to specify dynamic content scaling, dynamic content alignment, dynamic image resolution, frame rate control, and antialiasing, all at the same time, so that the output on every type of device is displayed similarly.

Dynamic content scaling

Corona allows you to specify the screen size you plan to aim your content for. This is done using a file called config.lua. You'll be able to scale the assets for your app to run on a device whose screen size is smaller or bigger.

The following values should be used to scale content:

  • width (number): This is the screen resolution width of the original target device (in portrait orientation)
  • height (number): This is the screen resolution height of the original target device (in portrait orientation).
  • scale (string): This is a type of autoscaling from the following values:
    • letterbox: This scales up content uniformly as much as possible
    • zoomEven: This scales up content to uniformly to fill the screen, while keeping the aspect ratio
    • zoomStretch: This scales up content nonuniformly to fill the screen and will stretch it vertically or horizontally

    Note

    The zoomStretch value works well with Android device scaling, since many of them have different screen resolutions.

Dynamic content alignment

Content that is dynamically scaled is already centered by default. You may find cases where you don't want the content to be centered. Devices such as the iPhone 3G and the Droid have completely different screen resolutions. In order for the content displayed on the Droid to be similar to iPhone 3G, the alignment needs to be adjusted so that the content fills the entire screen without leaving any empty black screen space. The alignment is as follows:

  • xAlign: This is a string that specifies the alignment in the x direction. The following values can be used:
    • left
    • center (default)
    • right
  • yAlign: This is a string that specifies the alignment in the y direction. The following values can be used:
    • top
    • center (default)
    • bottom

Dynamic image resolution

Corona allows you to swap in higher resolution versions of your images to higher resolution devices, without having to change your layout code. This is a case to consider if building for multiple devices with different screen resolutions.

An example where you want to display hi-res images is on an iPhone 4 where the resolution is 640 x 960 pixels. It is double the resolution of the earlier iOS devices, such as iPhone 3GS, which is 320 x 480 pixels. Scaling up the content from the iPhone 3GS to fit the iPhone 4 screen works, but the images will not be as crisp and will look a little fuzzy on the device.

Images of higher resolution can be swapped in for the iPhone 4 by adding a @2x suffix to the end of the filename (but before the period and file extension). For example, if your image filename is myImage.png, then your higher resolution filename should be myImage@2x.png.

In your config.lua file, a table named imageSuffix needs to be added for the image naming convention and image resolutions to take effect. The config.lua file resides in your project folder where all your other .lua files and image files are stored. Look at the following example:

application =
{
  content =
  {
    width = 320,
    height = 480,
    scale = "letterbox",

    imageSuffix =
    {
       ["@2x"] = 2,
    },
  },
}

When calling your display objects, use display.newImageRect( [parentGroup,] filename [, baseDirectory] w, h) instead of display.newImage(). The target height and width need to be set to the dimensions of your base image.

Frame rate control

The frame rate is 30 fps (frames per second) by default. Fps refers to the speed at which the image is refreshed in games. Thirty fps is standard in mobile games, especially for older devices. You can set it to 60 fps when you add in the fps key. Using 60 fps makes your app run smoother. You can easily detect a life-like fluidity in the motion when it comes to running animations or collision detections.