花了几个小时试图在这里解决一个故事板问题。我已经修剪了一个复制它的示例项目。
问题:
- 从场景 1 到场景 2,然后回到场景 1,然后在场景中切换几次后,我得到以下信息:
- 进入场景时,日志显示多个 enterScene/exitScene 被触发
- 似乎可能正在注册多个背景触摸侦听器,但是我试图在 exitScene 函数中删除这些
- 我在试用版:2011.704 (2011.12.8)
输出
<code>
-- Click on background to go back to screen_example1
storyboard.gotoScene( screen_example1)
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
.
.
.
etc
</code>
代码:三个文件:主文件和两个场景文件:
主程序.lua
<code>
display.setStatusBar( display.HiddenStatusBar )
local storyboard = require "storyboard"
storyboard.gotoScene( "screen_example1" )
</code>
screen_example1.lua
<code>
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local function onScreenTouch( event )
if event.phase == "began" then
print("")
storyboard.gotoScene( "scene_towerView")
end
end
function scene:createScene( event )
print ("screen_example1 - createScene")
local image = display.newImage( "bg.jpg" )
scene.view:insert( image )
end
function scene:enterScene( event )
print ("screen_example1: enterScene")
scene.view:addEventListener( "touch", onScreenTouch )
end
function scene:exitScene( event )
print ("screen_example1: exitScene")
scene.view:removeEventListener("touch", onScreenTouch)
end
-- Scene Listeners
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
</code>
scene_towerView.lua
<code>
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local function onScreenTouch( event )
if event.phase == "began" then
print("")
print("storyboard.gotoScene( screen_example1)")
storyboard.gotoScene( "screen_example1")
end
end
-- Scene Handlers
function scene:createScene( event )
print ("TowerScene - createScene")
-- -- Background Image
local image = display.newImage( "bg2.jpg" )
scene.view:insert( image )
end
function scene:enterScene( event )
print ("TowerScene - enterScene")
scene.view:addEventListener( "touch", onScreenTouch )
end
-- Scene Listeners
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
</code>