CreateJS / TweenJS

A simple but powerful tweening / animation library for Javascript. Part of the CreateJS suite of libraries.
http://createjs.com/
MIT License
3.56k stars 967 forks source link

Tween._inited does not reset if scene is being destroyed or removed inside the game and rebuild #92

Open simongcc opened 7 years ago

simongcc commented 7 years ago

I have written a game using easeljs + preloadjs + soundjs + tweenjs. The scene order like this: I used the same canvas for different scene. Promo Scene -> Start Game Scene Every time finishing the game, the restart button will go to Promo Scene again.

I found that probably(I haven't dig deep enough to find out) I use stage.clear(), stage.removeAllEventListeners() and so on. The Tween ticker is somehow being removed from the scene. So, first run, everything is fine, afterward, the Tween does not run unless I set Tween._inited = false before anything start again. Since the big Tween is supposed to be added once as tick function according to the source code logic.

I add this in restart game logic createjs.Tween._inited = false; as workaround. Since_inited is undocumented due to it is an internal property.

I think it is an issue for restarting everything, in the above situation, but not necessarily a bug.

if (!Tween._inited && createjs.Ticker) {
    console.log("init Tween");
    createjs.Ticker.addEventListener("tick", Tween); Tween._inited = true; 
}
spassvogel commented 7 years ago

I have been dealing with the same issue. This is what happens:

  1. Our game is over, so we destroy everything and call Ticker.reset
  2. All 'tick' event listeners are removed BUT Tween._inited is still true
  3. When we start a new game and try to create a new tween, Tween thinks it's initialized but there is no 'tick' listener on Ticker.

@simongcc has a solution, I have solved it differently, by explicitly setting Tween._inited = false when we destroy our game, because registering a new tween automatically adds the 'tick' listener if _inited is false.

if (!Tween._inited && createjs.Ticker) { createjs.Ticker.addEventListener("tick", Tween); Tween._inited = true; }

However, setting protected properties like that is not recommended practice. I would humbly request the devs to create a public Tween.reset() or something similar.