如何在 Lua Corona SDK 中生成超级动态随机数?

How Can I Generate Super Dynamic Random Numbers In Lua Corona SDK?

我需要动态生成一个每秒变化的随机数。

这个怎么样:

local function numberGenerator()
    print("Random number:", math.random(80000, 180000) )    
    timer.performWithDelay( 1000, numberGenerator )
end
numberGenerator()

您可以使用 timer.performWithDelay() 来做到这一点。喜欢:

local function generateRandomNumber()
   local myRandomNumber = math.random(10000)
   print("myRandomNumber="..myRandomNumber)
   timer.performWithDelay(1000, generateRandomNumber) -- Rest of your calls
end
generateRandomNumber() -- First call

定时器语法如下:

timer.performWithDelay( delay, listener [, iterations] )

例如:

timer.performWithDelay(1000, myFunction,1) -- Here the 'myFunction' will get called once. 
timer.performWithDelay(1000, myFunction,2) -- Here the 'myFunction' will get called twice.

在以下两种情况下,'myFunction' 将被无限调用。

timer.performWithDelay(1000, myFunction,-1)
timer.performWithDelay(1000, myFunction)

并且,1000 是以毫秒为单位的时间。
即,1000 毫秒 = 1 秒

您可以找到更多关于 corona timer.performWithDelay() here 的信息。

继续编码......................:)