To draw something every n frames

Hi again,

I’m trying to update the drawing of a string (let us call it “i”) on the screen, not 60 times a second but rather, let’s say, twice a second (while other stuff in the draw function are indeed updated 60 times a sec.). What would be a clever way to do this ?

I tried putting a “i= i + 1” at the beginning of draw() and find a way to say “draw every time ‘i’ increases by 30, don’t”, but I can’t figure how to do that…

(i’m not sure if my question is really clear…)

Thanks!

@Rodolphe The are several way to do a delayed draw. Here’s 2 of them. The 1st does a text every 40 times that draw is called. The 2nd does a text every 2 seconds.


function setup()
    i=0
    et=ElapsedTime
end

function draw()
    background(40, 40, 50)
    fill(255)
    i=i+1

    if i == 40 then  -- every 40 draws
        text("draw",WIDTH/2,HEIGHT/2)
        i=0
    end

    if ElapsedTime-et >= 2 then  -- every 2 seconds
        et=ElapsedTime
        text("ElapsedTime",WIDTH/2,HEIGHT/2-100)        
    end
end

Oh it seems so obvious afterwards…

This is great, thank you so much!!! :slight_smile:

problem with I=40 is that it will disappear in 1/60th of a second. write a Timer class that watches ElapsedTime and call Timer:draw() to manage your drawing.

Maybe make an infinite tween.delay(60 / 40, function() … end) loop?

This sky?

function Timer(delay)
 bool = false
 tween.delay(60/40, function() Timer(delay) bool = true  end)
 return bool
end

On my phone so I cant really develop on it but use bool as a global and an if statement in draw function to check if its called. The above function is not to be called in draw but in setup

Yeah, something like that. Delay isn’t ever used though, and from how you return it, bool would always be false.

function setup()
    frame = 0
    delay(60 / 30)
end

function delay(time)
    frame = frame + 1
    if frame == 31 then
        frame = 1
    end
    print("Updated frame!")
    tween.delay(time, function()
        delay(time)
    end)
end

function draw()
    sprite("Dropbox:Frame" .. frame, WIDTH / 2, HEIGHT / 2)
end

Untested, but it should start from Dropbox:Frame1 to Dropbox:Frame30 and then loop, updating twice every second.

Bool would be true on the one frame its executed, that seems logical? I haven’t tested it though, its sort of like stacking delays

It says that bool is false, and later to set it to true. But before it sets it to true, it returns bool, which you set to false. bool would be true sometimes, if, say, you used it in the draw function, but whenever the function Timer returns something, that something will always be false, as it hasn’t set it back to true yet.

Its sets to bool when the delay is up and sets the bool to false previous to that, its not meant to be called in the draw function because it will always be false

I’m not saying being called in the draw function, I’m saying using bool is the draw function. Since bool is global, you could use it in draw when it changes. If you use it that way, though, I don’t see the need for return bool.

I had another idea which I was going to do to make it a function that returns when a timer is up so you can do something like if Timer() then dosomecoolstuff end

Sadly, you can’t delay finishing a function without coroutines or halting every other process as well.