【问题标题】:Corona: Check nil value before call onComplete eventCorona:在调用 onComplete 事件之前检查 nil 值
【发布时间】:2013-12-18 21:40:00
【问题描述】:

在调用 onComplete 之前,我需要检查“regalo”是否为 nil,因为有时会被碰撞事件删除,当我调用 onComplete=removeRegalo 时,它会返回错误 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

【问题讨论】:

  • 这和iOS有什么关系?
  • 尝试在removeRegalo 中打印事件,onComplete 是否将事件作为参数?可以将event 作为外部变量输出removeRegalo()

标签: android ios lua coronasdk


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    这样的东西有用吗?

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

    希望对你有帮助:)

    【讨论】:

    • 没有帮助,这是消息错误:尝试调用方法'removeSelf'(一个零值)@apmartin1991
    【解决方案3】:

    也许这样就可以了。

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

    【讨论】:

      【解决方案4】:

      解决方法如下:

             local function removeRegalo(event)
      
                  display.remove( event )
                  event = nil
              end
      

      event = nil -- 正如@111WARLOCK111 所说,这是可选的。 谢谢!

      【讨论】:

        【解决方案5】:
           local function removeRegalo(event)
                 local tempObject=event.target
                 if tempObject  then         
                      tempObject:removeSelf()
                      tempObject = nil
                end
           end 
        

        【讨论】:

          猜你喜欢
          • 2013-09-12
          • 2012-09-07
          • 1970-01-01
          • 2013-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-12
          • 2015-05-09
          相关资源
          最近更新 更多