【问题标题】:Object in foreground when in a function在函数中时前景中的对象
【发布时间】:2013-10-06 21:50:31
【问题描述】:

各位,当我尝试在我的程序中插入一个元素时遇到了麻烦(使用 Corona SDK 制作,因此使用 LUA)。

问题是当我在函数中插入一个对象时,它会出现在前台,即使我在我的代码中声明另一个对象在函数中的对象之后

例如,如果我写

local function obD()

local obD = display.newRect(_W-30, _H/2+160, 10, math.random(-140, -20))
localGroup:insert(obD)
obD.isFixedRotation = true
obD:setFillColor(255, 0, 0)

end

tmrD = timer.performWithDelay(1500, obD, maxOb)


local myText = display.newText("Hello World", _W-30, 310, "PUSAB", 8)
localGroup:insert(myText)

应该在前台的对象是 myText,但 insted 显示为 obD,而如果我写

local obD = display.newRect(_W-30, _H/2+160, 10, math.random(-140, -20))
localGroup:insert(obD)
obD.isFixedRotation = true
obD:setFillColor(255, 0, 0)

local myText = display.newText("Hello World", _W-30, 310, "PUSAB", 8)
localGroup:insert(myText)

myText 按原样显示(出现在前台)

我可以做些什么来解决这个问题?谢谢! :)

【问题讨论】:

    标签: android iphone ios lua coronasdk


    【解决方案1】:

    您使用performWithDelay,它会延迟函数的执行。这会导致 localGroup:insert(obD) 在执行 localGroup:insert(myText) 之后执行,这会将其置于前台。

    您可以将第一个插入更改为localGroup:insert(1, obD) 以“强制”其索引并将其置于后台。详情请见GroupObject

    【讨论】:

      【解决方案2】:

      您可以使用(如 Paul 建议的那样):

      localGroup:insert(1, obD) -- This will make `obD` z-index to 1
      

      或:

      myText:toFront()  -- This will force the index of `myText` to the highest value/force forward
      

      注意:

      • 仅在创建 obD(根据您的代码)之后调用这些方法中的任何一个。
      • 使用第二种方法时,请确保在全局首选项中声明myText。 (即,您必须在场景顶部将 myText 声明为 local myText)。

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

      【讨论】:

      • @luaLover:如果你已经成功解决了你的问题,那么将任何答案标记为已接受...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多