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

Optimize your display images

It's important to optimize your image file size as much as you can. Using full-screen images can impact the performance of your application. They require more time to load on a device and consume a lot of texture memory. When a lot of memory is consumed in an application, in most cases it'll be forced to quit.

The iOS devices vary in the size of their available memory, depending on which one you have out of the following:

  • iPhone 3GS, iPad, and iTouch 3G/4G of 256 MB RAM
  • iPhone 4/4S, iPad 2, iPad Mini, and iTouch 5G of 512 MB RAM
  • iPhone 5/5S/6, 6 Plus, iPad 3G, and iPad 4G of 1 GB RAM

For example, texture memory on the iPhone 3GS should be kept under 25 MB before performance issues start occurring by slowing down your app or even forcing it to quit. An iPad 2 would have no problem going farther down that boundary since it has more memory available.

Note

Refer to http://docs.coronalabs.com/api/event/memoryWarning/index.html to apply memory warnings for iOS devices.

For Android devices, there is around a 24 MB memory limit. So, it's important to be aware of how many display objects you have in your scene and how to manage them when they are not needed in your app any more.

In cases when you no longer need an image to be displayed on screen, use the following code:

image.parent:remove( image ) -- remove image from hierarchy

Alternatively, you can also use this line of code:

image:removeSelf( ) -- same as above

If you want to remove an image from the scene completely throughout the lifetime of your app, include the following line after your image.parent:remove( image ) or image:removeSelf() code:

image = nil

Keeping memory usage low within your application will prevent crashes and improve performance. For more information on optimization, go to http://developer.coronalabs.com/content/performance-and-optimization.

Pop quiz – basics of Lua

Q1. Which of the following are values?

  1. Numbers
  2. nil
  3. Strings
  4. All of the above

Q2. Which relational operator is false?

  1. print(0 == 0)
  2. print(3 >= 2)
  3. print(2 ~= 2)
  4. print(0 ~= 2)

Q3. What is the correct way to scale an object in the x direction?

  1. object.scaleX
  2. object.xscale
  3. object.Xscale
  4. object.xScale