2014-04-26 45 views
-1

我有一个功能,并希望在3秒内每2秒钟拨打一次电话。我想试试timer.performwithDelay(),但它不回答我的问题。如何在有限的时间内运行一个函数?

这里是我想在3秒调用每个2个secondes功能:

function FuelManage(event) 
    if lives > 0 and pressed==true then   

     lifeBar[lives].isVisible=false 
     lives = lives - 1 
--  print(lifeBar[lives].x) 
     livesValue.text = string.format("%d", lives) 


    end 
end 

如何使用timer.performwithDelay(2000, callback, 1)打电话给我的功能FuelManage(event)

+0

每3秒钟3秒?这是一个呼叫从通话时间点延迟2秒? 'timer.performwithDelay(2000,callback,1)'会为你做。 –

+1

我很乐意提供帮助,但“3秒钟内每2秒钟”没有任何意义。第一次调用将在'performWithDelay'之后两秒钟,下一次调用将在此之后的另一个2秒钟内完成,因此从第一次调用到执行延迟时间总共需要4秒,并且不会在“3秒钟”内出现。请澄清。 – Schollii

回答

1

所以它看起来像你实际上是从“现在”开始几秒钟检查2秒,持续3秒。您可以为enterFrame事件安排注册和取消注册。使用这将调用您的FuelManage功能中的关注期间每个时间步长:

function cancelCheckFuel(event) 
     Runtime:removeListener('enterFrame', FuelManager) 
    end 

    function FuelManage(event) 
     if lives > 0 and pressed==true then   
      lifeBar[lives].isVisible=false 
      lives = lives - 1 
      -- print(lifeBar[lives].x) 
      livesValue.text = string.format("%d", lives)  
     end 
    end 

    -- fuel management: 
    local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds 
    local fuelCheckDurationMS = 3000 -- check for 3 seconds 
    local stopFuelCheckMS = startFuelCheckMS + fuelCheckDurationMS 
    timer.performWithDelay(
     startFuelCheckMS, 
     function() Runtime:addEventListener('enterFrame', FuelManager) end, 
     1) 
    timer.performWithDelay(
     stopFuelCheckMS, 
     function() Runtime:removeEventListener('enterFrame', FuelManager) end, 
     1) 

如果是这种频率太高,那么你要使用一个计时器,并跟踪时间:

local fuelCheckDurationMS = 3000 -- check for 3 seconds 
    local timeBetweenChecksMS = 200 -- check every 200 ms 
    local totalCheckTimeMS = 0 
    local startedChecking = false 

    function FuelManage(event) 
     if lives > 0 and pressed==true then   
      lifeBar[lives].isVisible=false 
      lives = lives - 1 
      -- print(lifeBar[lives].x) 
      livesValue.text = string.format("%d", lives)  
     end 

     if totalCheckTimeMS < 3000 then 
      timer.performWithDelay(timeBetweenChecksMS, FuelManage, 1) 
      if startedChecking then 
       totalCheckTimeMS = totalCheckTimeMS + timeBetweenChecksMS 
      end 
      startedChecking = true 
     end 
    end 

    -- fuel management: 
    local startFuelCheckMS = 2000 -- start checking for fuel in 2 seconds 
    timer.performWithDelay(startFuelCheckMS, FuelManage, 1) 
+0

谢谢你的帮助。我插入了代码,但它说:尝试调用字段'performwithDelay'(一个零值)(根据timer.performwithDelay(3000,FuelManage,1)) –

+0

@MehmetAitAddi请仅在尝试人们的答案时显示一些努力,花了整整一个小时试图弄明白,然后你发表了这样的评论。因为你应该自己想出了一个(如果你不能解决这个问题):问题是我拼错了它,它应该是'performWithDelay'。我已经更新了我的答案。另外,你是否正在尝试我的答案的解决方案3或4? – Schollii

+0

噢,我的上帝,我很抱歉:-(我尝试了答案4.我在一个名为“Checkfuel”的函数中添加了“燃料管理”,并添加了一个监听器,我编辑了我的问题 –

0

设置一个计时器这样的计时器内:

function FuelManage(event) 
    if lives > 0 and pressed==true then   
     lifeBar[lives].isVisible=false 
     lives = lives - 1 
--  print(lifeBar[lives].x) 
     livesValue.text = string.format("%d", lives)  
    end 
end 

-- Main timer, called every 2 seconds 
timer.performwithDelay(2000, function() 
    -- Sub-timer, called every second for 3 seconds 
    timer.performwithDelay(1000, FuelManage, 3) 
end, 1) 

不过要小心,因为它的设置知道你将有计时器很快运行无限多......因为第一计时器具有较低的方式终身比第二个。所以你可能会想,如果你想确保第二次计时器被取消,然后再次调用它,这种事情。

相关问题