Corona SDK 计时器 countUp 在按钮触摸时加速?
2017-2-1 10:46:12
收藏:0
阅读:85
评论:1
Corona SDK 计时器 countUp 在按钮触摸时加速?我在我的游戏中有15个问题,每次回答被触摸时,计时器 countUp 就会加速,每次转到另一个问题时都会加速..
这是我的按钮触摸事件
buttonTouched = function(event)
local t = event.target
local id = t.id
if event.phase == "began" and touchEnabled == true then
display.getCurrentStage():setFocus( t )
t.isFocus = true
if id == "answer" then
t.alpha = 0.6
else
t.xScale = 0.9
t.yScale = 0.9
end
elseif t.isFocus then
if event.phase == "ended" then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if id == "answer" then
t.alpha = 1
else
t.xScale = 1
t.yScale = 1
end
-- 检查触摸是否完成在按钮内部。
local b = t.contentBounds
if event.x >= b.xMin and event.x <= b.xMax and event.y >= b.yMin and event.y <= b.yMax then
utils.playSound("select")
if id == "answer" then
if timer_trans ~= nil then
transition.cancel(timer_trans)
timer_trans = nil
end
if result_trans ~= nil then
transition.cancel(result_trans)
result_trans = nil
end
if label_result ~= nil then
display.remove(label_result)
label_result = nil
end
-- 显示一些我们可以转换的文本
label_result = display.newText({parent=uiGroup, text="", font=easyFont, fontSize=75})
label_result.anchorX = 0
label_result.x = label_question.x - 540
label_result.y = label_question.y + 400
if t.index == questions[onQuestion].answer then
label_result.text = "正确!"
label_result:setFillColor(0,0.6,0)
utils.playSound("score")
updateScore(1)
else
label_result.text = "错误。。。"
label_result:setFillColor(0.8,0,0)
utils.playSound("incorrect")
end
result_trans = transition.to(label_result, {time=1600, alpha=0.1, y=label_result.y-18,tag="transTag", onComplete=function()
display.remove(label_result)
label_result = nil
end})
-- 现在创建下一个问题
createQuestion()
end
end
end
end
return true
end
function startTimer()
clockTimer = timer.performWithDelay(1000,doCountUp,gameTime)
end
function doCountUp()
currentTime = countUpText.text
currentTime = currentTime +1
countUpText.text = currentTime
if(currentTime == 0) then
countUpText.text = currentTime
startTimer()
end
end
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 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 代码?

计时器正在“加速”,因为您点击问题时不是重置当前计时器,而是创建一个新计时器。
您每次创建新问题时都会调用 startTimer()(假设您在 createQuestion()中设置
countUpText.text =“-1”)。每次触摸答案时,您都要创建另一个计时器来更新countUpText.text。您有多个计时器更新该文本,因为您没有删除先前创建的计时器,而只是创建新的计时器。修复这个问题最简单的方法是,如果已经创建了计时器,则取消计时器并开始一个新的计时器:
local clockTimer function startTimer() if (clockTimer ~= nil) then timer.cancel(clockTimer) clockTimer = nil end clockTimer = timer.performWithDelay(1000,doCountUp,gameTime) end因此,请将您的
startTimer()函数更新为上面的内容,然后将local clockTimer添加到您的Lua文件的顶部。