为什么这个Lua函数不能使用自己的":"标记为"local"而不会得到:
'('预计在'附近':'
也就是说,在下面的代码中工作.但是为什么我不能将"scene:createScene"函数设置为本地(因为我在尝试时得到了上述错误).
我注意到监听器功能需要在本地进行,我在故事板中有时会遇到跨场景问题.这些可以标记为本地和工作正常.
SceneBase = {}
function SceneBase:new()
local scene = Storyboard.newScene()
local function returnButtonTouch_Listener (event)
-- code here
end
function scene:createScene( event ) -- WHY CAN'T THIS BE LOCAL???
-- code here
end
return scene
end
return SceneBase
Run Code Online (Sandbox Code Playgroud)
这就是为什么函数行不能读取:
local function scene:createScene( event )
Run Code Online (Sandbox Code Playgroud)
Mud*_*Mud 14
function scene:createScene(event) ... end
Run Code Online (Sandbox Code Playgroud)
是语法糖:
scene.createScene = function(self, event) ... end
Run Code Online (Sandbox Code Playgroud)
这是语法糖:
scene["createScene"] = function(self, event) ... end
Run Code Online (Sandbox Code Playgroud)
你想做:
local scene["createScene"] = function(self, event) ... end
Run Code Online (Sandbox Code Playgroud)
这毫无意义.
放置它的另一种方式:local
是变量的限定符,它使其成为局部而非全局.你有资格获得什么变数local function scene:createScene( event )
?
createScene
它不是一个变量,它是表中的一个关键scene
.
实际上,这有点误导.当你说:
foo = 10
Run Code Online (Sandbox Code Playgroud)
没有资格,foo
变得全球化,也就是说它存储在这样的全球状态:
_G.foo = 10;
Run Code Online (Sandbox Code Playgroud)
这当然意味着与此相同:
_G["foo"] = 10;
Run Code Online (Sandbox Code Playgroud)
当您使用关键字时local
,它不会存储在表中,而是最终存储在VM寄存器中,这会更快并且范围更加严格.
当你写下这些中的任何一个时:
function foo.bar() end
function foo:bar() end
Run Code Online (Sandbox Code Playgroud)
您将函数值显式存储在表(foo
)中.这些陈述分别与这些陈述完全相同:
foo["bar"] = function() end
foo["bar"] = function(self) end
Run Code Online (Sandbox Code Playgroud)
我注意到监听器功能需要在本地进行
你是什么意思?在Lua中,函数是函数是函数.它只是另一个值,如字符串或数字.它是存储在全局,表,本地还是根本不存储是无关紧要的.
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)
Run Code Online (Sandbox Code Playgroud)
在这里我们传递print
函数.它具有相同的功能,只要我们引用它,我们就可以调用它.
我不知道Corona,但我猜测事件处理程序没有通过命名本地化的约定来指定.您需要将其注册为事件处理程序.例如,根据此视频,Button对象具有一个onEvent
字段,该字段设置为该按钮的处理程序.