Here are some things I have learned over the last couple of years of using Corona.
- Use moses.lua. It’s like underscore.js, and will save you many for loops. It’s
just better lua. https://github.com/Yonaba/Moses
local \_ = require("moses")
- If you need consistent random numbers from a seed (I sometimes do), you need your own function.
Random numbers are not consistent across iOS and Android. Here’s one:
local function make\_lcg(seed) local m, a, c = 2^32, 1103515245, 12345 local function rand() seed = (a\*seed + c) % m; return seed/2^32 end return rand end local function fisher\_yates\_shuffle\_inplace(atable, seed) local rand, floor, j = make\_lcg(seed), math.floor, nil for i=#atable,2,-1 do j = 1+floor(rand()\*i) atable[i], atable[j] = atable[j], atable[i] end end
- Don’t bother making graphics in lots of sizes. I used to have @2x, @3x, @4x, etc.
and it was a pain to write scripts to create all the sizes.
If you are starting with a base 320x480 app, then just use two sizes, equivalent to
@2x and @4x. Use @4x graphics for any resolution >2x, and let Corona scale the rest down.
Downscaling usually looks fine, plus nobody has 320x480 resolution anyway.
(If you are starting with a higher base resolution then all the better – I’m just used
to using 320x480.)
During the development phase I just use the highest resolution version of every graphic.
- Save game settings etc. in JSON using the loadsave.lua module.
Don’t use SQL, it’s too much hassle.
local loadsave = require("loadsave.lua") local settings\_file = "settings.json" local settings = loadsave.loadTable(settings\_file) if settings == nil then settings = {username="default", volume=50, } loadsave.saveTable(settings, settings\_file) else username = settings['username'] volume = settings['volume'] end
- Be careful with canceling timers and transitions if you are planning to remove scenes
from memory. This can cause bugs that crop up infrequently and unpredictably…
- Use a lock function to keep the user from tapping buttons while your app is doing
important business. Here’s an example using moses:
-- lock("new round animation"); \<do something\> ;unlock("new round animation") -- Check if all\_unlocked() before responding to inputs. local locks = {} local function lock(k) locks[k] = true end local function unlock(k) locks[k] = false end local function all\_unlocked() return \_.count(locks, true)==0 end
-
Keep track of the memory your app is using to see if there are leaks:
local memtext = display.newText("", 250, 8, “Helvetica”, 15) local monitorMem = function() collectgarbage() local textMem = system.getInfo( “textureMemoryUsed” ) / 1048576 memtext.text = " mem: “…math.floor(collectgarbage(“count”))…” "…math.floor(textMem)…“M” end Runtime:addEventListener( “enterFrame”, monitorMem )
-
Place sprites onscreen relative to the center of the play area. This is not strictly necessary
but I find it helps with adapting to different resolutions and aspect ratios.
local CX, CY = display.contentCenterX, display.contentCenterY