【问题标题】:How to check for a nil value of an object before execution of code in corona sdk at enter frame event如何在进入帧事件时在 corona sdk 中执行代码之前检查对象的 nil 值
【发布时间】:2013-09-12 02:34:12
【问题描述】:

我想从年龄增加到 20 秒的组中删除一个对象,对象是在开始游戏后 20 秒后创建的,所以它在游戏循环中显示 nil 值错误,我想知道如何要检查 nil 对象,因为这里游戏循环是在对象创建之前执行的,请尝试解决问题。谢谢...

下面是代码:

    function createobject(event)
        local object=display.newImage("obj1.png")
        object.x=500
        object.y=600
        object.GeneratedTime=event.time/1000
        group:insert( object )
    end

    function showobj(event)
        createobject(event)
    end
        timer.performWithDelay(8000, showobj, 0)

        function gameloop(event)
             if group~=nil then
                for i=1,group.numChildren do
                         local child = group[i]
                         local age= event.time/100-child[i].GeneratedTime
                         if age>20 then
                         child.parent:remove( child[i] )
                         end
                end
             end
        end

        Runtime:addEventListener( "enterFrame", gameloop )

【问题讨论】:

  • 您的描述不清楚。尝试更具体并发布您收到的错误消息。请修正代码的缩进以匹配定义的嵌套级别。

标签: android iphone lua coronasdk


【解决方案1】:

试试这个:

if(child[i]~=nil)then    -- You can check for the existence of child as this --
  local age= event.time/100-child[i].GeneratedTime
  if age>20 then
    child.parent:remove( child[i] )
  end
end

继续编码...... :)

【讨论】:

    【解决方案2】:

    我还没有使用过电晕,但是如果你只是要检查对象是否存在,我可以看到你在 show obj 函数的 arg 1 上得到了事件,你说它只是在计时器后给出 nil已通过。因此,检查您的对象是否为 nil 的最佳方法如下:

    function showobj(event)
       if event == nil then return end
       createobject(event)
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多