scene:show第二次去场景后跑两遍

scene:show runs twice after going to scene the second time

我使用 gotoscene 从 menu.lua 到 game.lua。在 game.lua 结束时,我再次从 game.lua 过渡到 menu.lua。在menu.lua中的scene:show下我删除了游戏场景。当我回到 game.lua 时,scene:show 中的所有内容都会重复两次。 scene:create还是只有一次。

知道为什么会这样吗?

谢谢。

function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then

print("does this print once or twice?")



        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
    physics.start()
gameLoopTimer = timer.performWithDelay( 1750, gameLoop, 0 )




  -- Start the music!
        audio.play( musicTrack, { channel=1, loops=-1 } )
        audio.setVolume( 0, { channel=1 } ) 


        -- Code here runs when the scene is entirely on screen


    end

end

composer.gotoscene(game.lua) 由在创建阶段调用的 menuDrawer 函数创建的多个对象之一的点击调用。

local function menuDrawer()

....

for i = 1, #menuLetters, 1 do 

....

    local Letterbackground = display.newRoundedRect(sceneGroup, Letterbackgroundx, Letterbackgroundy, 100, 125, 10 )

....
Letterbackground:addEventListener( "tap", gotoGame )
end

永远不会删除 EventListener,因为它仅由函数内的局部变量定义。这会导致问题吗?

如果您需要更多信息,请告诉我。

scene:show 有两个相等的相位 didwill。所以它确实被调用了两次。请参阅下面的代码(它来自 Introducing the Composer API

-- "scene:show()"
function scene:show( event )

   local sceneGroup = self.view
   local phase = event.phase

   if ( phase == "will" ) then
      -- Called when the scene is still off screen (but is about to come on screen).
   elseif ( phase == "did" ) then
      -- Called when the scene is now on screen.
      -- Insert code here to make the scene come alive.
      -- Example: start timers, begin animation, play audio, etc.
   end
end

问题已解决。 menu.lua 中的 gotoscene 是在一个通过点击调用的函数中,它在函数末尾被 'return true' 修复。