Corona SDK – random things I’ve learned

I’ve been using Corona SDK since October 2011 for experimenting and developing for mobile devices(primarily iOS) and it has re-kindled my love for software development.  Finally I have something that removes the burden of developing a game engine with sprites, sound, physics, collision detection and allows me to get my ideas onto the screen in as little time as possible.   I’m also quite fond of Lua the language that you use to program apps in Corona. Lua is very flexible when developing classes and allows you to structure your code in a very loose manner almost making C/C++ seem too rigid.  The ability to do nested functions, un-fixed dynamic variables, and define your tables (classes) as their created instead of cast in stone has been a very freeing experience.   There are some drawback, no visual debugging (only print statements), no access to the screen buffer or bitmap buffers, no 3D,  no access to native code (but plenty of native features), but overall it’s a fairly descent trade-off when you just want to do a simple 2D game.

A couple of random things I’ve learned developing using Corona that may not be obvious to a new developer that I’d like to share

 

- Divide by 0 may not report an error in your console, but will cause inexplicable problems in your app to show up

Always make sure you never divide a number by 0, the simulator may not catch it and you won’t know what’s going on in your app. I had objects disappear without notice while the app was still running but couldn’t figure out why, until I found out I had a piece of code that would occasionally divide by 0.  It’s not uncommon for the app to continue working when there’s errors happening and worse when it’s not reporting to the console.

 

- Constantly test your app in  your simulator with different device profiles, iPhone (3G/3GS), iPad,  Android (if your supporting it)

You will find a variety of bugs and inconsistencies just on the simulator regarding to screen ratio scaling on the device with regards to UI placement as well as automatic re-sizing of textures that exceed 512×512 to the maximum supported on that device (in other words that 1024×1024 image you were using in your game just got shrunk when you switched to iphone 3G/ipod touch 3rd gen mode)

 

- Printing from your iOS device

It’s essential to know what’s going on in your app, especially while testing device only features like the accelerometer,maps,camera,photos and gamecenter. So you need  a working print statement from your tethered device!  To print ‘print function’ statements to the xcode organizer console from your iOS device attached to your mac follow these steps:

  1. in main.lua add:  io.output():setvbuf(“no”)  and rebuild your  Corona app
  2. on your iOS device go to settings->developer and enable logging
  3. open xcode -> window ->organizer and find your device and click on the console, you should now be able to see print statements coming from your app

 

- Physics joints are too elastic when used with a touch joints to move them (Update: this is fixed)

Create a ragdoll and notice the limbs are separating if your moving it around with a touch joint? Well the the touch joint has a magic number of mass and this causes the physics simulation to be unbalanced causing tears between the physics bodies. Corona Labs is working on this, but basically creating physics objects in Corona is a balancing act when joints are concerned and your animating them like I do.  To avoid this problem you can either wait, or put bounds on how far your moving your touch joints

Update: although moving an object without applying physics will tear joints (to be expected), physics joints will no longer separate using touch joints if you set physics.setContinuous( false )

Elastic physics joints

 

-Audio Streams need to be taken care of when the app suspends and resumes

Audio streams (your background music) is constantly streaming from a file and will stop playing when you suspend/resume your app on the device but will play normally in the corona simulator if you do the same thing.  To take care of this just stop, destroy and reload the audio stream on suspend/resume events

 

- Corona is constantly changing, be aware of the changes that go on weekly that may effect your project

Corona is constantly changing, these guys are constantly adding new features, fixing bugs, refactoring their code, simplifying and deprecating methods and classes in their API and occasionally something breaks, so you have to be aware of the progress that is occurring and not freak out when suddenly your sprites aren’t showing up with the latest build.