【问题标题】:Previous scene doesn't get removed之前的场景不会被移除
【发布时间】:2013-12-14 18:17:32
【问题描述】:

我对电晕很陌生,我似乎无法找到解决问题的方法:

我的游戏中有关卡并且我正在使用情节提要,当玩家单击下一个关卡按钮时,他们会被发送到 level2 场景,场景以 storyboard.removeALL() 开头,但这不会删除前一个场景和我的背景搞砸了。

function nextlevel(event)
    storyboard.gotoScene( "level2" )
end

function win ()
  nextlevelbutton = display.newRoundedRect( display.contentCenterX, 285, 120, 30, 3 )
  nextlevelbutton:setFillColor( 61/255,61/255,61/255 )
  nextleveltxt = display.newText( { text = "Next Level", font = native.systemFontBold,    fontsize = 20, x= display.contentCenterX, y = 285 } )
  nextlevelbutton:addEventListener( "tap", nextlevel )

  end

这一切都在场景中:enterScene 函数。

这是level2场景的开始:

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

storyboard.removeAll( )

我尝试将 nextlevel 事件放在 win 函数中,但没有任何反应。

谢谢

【问题讨论】:

    标签: android ios lua storyboard coronasdk


    【解决方案1】:

    这应该对你有帮助:

    http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/

    如果您加载一个菜单并且您想从所有其他类中删除所有内容,请使用:

    storyboard.purgeAll() 
    

    这应该会停止所有的监听器并移除所有的对象。

    您可以做的另一件事是添加以下代码:

    function scene:didExitScene( event )
    storyboard.purgeScene( "sceneyouareleaving" )
    end
    scene:addEventListener( "didExitScene" )
    

    你需要做的就是调用那个函数然后你就完成了:)

    希望这会有所帮助!

    【讨论】:

    • 在 "storyboard.gotoScene( "level2" )" 之前尝试 "storyboard.purgeScene( "sceneyouareleaving" )"
    • 嗯,这也不起作用,在一个函数中我首先说:storyboard.purgescene 并且有一个计时器延迟,我使用 storyboard.gotoScene 但它给了我一个错误:尝试索引字段内容边界零值
    • 我已经重写了我的代码,特别注意所有的东西,比如将对象插入到组中,但现在我得到了这个错误:尝试连接全局'sceneName'(一个 nil 值)
    • 我猜你使用 displayGroup:insert 来插入你的对象?如果是这样,在你离开现场之前,使用代码“display.remove(displayGroup)”应该会有所帮助。
    • 至于您的新问题,请确保您在场景中拥有所有这些:“ local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function scene:createScene( event )结束函数场景:进入场景(事件) 结束函数场景:退出场景(事件) 结束函数场景:destroyScene(事件) 结束场景:addEventListener(“createScene”,场景)场景:addEventListener(“enterScene”,场景)场景:addEventListener(“ exitScene", 场景) 场景:addEventListener("destroyScene", 场景) 返回场景"
    【解决方案2】:

    此外,正如您的“混乱”背景所暗示的那样,请记住将您创建的所有对象添加到视图组:

    function scene:createScene(event)
        local group = self.view
        local params = event.params
    

    然后当你创建对象时,例如:

    background = display.newImageRect("gfx/bg.png", 1425, 900)
    background:setReferencePoint(display.TopLeftReferencePoint)
    background.x = 0
    background.y = 0
    background:setFillColor(0, 255, 255)
    

    记得把它添加到组中:

    group:insert(background)
    

    【讨论】:

    • 在我的 createscene 函数中,我创建了所有显示对象,最后我将它们全部放入组中。之后,我启动我的 enterscene 函数并让一些东西移动等。在 enterscene 中的一个函数中,我放置了:storyboard.gotoScene("level2")。然后这把我带到了我的第二个场景。我还在开头放了:storyboard.removeAll()。但是第一个场景中的物体似乎并没有被移除并留在第二个场景中。
    【解决方案3】:

    我设法解决了它。我认为我在功能和组的位置上做错了。无论如何感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多