Corona/Lua:碰撞后无法访问表格值。

我正在使用Corona的物理引擎,遇到了一个问题。当发生碰撞时,'self'和'event'都是nil,尽管它们有一个表值(我的理解是表可以保留除“nil”之外的任何值)。

function onCollision(self,event)
    print(event); -- output -> table: 097EF680
    print(self); -- output -> table: 098349D0
    if (event.phase == "began") then
        print(self.myName .. ": collision began with " .. event.other.myName)
    end
end

if语句应该打印哪两个对象正在发生碰撞,但我收到了错误消息

...\main.lua:270: attempt to concatenate field 'myName' (a nil value)

这就是我添加打印声明的原因。注意到发生了碰撞,因为此错误只发生于碰撞时,并且这两个值应该存在,因为它们有一个ID。但是,试图访问它们(通过event.myName,event [1])返回一个“nil”值。

' myName '当然是为发生碰撞的两个对象定义的,而且不是“nil”。

点赞
用户1078537
用户1078537

请确保在Lua中使用“:”语法添加事件处理程序,以便发送“self”。像这样:

crate1:addEventListener( "collision", crate1 )

现在,您需要为crate1对象定义“collision”函数: 在使用“:”时,“self”是隐含的

function crate1:collision(event)
   ...
end`

如果您需要更多示例,请查看Corona文档: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

2014-02-17 18:34:18