我使用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,因为只有函数中的局部变量定义了它。这会导致问题吗?
如果您需要更多信息,请告诉我们。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
问题解决了。 menu.lua中的gotoscene是一个通过点击调用的函数,它由' return true'在功能结束时。