【问题标题】:scene:show runs twice after going to scene the second timescene:show 在第二次进入场景后运行两次
【发布时间】:2017-05-09 04:21:19
【问题描述】:

我使用 gotoscene 从 menu.lua 转到 game.lua。在 game.lua 结束时,我再次从 game.lua 过渡到 menu.lua。在 scene:show in menu.lua 下,我删除了游戏场景。然后,当我回到 game.lua 时,场景中的所有内容:显示都重复了两次。场景:创建仍然只有一次。

知道为什么会这样吗?

谢谢。

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 永远不会被删除,因为它只由函数内的局部变量定义。这会导致问题吗?

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

【问题讨论】:

  • 请注意:为了确认这一点,我只是将 print("something") 放在 scene:show 下。

标签: coronasdk


【解决方案1】:

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

【讨论】:

  • 嗨。对不起,是的,我知道。我的意思是每个短语下的所有内容都重复两次。我将编辑我的原始帖子以包含它。
  • 我也有类似的问题。它与事件侦听器有关。我在里面打了composer.gotoScene 电话。在移动到下一个场景之前,我忘记了删除事件侦听器。所以它被调用了两次甚至更多。每次我回到菜单场景时它都会增加。如果您显示更多代码,我会更容易解决您的问题。
  • 好的。再次编辑。谢谢。但如果是这样的话,我肯定创建场景也会运行两次?
  • 就我而言,scene.create 只运行一次。
【解决方案2】:

问题解决了。 menu.lua 中的 gotoscene 位于通过点击调用的函数中,并在函数末尾通过“return true”修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 2018-11-03
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多