Richard C
Richard C

Reputation: 67

Resume game from time at which it was paused

I am writing a game using CreateJS and using CocoonJS to distribute. Within CocoonJS API are a couple of listener functions that allow callbacks when pausing and resuming of the game. The game's timer and (time-based) animations are driven by the Ticker event's "delta" property. The issue that I am having at the moment is, on resuming the game following on from pausing it, the timer will pick up from the time at which it paused plus the time spent whilst paused. For example, I pause the game after 20 seconds for exactly 4 seconds, on resuming the game the timer will carry on from 24 seconds (not 20 seconds, which is intended). I've tried storing the ticker event's "runTime" property before pausing and attempting to then set the ticker event's "runTime" to this stored value on resume, but this doesn't work.

A snippet of my original code (before tinkering) is like the following:

createjs.Ticker.setFPS(60);
createjs.Ticker.on("tick", onTick, this);

Cocoon.App.on("activated", function() {
    console.log("---[[[[[ APP RESUMING ]]]]]---");
    createjs.Ticker.paused = false;
});

Cocoon.App.on("suspending", function() {
    console.log("---[[[[[ APP PAUSING ]]]]]---");
    createjs.Ticker.paused = true;
});

onTick = function (e) {
    if (!e.paused) {
        animateUsingTicker(e.deltaTime);
        countDownTimerUsingTicker(e.deltaTime);
        //etc...
        stage.update();
    }
};

Can someone please assist me on this?

Many thanks

Upvotes: 0

Views: 305

Answers (1)

gskinner
gskinner

Reputation: 2488

One really easy way to deal with this is to use Timer.maxDelta. For example, if you are targeting 60fps, you could set this to something like 32ms (double the expected delta), to prevent getting huge values back when resuming an app/game.

Upvotes: 0

Related Questions