Jump to content

Cannot remove a single Tween in v2.0.5


Dev Monster
 Share

Recommended Posts

I have been trying to remove a single tween from a game after adding it but cannot seem to remove it no matter what I do.

 

I add my tween like this:

 

// yoyo tween for ball movement
this.idleBallTween = this.game.add.tween(ball).to({y: -50}, 500 , Phaser.Easing.Sinusoidal.InOut, true, 0, 9999, true);

 

 

Then I tried to remove it with all the methods below but it never gets removed and generates an error:

 

------------------------------------------------------------------------------------------------------

this.game.remove(this.idleBallTween);    (from http://docs.phaser.io/Phaser.TweenManager.html#remove)

TypeError: this.game.remove is not a function

 

------------------------------------------------------------------------------------------------------

this.game.removeTween(this.idleBallTween);

TypeError: this.game.removeTween is not a function   (from previous versions of Phaser)

 

------------------------------------------------------------------------------------------------------

this.idleBallTween.isRunning = false;
this.idleBallTween.pendingDelete = true; // always force pendingDelete
this.tweens.remove(this.idleBallTween);

 

NO ERROR but tween is not removed

 

------------------------------------------------------------------------------------------------------

this.idleBallTween.isRunning = false;
this.idleBallTween.pendingDelete = true; // always force pendingDelete
this.tweens.remove(this.idleBallTween);
this.idleBallTween = null;
this.tweens = []; (tried to empty the entire tweens array and it worked, showed no tweens at all in the console, but the tween is still running)

 

NO ERROR but tween is not removed and still plays

 

------------------------------------------------------------------------------------------------------

this.game.tweens.remove(this.idleBallTween);

 

NO ERROR but tween is not removed

 

 

Can somebody please help with this? What am I doing wrong?

 

Thanks

Link to comment
Share on other sites

game.tweens.remove(tween) should work. I've created a jsfiddle to test: http://jsfiddle.net/lewster32/3Sx5h/ - it's running 2.0.4 but I don't think any tweening stuff changed.

 

It looks like maybe a scope issue - maybe check to see if 'this' is what it should be within the context of where you're adding and removing the tween.

Link to comment
Share on other sites

@wayfinder

 

I tried stop() and but the tween did not stop...I am also trying to remove it completley from the project.

 

 

game.tweens.remove(tween) should work. I've created a jsfiddle to test: http://jsfiddle.net/lewster32/3Sx5h/ - it's running 2.0.4 but I don't think any tweening stuff changed.

 

It looks like maybe a scope issue - maybe check to see if 'this' is what it should be within the context of where you're adding and removing the tween.

 

I did a version on top of your jsfiddle example (Im not using TypeScript so I had to change it). Its located here: http://jsfiddle.net/3Sx5h/9/

 

It works fine in 2.0.4 the way you described and with my modified code how Im using it (non TypeScript). There is no cdnjs.cloudflare source for 2.0.5 so I am not able to test using 2.0.5 but in my project, it no longer works to stop the tween using the methods that we put in jsfiddle.

 

It would be interesting to see a test in 2.0.5 to see if its broken.

Link to comment
Share on other sites

My version wasn't done in TypeScript, just plain ol' JavaScript! I've updated it to use the version of Phaser from examples.phaser.io (2.0.5 - latest) and it still works as expected: http://jsfiddle.net/lewster32/3Sx5h/

 

Be careful with scope - in your changes you've initialised idleBallTween on line 10 without var, which makes it global (accessible outside of all of your states) - you should really just do this.idleBallTween on line 21 and reference it as such from then on, as 'this' then belongs to the state (Boot).

Link to comment
Share on other sites

My version wasn't done in TypeScript, just plain ol' JavaScript! I've updated it to use the version of Phaser from examples.phaser.io (2.0.5 - latest) and it still works as expected: http://jsfiddle.net/lewster32/3Sx5h/

 

Be careful with scope - in your changes you've initialised idleBallTween on line 10 without var, which makes it global (accessible outside of all of your states) - you should really just do this.idleBallTween on line 21 and reference it as such from then on, as 'this' then belongs to the state (Boot).

 

Ok...thats good to know. I have been creating alot of vars like that and maybe thats why I cannot access them. I have also done it like this:

 

BasicGame.Boot = function (game) {

    this.idleBallTween = null;

};

 

That would also scope the idleBallTween to the state and not globally right?

Link to comment
Share on other sites

Yes, but you don't need to generally initialise state properties in advance - if you look at the example, I've altered it to remove this line and it still works fine. 

 

If you're ever in doubt about the value of 'this', just do console.log(this) and see what you get. If you're still not sure, set a unique property like this.test = "blah" and then try to access it again elsewhere with console.log(this.test) - just little ways of trying to get to grips with scope, which is not always easy with the various different styles of writing JavaScript.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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