Jump to content

Why can't I call start on a tween object twice?


denisb
 Share

Recommended Posts

Lets say I have this code:

var mySprite=this.add.sprite('mySprite');

mySprite.alpha=0;

var mySpriteTween=this.add.tween(mySprite);

mySpriteTween.to({alpha:1},1000,'Linear',false,0,0,true);

mySpriteTween.start();

mySpriteTween.start();

When I call start() the second time the tween does not run even though I have yoyo set to true and thus the alpha of mySprite has been tweened back to zero.

Does the Tween Manager clear the TweenData object owned by mySpriteTween after it has completed?

 

Link to comment
Share on other sites

34 minutes ago, denisb said:

Lets say I have this code:

var mySprite=this.add.sprite('mySprite');

mySprite.alpha=0;

var mySpriteTween=this.add.tween(mySprite);

mySpriteTween.to({alpha:1},1000,'Linear',false,0,0,true);

mySpriteTween.start();

mySpriteTween.start();

When I call start() the second time the tween does not run even though I have yoyo set to true and thus the alpha of mySprite has been tweened back to zero.

Does the Tween Manager clear the TweenData object owned by mySpriteTween after it has completed?

 

I think tween is cleared when its done. you set a timer loop to call the tween many times. 

Link to comment
Share on other sites

Hi, you are setting yoyo tween. Your number of repeats is 0, which means it will run once. As it is yoyo tween it will first tween alpha from 0 to 1 and then yoyo part starts and tweens alpha from 1 back to 0.

In your code, you are calling start twice without any delay or time to finish your 2 seconds (2 x 1000 - there and yoyo back) tween. If you call start on running tween it is ignored (see https://github.com/photonstorm/phaser/blob/master/src/tween/Tween.js#L296)

 

 In case you first run your tween and then somewhere in code you need to start it again (after it already ended), you may encounter problem with Phaser versions. Up to 2.4.4 yoyo tweens were not reusable. From 2.4.5 this issue (https://github.com/photonstorm/phaser/issues/2307) was solved and yoyo tweens are reusable now.

 

 And finally, there is also issue if you call stop on running tween and want to start it agian - it will not work. Tweens stopped with stop are marked as "to be deleted". Tween has internal property "pendingDelete", which is set to true and then the tween is deleted. Setting pendingDelete to true by hand seemed as solution (see discussion in "Restart tween" thread below). But, it has some drawbacks regarding tween running time (tween can be updated multiple times in one frame so it is running faster) :

 Recently I found correct way how to stop and reuse tween. Code is typescript and it adjusts Phaser engine during runtime - you need to call it in the beginning of game:

        public static AdjustTweenFunctions(): void {

            Phaser.TweenManager.prototype["removeFromUpdateQueue"] = function (aTween: Phaser.Tween) {
                var i = this["_tweens"].indexOf(aTween);

                if (i !== -1) {
                    this["_tweens"].splice(i, 1);
                }
                else {
                    i = this["_add"].indexOf(aTween);

                    if (i !== -1) {
                        this["_add"].splice(i, 1);
                    }
                }
            }

            Phaser.Tween.prototype["stopAndRemoveFromUpdateQueue"] = function (aComplete: boolean): Phaser.Tween {
                var self = <Phaser.Tween>this;
                var result = self.stop(aComplete);
                self.manager["removeFromUpdateQueue"](self);
                self.pendingDelete = false;
                return result;
            }
        }

 Then, during game, if you want to stop your running tween and keep it for further restart you can do it like this:

        // -------------------------------------------------------------------------
        private stopLandingTween(): void {
            if (this._landTween.isRunning) {
                this._landTween["stopAndRemoveFromUpdateQueue"]();
            }
        }

 Important part is to remove tween from update queue immediately.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...