如何使用事件停止循环?(使用 CORONA SDK)
2013-12-13 13:54:30
收藏:0
阅读:103
评论:3
在开发我的应用程序时,因为我不知道如何用事件停止循环,所以我遇到了两个问题。
我编写了以下代码来复制这个问题。
在我的应用程序中,有一个复杂的模型,需要长达两分钟的时间来计算解决方案。
我想要有可能在计算过程中显示经过的时间,并通过单击两个不同的按钮(在代码中是两个圆圈)停止或重新开始计算。
如下代码所示,只有当相同的循环结束时,我才能查看时间的流逝并停止循环。
有人已经遇到了这个问题吗?
对于英语我很抱歉。
谢谢将要来的提示
问候,
Gianluca
local calculation_progress=true
local messagge_obj=display.newText("Start Calculation",display.contentCenterX-200, display.contentCenterY-200, native.systemFont, 40)
messagge_obj:setTextColor( 255, 255, 255 )
--- 这里有一个简单的函数,但在我的应用程序中,是一个需要工作两分钟的复杂循环
local function loop_fun()
messagge_obj.text="Calculation started"
local max_comps=1000000
for i=1,max_comps do
if calculation_progress==true then
local messagge_loop=tostring(i) --- 这里是一个简单的模型
print(messagge_loop)
else
break
end
end
messagge_obj.text="Calculation done"
end
--- 定时器的设置
local test_time_label = display.newText("Time:", display.contentCenterX-125, display.contentCenterY-50, native.systemFont, 40 )
local test_time = display.newText("", display.contentCenterX-20, display.contentCenterY-10, native.systemFont, 40 )
local function refresh_time()
local currentTime = os.date("*t")
test_time.text=os.time( t ) --currentTime
end
Runtime:addEventListener("enterFrame", refresh_time)
--- 播放和停止按钮的设置
local function play_calc()
calculation_progress=true
loop_fun()
print("calculation_progress",calculation_progress)
end
local function stop_calc()
calculation_progress=false
print("calculation_progress",calculation_progress)
end
local myButton_calc_play = display.newCircle(display.contentCenterX+60,display.contentCenterY+200,60)
local test_play = display.newText("Play", display.contentCenterX+10,display.contentCenterY+180, native.systemFont, 45 )
test_play:setTextColor(0)
test_play:addEventListener("tap", play_calc)
local myButton_calc_stop = display.newCircle(display.contentCenterX-100,display.contentCenterY+200,60)
local test_stop = display.newText("Stop", display.contentCenterX-145,display.contentCenterY+180, native.systemFont, 45 )
test_stop:setTextColor(0)
test_stop:addEventListener("tap", stop_calc)
--- 模型内的循环
loop_fun()
点赞
用户3099582
非常感谢您的回复和代码。 但我的意图是退出一个非常长的计算循环(长达2-5分钟)。 我在COPROA SDK论坛中得到了一个建议,解决了这个问题。 以下是代码。
问候, Gianluca
local calculation_progress=true
local messagge_obj=display.newText("Start Calculation",display.contentCenterX-200, display.contentCenterY-200, native.systemFont, 40)
messagge_obj:setTextColor( 255, 255, 255 )
--- 退出循环的函数
function verify_progress_loop()
local outcome=true
if calculation_progress==false then
outcome=false
end
return outcome
end
--- 这里有一个简单的函数,但在我的应用程序中,它是一个需要工作2分钟的复杂循环
local max_comps=100000
local n_min_cicle=100
local n_delay=max_comps/n_min_cicle
local function loop_fun()
local t
local i=0
local function doWork()
for j=1,n_min_cicle do
local outcome=verify_progress_loop()
if outcome==true then
local messagge_loop=tostring(i) --- 这里是一个简单的模型
print(messagge_loop)
else
messagge_obj.text="计算停止"
timer.cancel(t)
--return 0
end
i=i+1
if i==max_comps then
messagge_obj.text="计算完成"
timer.cancel(t)
end
end
end
t=timer.performWithDelay( 0.1, doWork, n_delay )
end
--- 定时器设置
local test_time_label = display.newText("时间:", display.contentCenterX-125, display.contentCenterY-50, native.systemFont, 40 )
local test_time = display.newText("", display.contentCenterX-20, display.contentCenterY-10, native.systemFont, 40 )
local function refresh_time()
local currentTime = os.date("*t")
test_time.text=os.time( t ) --currentTime
end
Runtime:addEventListener("enterFrame", refresh_time)
--- 播放和停止按钮设置
local function play_calc(event)
messagge_obj.text="计算开始"
calculation_progress=true
loop_fun()
print("Inside play_cal function")
return true
end
local function stop_calc(event)
calculation_progress=false
print("Inside stop_cal function")
return true
end
local myButton_calc_play = display.newCircle(display.contentCenterX+60,display.contentCenterY+200,60)
local test_play = display.newText("播放", display.contentCenterX+10,display.contentCenterY+180, native.systemFont, 45 )
test_play:setTextColor(0)
test_play:addEventListener("tap", play_calc)
local myButton_calc_stop = display.newCircle(display.contentCenterX-100,display.contentCenterY+200,60)
local test_stop = display.newText("停止", display.contentCenterX-145,display.contentCenterY+180, native.systemFont, 45 )
test_stop:setTextColor(0)
test_stop:addEventListener("tap", stop_calc)
2013-12-14 14:06:42
用户2409015
使用这段代码。
local t
local count = 1
local function loop_fun()
if calculation_progress==true and count<= 1000000 then
messagge_obj.text="开始计算"
local messagge_loop=tostring(count) --- 这里是一个简单的模型
print(messagge_loop)
count = count + 1
t = timer.performWithDelay( 0.1, loop_fun)
else
messagge_obj.text="计算完成"
timer.cancel(t)
end
end
在 calculation_progress 标志后调用 loop_fun()。
2014-03-20 10:39:27
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
我不完全了解您的需求。以下是您需要的内容吗?
local statusLabel local calculation_progress = false local timer_1 local messageLabel = display.newText("开始计算", 90, 90, native.systemFont, 20) messageLabel:setTextColor(255, 255, 255) local minVal = 02 local secVal = 00 local minLabel = display.newText("0" .. minVal, 130, 120, native.systemFont, 20) local secLabel = display.newText(":0" .. secVal, 160, 120, native.systemFont, 20) local function timerDown() secVal = secVal - 1 if (secVal < 0 and minVal <= 0) then if (timer_1) then timer.cancel(timer_1) end statusLabel.text = "播放" print("计时器停止... 计算完成...") return true; end if (secVal <= 0 and minVal > 0) then secVal = 59 minVal = minVal - 1 end secLabel.text = ":" .. secVal minLabel.text = minVal if (secVal < 10) then secLabel.text = ":0" .. secVal end if (minVal < 10) then minLabel.text = "0" .. minVal end print(secVal) end local function myCalculationChanger() --[[我将此分配给一个按钮, 如果您愿意,可以将它们分开--]] if (calculation_progress == false) then -- 第一次点击开始 calculation_progress = true statusLabel.text = "停止" timer_1 = timer.performWithDelay(1000, timerDown, -1) else -- 下一次点击重新开始 if (timer_1) then timer.cancel(timer_1) end minVal = 02 secVal = 00 secLabel.text = ":" .. secVal minLabel.text = minVal if (secVal < 10) then secLabel.text = ":0" .. secVal end if (minVal < 10) then minLabel.text = "0" .. minVal end statusLabel.text = "播放" calculation_progress = false end end local myButton = display.newCircle(0, 0, 60) myButton.x = display.contentWidth / 2 myButton.y = display.contentHeight - 100 myButton:addEventListener("tap", myCalculationChanger) statusLabel = display.newText("播放", 20, 20, nil, 40) statusLabel.x = myButton.x statusLabel.y = myButton.y statusLabel:setTextColor(0)继续编码…… :)