简体   繁体   English

电晕:在调用onComplete事件之前检查nil值

[英]Corona: Check nil value before call onComplete event

I need to check if "regalo" is nil or not, before call the onComplete, because sometimes is deleted by a Collision event and when I call onComplete=removeRegalo it return error nil value. 在调用onComplete之前,我需要检查“ regalo”是否为nil,因为有时会被Collision事件删除,并且当我调用onComplete = removeRegalo时,它将返回错误nil值。

error: Attempt to call method 'removeSelf' (a nil value) 错误:尝试调用方法“ removeSelf”(nil值)

Any idea? 任何想法?

    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
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

Would something like this work? 这样的事情行吗?

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

Hope it helps :) 希望能帮助到你 :)

Maybe this would do the job. 也许这可以完成工作。

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

Here is the solution: 解决方法如下:

       local function removeRegalo(event)

            display.remove( event )
            event = nil
        end

event = nil --This is optional as @111WARLOCK111 said. event = nil-如@ 111WARLOCK111所述,这是可选的。 Thanks! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM