Howdy, Stranger!

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

Pause function

edited August 2012 in Code Sharing Posts: 18

Hi, here is a simple pause function I made :



-- TEST -- Use this function to perform your initial setup function setup()     print("Hello World!")     wait = 5     i = 0 end function sectoframes(sec)     return sec * 60 end function frametosec(frame)     return frame / 60 end function pause(wait)          if frametosec(i) < wait then          i = i + 1         return 0     end          if i == sectoframes(wait) then         return 1     end      end      -- This function gets called once every frame function draw()     -- This sets a dark background color      background(40, 40, 50)          -- This sets the line thickness     strokeWidth(5)     -- Do your drawing here     pausenumber = pause(5)     if pausenumber == 1 then         text("ok", WIDTH/2, HEIGHT - 100)     elseif pausenumber == 0 then         text("wait", WIDTH/2, HEIGHT - 100)     else         text("error", WIDTH/2, HEIGHT - 100)     end end

What do you think about it ?

Comments

  • Posts: 489

    Hello @eloi67. If you made use of ElapsedTime to keep track of the passage of time, then you would not need to count frames (or assume a constant frame rate of 60 per second).

    By using the arg feature of Lua, the pause function could do two things:

    (1) called with an argument, it could set the duration of the next pause (if it is not already waiting); and

    (2) called with no arguments, it could only return whether or not it is waiting.

    For example (touch the viewer for a two-second wait):

    function setup()
        fill(255)
        fontSize(64)
        cx = WIDTH/2
        cy = HEIGHT/2
    end
    
    local isWaiting = false
    local endTime
    function pause(...)
        if isWaiting and ElapsedTime > endTime then
            isWaiting = false
        end            
        if arg["n"] > 0 and not isWaiting then
            endTime = ElapsedTime + arg[1]
            isWaiting = true
        end
        return isWaiting
    end
        
    function draw()
        background(0)
        if pause() then
            text("Waiting", cx, cy)
        else
            text("Not waiting", cx, cy)
        end
    end
    
    function touched(touch)
        if touch.state == BEGAN then pause(2) end
    end
    
  • Jmv38Jmv38 Mod
    Posts: 3,295

    hello. I have found the following function in the forum. I use it in my pushball game, it works very well, once i understood the syntax to pass the callback function. It is not exactly a pause() function, but the usage of a pause function is to start something after the pause, exactly what this timer does.

    -- timer class code from Barryrgo on CODEA forum (with minor changes from JMV38)
    -- usage syntax for instance to launch myCall() in 2s:
    --    timer1 = Timer(function() myCall() end)
    --    timer1:start(2)
    
    Timer = class()
    
    function Timer:init(callBackFun)
        -- you can accept and set parameters here
        self.action = callBackFun
        self.startTime = 0
        self.timeDelay = 0
        self.enable = false
    end
    
    
    function Timer:start(time)
        self.startTime = ElapsedTime
        self.timeDelay = time
        self.enable = true
    end
    
    function Timer:reset()
        self.enable = false
    end
    
    function Timer:update()
        if (self.enable == true) then
            local currentTime = ElapsedTime
            if currentTime - self.startTime >= self.timeDelay then
                self.enable = false
                if type(self.action) == "function" then
                    self.action()  
                end
            end
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,683

    If you're after a pause for a game or anything else, then this is pretty simple. But running this leads to a question I have about the draw function. When I run this and hit pause, I constantly show 3 numbers during the pause even though I only draw 1 number per draw cycle. It doesn't matter how fast or slow I hit pause. Is there a 3 screen buffer being drawn during each draw cycle.
    If I add another text line, then I see 6 numbers during the pause. I was expecting to see 1 number per text line, not 3.


    function setup()     count=0 end function draw()     if pause then    -- if pause is true, exit the draw function         return      end          -- draw something to show that pause is working     background(40, 40, 50)     fill(255)     count = count + 1     text(count,math.random(WIDTH),math.random(HEIGHT))  end function touched(t)     if t.state==BEGAN then         pause = not pause    -- toggle pause, true or false     end end
  • Posts: 2,161

    Is there a 3 screen buffer being drawn during each draw cycle.

    Yes.

  • dave1707dave1707 Mod
    Posts: 7,683

    I took my pause program one step further by adding pause=true after the text statement. So basically I'm pausing after each draw cycle. Looking at the numbers that are displayed, I see that:

    1.) There are 3 screen buffers that I saw earlier.

    2.) The buffers don't change in a regular pattern. Some of the buffers stay around for up to 8 cycles, while other buffers stay for 1 cycle. Interesting how the draw function works.


    function setup()     count=0 end function draw()     if pause then    -- if pause is true, exit the draw function         return      end          -- draw something to show if pause is working     background(40, 40, 50)     fill(255)     count = count + 1     text(count,math.random(WIDTH),math.random(HEIGHT))      pause=true end function touched(t)     if t.state==BEGAN then         pause = not pause    -- toggle pause, true, false     end end
Sign In or Register to comment.