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

问题描述:

我需要在调用onComplete之前检查“regalo”是否为零,因为有时会被碰撞事件删除,当我调用onComplete = removeRegalo时,它会返回错误nil值。Corona:在调用onComplete事件前检查nil值

错误:尝试调用方法 '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 
+0

这是如何涉及'iOS'? –

+0

在'removeRegalo'尝试打印事件,onComplete是否将事件作为参数?可以将'event'作为外部变量'removeRegalo()'。 – Tim

会是这样的工作?

if regalo ~= nil then 
    -- object exists so do some code 
end 

希望它能帮助:)

+0

没有帮助,下面是消息错误:尝试调用方法'removeSelf'(一个零值) @ apmartin1991 – benLIVE

也许这将做的工作。

local function removeRegalo(event) 
    -- Check if event:removeSelf exists 
    if event:removeSelf then 
     event:removeSelf() 
     event = nil 
    end 
end 

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 

下面是解:

 local function removeRegalo(event) 

      display.remove(event) 
      event = nil 
     end 

事件=零 - 这是可选的,因为@ 111WARLOCK111说。 谢谢!

local function removeRegalo(event) 
     local tempObject=event.target 
     if tempObject then   
       tempObject:removeSelf() 
       tempObject = nil 
     end 
    end