为什么 Corona SDK 的场景隐藏事件未被触发?为什么定时器仍然在控制台打印信息,即使我已经移除了场景?
2016-8-8 7:8:6
收藏:0
阅读:95
评论:1
这是我如何开启定时器的:
local composer = require( "composer" )
local scene = composer.newScene()
timersArray = {}
function scene:create( event )
function performTimedEvents(timeString)
local function onTimer( event )
local params = event.source.params
if(arg == 5) then
timer.cancel( event.source )
return
end
end
for i=1,10 do
--print("performWithDelay "..sliceStars(timeString)[i])
timersArray[i] = timer.performWithDelay( 1000*sliceStars(timeString)[i], onTimer )
timersArray[i].params = { n = i }
end
end
performTimedEvents(pagesTimers[currentPage])
end
这是我在离开场景之前取消定时器的方法,但是当我离开场景时它们仍然存在,定时器仍然在控制台打印信息……特别注意此行 timer.cancel(timersArray[j])
function flushTimers()
pageSequence(5)
print("QQQtimersArray")
print_r(timersArray)
for j=1,#timersArray do
print("didCancelTimer "..j)
timer.cancel(timersArray[j])
--timer.pause(timersArray[j])
timersArray[j] = nil
end
timersArray = {}
print_r(timersArray)
print("didReturn");
end
另外,为什么 scene:hide 没有被触发,以前有 exit 的时候它还可以工作,但是现在它会输出 hide 回调,但不是 hide 事件,请帮助 :(
function scene:hide( event )
print("YYY_hide_!!!")
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
print("YYY_hide_will")
-- Called when the scene is on screen (but is about to go off screen)
-- Insert code here to "pause" the scene
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
print("YYY_hide_did")
-- Called immediately after scene goes off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- Insert code here to clean up the scene
-- Example: remove display objects, save state, etc.
--myAudioPlayer = nil
--audio.stop()
--audio.rewind(1)
flushTimers()
--timersArray = nil
print("YYY_destroy_did")
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
顺便说一下,这是我移除当前场景的方法:
composer.removeScene("currentScene")
composer.gotoScene("nextScene",effectOptions)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

当在
scene:hide()事件中的phase为did时,应该取消你的定时器:function scene:hide( event ) print("YYY_hide_!!!") local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("YYY_hide_will") -- 当场景在屏幕上但即将离开时调用 -- 在这里插入代码以“暂停”场景 -- 示例:停止定时器、停止动画、停止音频等 elseif ( phase == "did" ) then print("YYY_hide_did") -- 当场景离开屏幕后立即调用 flushTimers() composer.removeScene("sceneName") -- 示例:移除主场景 end end