This is a great question.
Corona has several ways to define functions. When you do:
local function myFunction()
You are creating a standalone function that can be called from the current module as long as you define it before you use it. For many functions that are local to that scene, this is the right way to define them.
However, Lua also supports “table functions”. These create an “Object Oriented” like interface for doing things. Consider you have an object named “player” and an object named “enemy”. Both need a “move” function. With Lua, you can create your player object:
local player = {}
and of course put in properties like numberOfLives, health, etc.
player.numberOfLives = 3 player.health = 10
You can also add functions to the table:
function player:fire( yourParameters ) function enemy:fire( yourParameters )
This way the object owns the function instead of the module owning it. There are two syntaxes you will run into:
function player.fire(self, yourParameters) function player:fire( yourParameters )
When you use a colon instead of a dot, you implicitly pass the instance of the object to the function. This is done assuming you will have multiple copies of the same object. You might only have one player, but you certainly might have multiple enemies and you want your function to work on the right one, so using the colon operator lets you automatically pass “self” to the function.
Corona’s Composer uses table functions because you could technically have multiple scenes in one Lua file (not recommended). This is why you see mixed ways of doing functions. The ones with “local” are not tied to specific objects and just do the work they are asked. The ones that are: function object:functionName() assume you want to work with that specific object.
Rob