【问题标题】:Adding a delay/timer [duplicate]添加延迟/计时器[重复]
【发布时间】:2015-07-08 04:08:27
【问题描述】:

我目前使用 Corona SDK,Lua 作为我的主要语言。 我在这段代码中遇到问题 - 当我运行它时,它会自动为我提供我声明要打印出来的“光”值。我设置了 light = 2,并且通过这个循环,它应该每次将 light 减少 1,直到它

我正在制作一个“西蒙说”的游戏,因此,盒子不会亮起,因为它会同时运行所有内容。

代码如下:

if(count%20 == count - math.floor(count/20)*20) then
    clicked = 0

    while(light >= 0) do
        light = light - 1
        print(light)
    end
end 

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    这里是 Corona SDK 的一个简单的 timer.performWithDelay 函数。你可以在这里查看更多:https://docs.coronalabs.com/api/library/timer/performWithDelay.html

    以下是适合您问题的示例代码。

    注意:我将代码基于您上面提供的代码。

    local lights = 2
    local timerName -- ADD A TIMER ID TO YOUR TIMER SO THAT WE CAN CANCEL IT LATER ON
    
    local function myFunction()
    
        lights =  lights - 1
    
        if (lights >= 0) then
            --DO SOMETHING WITH THE LIGHTS
            print(lights)
        else
    
            --TERMINATE THE TIMER
            timer.cancel( timerName )
    
        end
    
    end
    
      if(count%20 == count - math.floor(count/20)*20) then
    
        timerName = timer.performWithDelay( 1000, myFunction, 0 )
    
      end 
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多