Corona:在调用onComplete事件之前先检查nil值。

在调用 onComplete 前,我需要检查 "regalo" 是否为 nil,因为它有时会在碰撞事件中被删除,当我调用 onComplete=removeRegalo 时,它会返回一个空值错误。

错误:尝试调用方法“removeSelf”(一个空值)

有什么想法吗?

    local function removeRegalo(event)
        event:removeSelf()
        event = nil
    end
    local function tiraregalo()
        regalo = display.newImageRect("images/regalo.png", 30, 30)
        regalo.x = ship.x
        regalo.y = ship.y
        regalo:toFront()
        regalo.name = "regalo"
        physics.addBody( regalo, {isSensor = true } )
        grupoCasas:insert(regalo)
        local wind = 10
        transition.to(regalo,{time=1500, y = screenH + 30, x = regalo.x + wind,rotation= math.random(-20,60), onComplete=removeRegalo})
    end
function onCollision( event )
    if(event.object1.name == "casa" and event.object2.name == "regalo") then
        display.remove( event.object2 )
    end
end
点赞
用户3090678
用户3090678

希望这有所帮助 :)

如果 regalo ~= nil 那么
  -- 对象存在,所以执行一些代码
end
2013-12-16 12:55:04
用户3107759
用户3107759
本地函数 `removeRegalo(event)`,

检查 `event:removeSelf` 是否存在,

如果存在,则移除 `event` 并将其设为 `nil`。
2013-12-16 15:04:58
用户2679394
用户2679394
本地函数 `removeRegalo(event)`,用于移除事件。

local function removeRegalo(event) if event == nil then return end event:removeSelf() -- event = nil Not really needed, but okay if you want it here. end ```

2013-12-16 17:42:23
用户2291724
用户2291724

以下是解决方案:

local function removeRegalo(event)

     display.remove( event )
     event = nil
end

event = nil -- 如 @111WARLOCK111 所述,这是可选项。谢谢!

2013-12-16 22:10:33
用户1218135
用户1218135
本地函数`removeRegalo`用于移除对象:

```lua
local function removeRegalo(event)
    local tempObject = event.target
    if tempObject then
        tempObject:removeSelf()
        tempObject = nil
    end
end
2013-12-18 13:40:00