如何使用事件停止循环?(使用 CORONA 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 )

--- 这里有一个简单的函数,但在我的应用程序中,是一个需要工作两分钟的复杂循环
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()
点赞
用户1979583
用户1979583

我不完全了解您的需求。以下是您需要的内容吗?

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)

继续编码…… :)

2013-12-14 10:00:42
用户3099582
用户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
用户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