【问题标题】:Corona/Lua: unable to access table value after collisionCorona/Lua:碰撞后无法访问表值
【发布时间】:2014-03-14 14:26:59
【问题描述】:

我正在使用 Corona 的物理引擎并遇到了问题。当发生冲突时,selfeventnil,即使它们有一个表值(我的理解是表可以保存除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。

【问题讨论】:

  • 你能显示你的 addEventListener 调用吗

标签: android ios lua coronasdk lua-table


【解决方案1】:

确保您使用 lua 中的 ':' 语法添加事件处理程序,因此它会发送 'self' 。像这样:

crate1:addEventListener( "collision", crate1 )

现在,您需要为 crate1 对象定义“碰撞”函数: 使用':'时隐含'self'

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 2015-02-09
    • 1970-01-01
    • 2014-06-22
    • 2014-07-07
    • 2014-03-13
    • 1970-01-01
    • 2017-08-17
    相关资源
    最近更新 更多