Reputation: 21
In developing of my App, i have two problem caused by the fact that I do not understand how to stop a loop with an event. I have written the following code to replicate this problem.
In my App there is a complex model that takes up to two minutes of time to calculate the solution.
I would like to have the possibility to show the time that pass during the calculation and to stop or start again the calculation through the click on two differents buttons (in the code are two circles for simplicity).
How is visible from the following code, I can view the time passing and stop the loop only when the same loop is finished.
Someone has already had this issue?
Sorry for english. Thanks for the tips that will come
regards, 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 )
--- 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()
Upvotes: 1
Views: 1386
Reputation: 224
Use this code.
local t
local count = 1
local function loop_fun()
if calculation_progress==true and count<= 1000000 then
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)
else
messagge_obj.text="Calculation done"
timer.cancel(t)
end
end
and call the loop_fun() within stop_calc() after calculation_progress flag.
Upvotes: 0
Reputation: 21
Thank you very much for you reply and for your code posted. But my intention is to exit from a calculation loop very long (until 2-5 minutes). I have resolved it with a advice received in the COPROA SDK Forum Below the code.
regards, 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 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)
Upvotes: 1
Reputation: 7390
I'm not fully aware of your need. Is the following is what you needed?
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)
Keep coding.................. :)
Upvotes: 1