【发布时间】:2014-01-01 07:46:06
【问题描述】:
在开发我的应用程序时,我遇到了两个问题,因为我不明白如何通过事件停止循环。 我编写了以下代码来复制这个问题。
在我的 App 中有一个复杂的模型,需要最多两分钟的时间来计算解决方案。
我希望能够显示计算过程中经过的时间,并通过单击两个不同的按钮来停止或重新开始计算(为简单起见,代码中有两个圆圈)。
从下面的代码中可以看出,我可以查看经过的时间,只有在同一个循环完成时才停止循环。
有人遇到过这个问题吗?
对不起英语。 感谢您提供的提示
问候, 詹卢卡
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 )
--- here there is a simple function, but in my App is a complex loop that works until 2 minutes
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) --- here is a simple model
print(messagge_loop)
else
break
end
end
messagge_obj.text="Calculation done"
end
--- setting of the timer
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)
--- setting of play and stop button
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 inside model
loop_fun()
【问题讨论】:
-
你为什么使用限制为'1000000'的循环:O。是用来计算时间的吗!!?