场景:第二次出现在场景中后显示两次
问题描述:
我使用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。这会导致问题吗?
如果您需要更多信息,请让我知道。
答
问题解决了。 menu.lua中的gotoscene被一个叫做tap的函数所调用,并且在函数的结尾被'return true'修复。
答
scene:show
有两个相位等于did
和will
。所以它确实被称为两次。见下面的代码(它来自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
只是一个说明:为了确认这一点,我只是在场景下放了print(“something”):show。 – Atrag