[英]CORONA SDK: How can I stop a loop with an event?
在開發我的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()
我不完全了解您的需求。 以下是您所需要的嗎?
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)
繼續編碼......... :)
非常感謝您的答復和您發布的代碼。 但是我的意圖是要很長一段時間(直到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)
使用此代碼。
本地t
本地計數= 1
局部函數loop_fun()
如果Calculation_progress == true並且count <= 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)
結束
結束
並在calculation_progress標志之后在stop_calc()中調用loop_fun()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.