function scene:createScene(event) ... end
It is syntactic sugar for:
scene.createScene = function(self, event) ... end
What is the syntactic sugar for:
scene["createScene"] = function(self, event) ... end
You want to do:
local scene["createScene"] = function(self, event) ... end
It does not make sense.
Another way: local
is the qualifier for a variable that makes it local, not global. Which variable would you qualify with local function scene:createScene( event )
?
createScene
not a variable, it is the key in the scene
table.
Actually, this is a bit misleading. When you speak:
foo = 10
Without qualification, foo
becomes global, that is, it is stored in a global state as follows:
_G.foo = 10;
Which of course means the same:
_G["foo"] = 10;
When you use the local
keyword, it is not stored in the table, it ends up being stored in the VM register, which is faster and has a more rigid limited area.
When you write one of them:
function foo.bar() end function foo:bar() end
You explicitly store the value of the function in a table ( foo
). These statements are exactly equivalent to these:
foo["bar"] = function() end foo["bar"] = function(self) end
I note that listener functions must be made local
What do you mean by that? In Lua, a function is a function. It is just another value, like a string or a number. Whether it is stored in a global, table, local, or not stored does not matter at all.
local foo = print _G["zap"] = foo doh = zap t = { zip = doh } t.zip("Hello, World") -- Hello, World assert(print == foo and zap == foo and zap == doh and t.zip == doh)
Here we pass the print
function. This is still a function, and as long as we refer to it, we can call it.
I do not know Corona, but I assume that the event handlers are not defined by the local user naming convention. You need to register it as an event handler. For example, in accordance with this video, the Button object has an onEvent
field that is set for the handler of this button.