Skip to main content
Inspiring
February 21, 2017
Answered

Remove/Restart Ticker

  • February 21, 2017
  • 2 replies
  • 4549 views

Hello,

    I am creating a canvas animation that I want to restart. I am using the ticker and have tried numerous was to remove/restart it, but it continues to increment no matter what I do.

Here is how I initialize it:

createjs.Ticker.addEventListener("tick", handleTick);

createjs.Ticker.setFPS(24);

console.log(createjs.Ticker.getTicks());

in my restart function, I do this, but have tried numerous ways:

createjs.Ticker.removeAllEventListeners();

createjs.Ticker.addEventListener("tick", handleTick);

console.log(createjs.Ticker.getTicks()); //When I output this, it just picks up where it left off even though I remove it.

Please note that I do not want to pause it. I would like it to start again from 0 when I restart.

As always, any insight is much appreciated!

    This topic has been closed for replies.
    Correct answer ClayUUID

    Ok, so the key issue that I was having with the ticker speed is that it was basically getting re initialized every time I "rewind" using gotoAndPlay(0);

    To remedy this problem, I put the ticker inside of this function so that the listener is only added when the window is initialized. My slider (or timeline scrubber) is now in sync with the animation on the timeline.

    if (!window.iHaveBeenInitialized) {

         createjs.Ticker.addEventListener("tick", handleTick);

         createjs.Ticker.setFPS(24);

         useTicks = true;

    }

    function handleTick(event) {

           if(sliderBtn.x >= bounds.x && sliderBtn.x <= 700){

           sliderBtn.x = sliderBtn.x + (bounds.width/theTotalFrames);

      }

    }


    I'm going to say again that doing it that way is wrong. Incrementing the position of the slider every tick is an incredibly brittle solution that only works when the timeline is playing. Stopping, pausing, or rewinding the timeline completely breaks it. A proper solution that doesn't have any of those problems would look more like this:

    function handleTick() {

        sliderBtn.x = bounds.x + bounds.width * (exportRoot.currentFrame / exportRoot.totalFrames);

    }

    The sort of thing you're trying to do is the very reason the currentFrame property exists, so people don't have to resort to ugly hacks to sync code-based animation with the timeline.

    Also, there's no reason to include setFPS in your initialization function, since the FPS is already automatically set to the stage frame rate. And I'm pretty sure "useTicks = true" doesn't do anything. There is no global variable "useTicks".

    2 replies

    Brainiac
    February 21, 2017

    30 seconds of Googling...

    EaselJS v0.8.2 API Documentation : Ticker - reset

    EaselJS v0.8.2 API Documentation : Ticker - init

    I don't know why you'd think removing event listeners from the Ticker would stop it. Events happen regardless of whether they're being listened for.

    That being said, I'm doubtful that resetting the ticker will accomplish what you think it will. I kind of hope it doesn't, because messing with the global ticker sounds like a horrible hack.

    aewuAuthor
    Inspiring
    February 21, 2017

    This did not work. I tried. Also, please keep in mind that you likely have a lot more experience than I do and I'm trying to feel this out. I am a big advocate of trying to figure things out myself and only post after trying several different ways of doing something. Pointing out the time it takes for you to Google something that didn't work for me is not helpful and not why I use these forums.

    kglad
    Community Expert
    February 21, 2017

    what do you want to restart?

    aewuAuthor
    Inspiring
    February 21, 2017

    I want to go back to the first frame and I am checking to see if the current tick is greater than the total time of the animation to see whether to have my timeline scrubber continue to increment.

    aewuAuthor
    Inspiring
    February 22, 2017

    I'm going to say again that doing it that way is wrong. Incrementing the position of the slider every tick is an incredibly brittle solution that only works when the timeline is playing. Stopping, pausing, or rewinding the timeline completely breaks it. A proper solution that doesn't have any of those problems would look more like this:

    function handleTick() {

        sliderBtn.x = bounds.x + bounds.width * (exportRoot.currentFrame / exportRoot.totalFrames);

    }

    The sort of thing you're trying to do is the very reason the currentFrame property exists, so people don't have to resort to ugly hacks to sync code-based animation with the timeline.

    Also, there's no reason to include setFPS in your initialization function, since the FPS is already automatically set to the stage frame rate. And I'm pretty sure "useTicks = true" doesn't do anything. There is no global variable "useTicks".


    This works well. Thank you! Also deleting the useTicks and setFPS resulted in no change, so you are right about that too. Thank you for your help and patience.