尝试将显示对象添加到Composer场景组时,需要错误表。

我有一个 display 对象,它是在场景生命周期函数之外创建的,我想将它添加到 sceneGroup 中。

 local trans
    local function tra( )
      trans = display.newImage('assets/gray.png',indicesToOuterCordinate(layout.finalx,layout.finaly,layout.finalside,false))
      physics.addBody( trans, "static")
      trans:addEventListener( "collision", lis )
    end

但是当我尝试添加它时,我会得到一个“table expected”的错误。

sceneGroup:insert( trans )

即使在我尝试将 trans 显示对象添加到 sceneGroup 之前,我的函数 tra 也被调用。

点赞
用户2260604
用户2260604

仅为了快速汇总回复的答案。

通过将函数和声明移动到 function scene:create( event ) 中进行适当实例化,解决了最初的问题/疑问。

function scene:create( event )

    local sceneGroup = self.view

    local trans

    local function tra()
      trans = display.newImage('assets/gray.png',indicesToOuterCordinate(layout.finalx,layout.finaly,layout.finalside,false))
      physics.addBody( trans, "static")
      trans:addEventListener( "collision", lis )
    end

    tra()
    sceneGroup:insert( trans )

    -- More code here

    end

解决了这个问题,引发了一个关于应用程序中图像分层的新问题。

这通过将 displayObjects 添加到显示组中以及添加索引值来解决,索引了层在 Z 轴上的位置。

sceneGroup:insert(2, imageForBack)
sceneGroup:insert(1, imageForFront)
2014-07-26 18:44:05