场景:第二次出现在场景中后显示两次

问题描述:

我使用gotoscene从menu.lua转到game.lua。在game.lua结束时,我再次从game.lua转换到menu.lua。在场景下:在menu.lua中显示我删除游戏场景。当我回到game.lua场景中的所有内容时:show重复两次。场景:创造仍然只有一次。场景:第二次出现在场景中后显示两次

任何想法,为什么会这样?

谢谢。

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不会被删除,因为只有函数中的局部变量定义了EventListener。这会导致问题吗?

如果您需要更多信息,请让我知道。

+0

只是一个说明:为了确认这一点,我只是在场景下放了print(“something”):show。 – Atrag

问题解决了。 menu.lua中的gotoscene被一个叫做tap的函数所调用,并且在函数的结尾被'return true'修复。

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 
+0

嗨。对不起,是的,我知道。我的意思是在每个短语下重复两次。我将编辑我的原始文章以包含它。 – Atrag

+0

我有类似的问题。它与事件听众有关。在它内部,我放了'composer.gotoScene'调用。在移至下一个场景之前,我忘记移除事件监听器。所以这是两次以上的电话。每次我回到菜单场景时它会增加。如果您显示更多代码,那么为我解决问题将更容易。 – ldurniat

+0

好的。再次编辑。谢谢。但是如果情况确实如此,我肯定创作场景也会运行两次。 – Atrag