Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and Restart CreateJs animation

I was wondering how could I stop an entire CreateJs canvas animation and how to restart it later. I'd like to save computing power and memory while the canvas is not visible for the user.

Does anybody know how?

Thank you in advance

like image 387
Andrea Silvestri Avatar asked Jul 14 '15 15:07

Andrea Silvestri


2 Answers

Simply remove the listener on Ticker. For example:

createjs.Ticker.removeEventListener("tick", myStageOrTickFunction);
// then add it back later to unpause:
createjs.Ticker.addEventListener("tick", myStageOrTickFunction);

If you want to reset an entire animation exported from Flash, you have a couple of options:

  1. You can re-instantiate the main timeline. You'll need to take a look at the output code to grab the name of the main timeline symbol, but it is generally based on the FLA name (ex. an FLA named "test.fla" will have its main timeline symbol named "Test").

stage.removeChildAt(0);

stage.addChild(new lib.Test());

  1. You can use gotoAndPlay(0). This requires that all child MovieClips are set as Graphic instances though, because MCs play independently of their parent.

stage.getChildAt(0).gotoAndPlay(0)

like image 81
gskinner Avatar answered Nov 19 '22 00:11

gskinner


You need to remove exportRoot from Stage then add it again

function restart()
    {


        stage.getChildAt(0).gotoAndPlay(0);
        stage.removeChildAt(0);
        createjs.Ticker.removeEventListener("tick", stage);
        createjs.Sound.stop();

        exportRoot = new lib[Object.getOwnPropertyNames(lib)[Object.getOwnPropertyNames(lib).length-3]]();
        stage.addChild(exportRoot);

        createjs.Ticker.addEventListener("tick", stage);
        stage.update(); 

    }
like image 43
freeman3020 Avatar answered Nov 19 '22 00:11

freeman3020