在开发我的应用程序时,我有两个问题是由于我不明白如何使用事件停止循环。 我写了以下代码来复制这个问题。
在我的应用程序中,有一个复杂的模型,最多需要两分钟的时间来计算解决方案。
我希望有可能在计算过程中显示通过的时间,并通过点击两个不同的按钮来停止或重新开始计算(为简单起见,代码中有两个圆圈)。
如何从以下代码中看到,我可以查看传递时间并仅在完成相同循环时停止循环。
有人已经遇到过这个问题吗?
抱歉英语。 感谢您的提示
的问候, 詹卢卡
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()
答案 0 :(得分:1)
我不完全了解你的需要。以下是您所需要的吗?
local statusLabel
local calculation_progress = false
local timer_1
local messageLabel =display.newText("Start Calculation",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 = "Play"
print("Timer stopped... Calculation Finished...")
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()
--[[I've assigned this to a single button,
if you want, you can separate them --]]
if(calculation_progress==false)then -- First click to start
calculation_progress = true
statusLabel.text = "Stop"
timer_1 = timer.performWithDelay(1000,timerDown,-1)
else -- Next click to restart
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 = "Play"
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("Play",20,20,nil,40)
statusLabel.x = myButton.x
statusLabel.y = myButton.y
statusLabel:setTextColor(0)
继续编码..................:)
答案 1 :(得分:1)
非常感谢您的回复以及发布的代码。 但我的意图是退出计算循环很长时间(直到2-5分钟)。 我已经通过COPROA SDK论坛收到的建议解决了这个问题 代码下方。
的问候, 詹卢卡
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 to exit from loop
function verify_progress_loop()
local outcome=true
if calculation_progress==false then
outcome=false
end
return outcome
end
--- here there is a simple function, but in my App is a complex loop that works until 2 minutes
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) --- here is a simple model
print(messagge_loop)
else
messagge_obj.text="Calculation stopped"
timer.cancel(t)
--return 0
end
i=i+1
if i==max_comps then
messagge_obj.text="Calculation done"
timer.cancel(t)
end
end
end
t=timer.performWithDelay( 0.1, doWork, n_delay )
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(event)
messagge_obj.text="Calculation started"
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("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)
答案 2 :(得分:0)
使用此代码。
local t
本地计数= 1
本地函数loop_fun()
如果calculate_progress == true且count&lt; = 1000000则
messagge_obj.text="Calculation started"
local messagge_loop=tostring(count) --- here is a simple model
print(messagge_loop)
count = count + 1
t = timer.performWithDelay( 0.1, loop_fun)
否则
messagge_obj.text="Calculation done"
timer.cancel(t)
端
端
并在calculate_progress标志之后调用stop_calc()中的loop_fun()。