如何在有限时间内运行函数?
2014-4-28 17:18:47
收藏:0
阅读:113
评论:2
我有一个函数,希望在3秒内每2秒调用一次。
我尝试使用timer.performwithDelay(),但它没有回答我的问题。
以下是我想在3秒内每2秒调用的函数:
function FuelManage(event)
if lives > 0 and pressed==true then
lifeBar[lives].isVisible=false
lives = lives - 1
-- print( lifeBar[lives].x )
livesValue.text = string.format("%d", lives)
end
end
如何使用timer.performwithDelay(2000, callback, 1)调用我的函数FuelManage(event)?
点赞
用户869951
看起来你实际上想要从 "现在" 开始检查一些内容,持续 3 秒钟。你可以安排注册和注销 enterFrame 事件。使用它将在感兴趣的时间段内每个时间步长调用你的 FuelManage 函数:
function cancelCheckFuel(event)
Runtime:removeListener('enterFrame', FuelManager)
end
function FuelManage(event)
if lives > 0 and pressed==true then
lifeBar[lives].isVisible=false
lives = lives - 1
-- print( lifeBar[lives].x )
livesValue.text = string.format("%d", lives)
end
end
-- fuel management:
local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds
local fuelCheckDurationMS = 3000 -- check for 3 seconds
local stopFuelCheckMS = startFuelCheckMS + fuelCheckDurationMS
timer.performWithDelay(
startFuelCheckMS,
function() Runtime:addEventListener('enterFrame', FuelManager) end,
1)
timer.performWithDelay(
stopFuelCheckMS,
function() Runtime:removeEventListener('enterFrame', FuelManager) end,
1)
如果这个频率太高,那么你将需要使用计时器,并跟踪时间:
local fuelCheckDurationMS = 3000 -- check for 3 seconds
local timeBetweenChecksMS = 200 -- check every 200 ms
local totalCheckTimeMS = 0
local startedChecking = false
function FuelManage(event)
if lives > 0 and pressed==true then
lifeBar[lives].isVisible=false
lives = lives - 1
-- print( lifeBar[lives].x )
livesValue.text = string.format("%d", lives)
end
if totalCheckTimeMS < 3000 then
-- check again in timeBetweenChecksMS milliseconds
timer.performWithDelay(timeBetweenChecksMS, FuelManage, 1)
if startedChecking then
totalCheckTimeMS = totalCheckTimeMS + timeBetweenChecksMS
end
startedChecking = true
end
end
-- fuel management:
local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds
timer.performWithDelay(startFuelCheckMS, FuelManage, 1)
2014-04-27 20:32:50
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
可以像这样在定时器内部设置定时器:
function FuelManage(event) if lives > 0 and pressed == true then lifeBar[lives].isVisible = false lives = lives - 1 -- print( lifeBar[lives].x ) livesValue.text = string.format("%d", lives) end end -- 主定时器,每2秒调用一次 timer.performWithDelay(2000, function() -- 子定时器,每秒钟调用一次,共3秒 timer.performWithDelay(1000, FuelManage, 3) end, 1)不过需要注意,目前这种设置方式会导致无限数量的定时器在运行... 因为第一个定时器的生命周期低于第二个定时器。所以你可能需要想些如何确保在再次调用第二个定时器之前先取消它的方法,这种事情。