如何在Corona中允许使用本地事件监听方法

如何将“touch”方法变为局部方法,以便无法从类外部调用它而不使编译器抱怨?我原先想做的是,将不同的对象(称之为“buttonManager”)传递到类事件监听器方法中,因为我需要在那里访问buttonManager。 我该怎么让buttonManager到达事件监听器?

原文链接 https://stackoverflow.com/questions/7404310

点赞
stackoverflow用户850721
stackoverflow用户850721

听起来你可以为你正在设计的类创建一个单独的模块,然后将触摸处理程序作为该模块的本地函数。查看Corona的ui.lua文件(包含在许多样例项目中)以查看他们是如何实现的。

module(..., package.seeall)

local function newButtonHandler( self, event )
    . . .
end

function newButton( params )

    . . .

    button.touch = newButtonHandler
    button:addEventListener( "touch", button )

    . . .

    return button
end

这是从他们的代码简化而来的。如你所见,newButtonHandler是本地函数,因此外界无法调用它。

希望能对你有所帮助!

2011-09-13 16:57:51