Timers with Pause in Corona SDK

Time Machine Apps Logo

Time Machine Apps

After a few months of hard work, we come back with a lot of work well done, new supergames soon available and new post in our blog. The fruits of this labour are a big account of snippets that we want to share with all of you. In this post, we are going to see a topic related to timer in Corona SDK. As always, sorry for my bad English :-)

Timers are very useful in many situations in a game development: events that activate doors, make a gamer invulnerable during a few seconds, etc. However, what happens when we want to pause the game? How can we make to stop and restore the timers state when we make a pause during the game? In other frameworks, we can directly pause the timer and play them when the pause ends. But we can not make this in Corona SDK, so if we do not control it, the actions associated to a timer will be execute when the game is paused.

Now we are going to show the source code that can be used to solve this problem. In this example, two buttons appear in the screen, one to pause the game and another one to put bombs in the game. This last button executes a timer that will produce the bomb explosion after three seconds. Logically all these explosions will be simulated and all the information will be shown in the Corona SDK’s console.

The code is commented. We hope it was useful for you.
Regards.

5 comments

  1. Hi, thanks for sharing! not sure why i cant execute your sample successfully, i have put v1.5 ui.lua , shortButton.png , shortButtonOver.png in the project folder, it will output errors in corona simulator.

  2. Hi Sun,
    I’ve uploaded another time to the entry. Could you try again?

    The other resources I’ve used are:
    – ui.lua, v1.5
    – build.settings
    – config.lua
    – two png

    all the resources are typical, they don’t contain nothing special.

    Could you tell me what are the errors?

  3. Hi,

    Sorry for the late.
    I replaced “defaultSrc” with “default” ,”overSrc” with “over”, now it’s working.
    I didn’t create build.settings and config.lua .

    But it seems not stable, sometimes get this error:

    /Users/sunjiajie/TestPro/main.lua:14: attempt to index field ‘?’ (a nil value)
    stack traceback:
    [C]: ?
    /Users/sunjiajie/TestPro/main.lua:14: in function ‘_listener’
    ?: in function
    ?: in function

    the 14 line is print (“The bomb has exploted!!!, number “, timersData[event.source].bomb)

    I’ll also try to fix this bug.

  4. Hi,

    Just find this bug: if you click “Pause” button first, then click “Put bomb” button several times, then click “Pause” again, the error will be occur.

    Added a ” if ” condition to the putBombButton function , the bug will be fixed:

    local function putBombButton()
    — timer that explotes the bomb.
    if gameIsActive == true then
    local timerId = timer.performWithDelay(3000 , explotebomb, 1 )

    — Save the timer data, in this table we could also include logic data of our game. For example the bomb number.
    — For implement the pause, we need 2 attributes:
    — * startTime: that indicates when the timer started.
    — * remainingTime: the remaining time for the method execution, in this case the initial time.
    timersData[timerId] = { startTime = system.getTimer(), remainingTime = 3000, bomb = bombs }
    bombs = bombs + 1
    end
    end

  5. Hi Sun,
    the example doesn’t have bugs, because its aim is to show how to make pauseble timers. No other considerations are taken into account.

    On the one hand, if you have had to replaced “defaultSrc” with “default” and ”overSrc” with “over”, it is because you aren’t using the v1.5 of ui.lua. In older versions, the names of the params were default and over. Please, update your version.

    Regarding to the other error, this is not an error of the example. The example only covers pausable timers. The pause doesn’t manage menus or buttons. They weren’t included on the one hand, due to this is a problem with an easy solution, and on the other hand, to make the code as simple as possible. This way everybody can extract and reuse the code in their own projects easily. Others elements that we have to take into account in a real game are: listener (touch,…), enterFrame, collision actions, etc.

    Regards and thanks for the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *


seven × = 14

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>