Question

I've a function and would like to call here each 2 seconds during 3 seconds.

I tried timer.performwithDelay() but it doesn't answer to my question.

Here is the function I want to call each 2 secondes during 3 seconds :

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

How can I use timer.performwithDelay(2000, callback, 1) to call my function FuelManage(event) ?

Was it helpful?

Solution

So it looks like what you are actually after is to start a few check 2 seconds from "now", for a duration of 3 seconds. You can schedule registering and unregistering for the enterFrame events. Using this will call your FuelManage function every time step during the period of interest:

    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)

If this is too high frequency, then you'll want to use a timer, and keep track of time:

    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)

OTHER TIPS

Set a timer inside a timer like this:

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)

Be careful though because the way it's setup know you will have an infinite number of timer running very soon... Since the first timer has a lower lifetime than the second one. So you might think if you would like to secure the second timer by making sure it's cancelled first before calling it again, this kind of thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top