Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Delay time

edited November 2011 in Code Sharing Posts: 65

Is there any way to call draw() function in every 1 sec rather than in every frame?


  • BortelsBortels Mod
    Posts: 1,557

    No... but why would you want to? if you have something that needs to happen once per second, you can look at ElapsedTime.

    draw() is probably unfortunately named. Yes, it's where you draw - but it's also your application's main event loop. It has to happen frequently to handle all of the other aspects of your IO - touches, the accelerometers, etc.

    There's a part of me that wishes they had instead just said "Your main.lua is executed repeatedly, and if you have a setup() function, it'll be called once at the beginning of the program". The result would be the same, but people wouldn't be misled into thinking that a good way to mess with draw() timing is to limit how often it's called. This should probably be in the FAQ...

  • SimeonSimeon Admin Mod
    Posts: 5,387

    That said, you can perform events once per second like this:

    function setup()
        currentSecond = 0
        xpos = 0
    function updatePos()
        -- This does something that happens once per second
        xpos = xpos + 20
    function draw()
        if ElapsedTime >= currentSecond then
            -- do something, eg.
            currentSecond = currentSecond + 1
        -- Your drawing still happens every frame
        ellipse( xpos, HEIGHT/2, 100, 100 )

    For the moment, it's best to keep drawing every frame. When retained backing support is available (update after 1.1.2) you will be able to actually draw once per second as well.

  • Thanks a lot bortels and Simeon for your responses.
    I have understood the fact about draw() function.. Thanks bortels. I was actually trying to create an analog clock where a line will rotate in every second. I managed to do that with the help of Simeon's code. Thanks Simeon for a nice example which even made more clear how to al with draw function :)

Sign In or Register to comment.