Random 带有随机秒数的计时器¿;如何更新随机秒数?
我有一个计时器“Random 带有随机秒数的计时器¿;如何更新随机秒数?,random,timer,lua,coronasdk,2d-games,Random,Timer,Lua,Coronasdk,2d Games,我有一个计时器“tmr\u sendCesta”,必须在1到3秒之间每x秒调用一次。问题是计时器“tmr\u sendCesta”只调用了一次,而随机秒数从未更新。我需要每x秒随机调用函数“createCesta” 知道怎么做吗 function createCesta() cesta = display.newImageRect("cesta.png", 100, 55) cesta.x = -110 cesta.y = screenH - 110 cesta.
tmr\u sendCesta
”,必须在1到3秒之间每x秒调用一次。问题是计时器“tmr\u sendCesta
”只调用了一次,而随机秒数从未更新。我需要每x秒随机调用函数“createCesta
”
知道怎么做吗
function createCesta()
cesta = display.newImageRect("cesta.png", 100, 55)
cesta.x = -110
cesta.y = screenH - 110
cesta.name = "cesta"
physics.addBody( cesta, physicsData:get("cestaSmall"))
grupoCesta:insert(cesta)
transition.to(cesta, {time = 4000, x = screenW + 110})
end
function scene:enterScene( event )
local group = self.view
physics.start()
Runtime:addEventListener("touch", touchScreen)
Runtime:addEventListener( "collision", onCollision )
tmr_sendCesta = timer.performWithDelay(math.random(1000, 3000), createCesta, 0)
end
您的呼叫不正确,应该是:
tmr_sendCesta = timer.performWithDelay(math.random(1000, 3000), createCesta, 0)
另外,我认为使用0参数调用performWithDelay不会达到您的目的。我想数学随机数不会再计算了。。。(我在这里运行,两次调用之间有一个稳定的2078-2079毫秒的延迟)如果你想以随机间隔调用
createCesta
(或者randomCesta
,不确定这是否是一个输入错误或者你没有显示正确的函数),那么你每次都必须重新计算math.random。因此不能使用循环计时器,因为每次延迟都是相同的。您必须重新安排一个新的计时器,以计算一个新的随机数并创建一个新的计时器:
local function randomDelay() return math.random(1000, 3000) end
local function randomCesta()
cesta = display.newImageRect("cesta.png", 100, 55)
...
grupoCesta:insert(cesta)
transition.to(cesta, {time = 4000, x = screenW + 110})
# reschedule at new random time:
timer.performWithDelay(randomDelay(), randomCesta)
end
function scene:enterScene( event )
...
timer.performWithDelay(randomDelay(), randomCesta)
end
如果要取消/恢复/暂停计时器或转换,您可能只需要
timer.performWithDelay
和transition.to
的返回值。有其他方法吗@谢谢你!!这正是我所需要的,正确的方法是“createCesta”,我在问题中有“randomCesta”,因为我在尝试另一种方法。一个问题,enterScene函数中的计时器只调用了一次,对吗@这场戏出现在银幕上后,被称为“学院派”。