Jump to content

Destroy after tween completes doesn't work for arrays


bymafmaf
 Share

Recommended Posts

Hi,

 

I'm trying to destroy some elements of an array which consists of sprites. However, I don't want them just disappear but with some tween effect. Here is the code:

if (Kare.gameBoard[i][j] != null){                    var tween = Kare.gameBoard[i][j].game.add.tween(Kare.gameBoard[i][j].scale).to({ x: 0.1, y: 0.1}, 1000, Phaser.Easing.Back.Out, true);                    tween.onComplete.add(function () {                        Kare.gameBoard[i][j].destroy();                        Kare.gameBoard[i][j]= null;                    });                                    }

This doesn't work quite as I expected because (I think) after "var tween" line it adds oncomplete function but it doesn't call it just after tween completes. Only adds it and after a while it tries to call it but i and j variables are lost already.

 

I tried defining a variable for Kare.gameBoard[j] and sending it to the function as parameter etc but did not seem to work.

 

I only want that sprite should scale to really small point and then destroy.

Link to comment
Share on other sites

try this

 

[update: ignore, broken :unsure: ]

tween.onComplete.add(function (i2, j2) {  // you could just use (i,j) here    Kare.gameBoard[i2][j2].destroy();     // you could just use [i][j] here, but note they are local variables ie not the same i,j in your original loop    Kare.gameBoard[i2][j2]= null;         // hence why i have used a separate variable name to clarify}, this, null, i, j); // null is priority argument ... arguments at the end pass original i,j into callback function arguments i2,j2 

 

but actually you can simplify it

tween.onComplete.add(function (obj) {  obj.destroy();  obj = null;}, this);

personally i'd do it like this

tween.onComplete.add(onTweenComplete, this)function onTweenComplete(obj) {  obj.destroy();  obj = null;}
Link to comment
Share on other sites

ok sorry about that I got the arguments wrong.. it must be the way you are calling the tween, that mean obj does not refer to the item. your syntax looks a bit odd

 

here is my (TypeScript) code.. 

var tween = this.game.add.tween(this.heroGroup);tween.to({y: this.heroGroup.y+36}, 500, Phaser.Easing.Elastic.Out , true,0, 0,false);tween.onComplete.add(this.shoveDownComplete, this, null, "hello", "world");shoveDownComplete(obj, tween, hello, world) {    obj.y=0    console.log(hello) // "hello"    console.log(world) // "world"}

so you should maybe use

tween.onComplete.add(function (obj, tween, i2, j2) { // you could just use (i,j) here  Kare.gameBoard[i2][j2].destroy(); // you could just use [i][j] here, but note they are local variables ie not the same i,j in your original loop  Kare.gameBoard[i2][j2]= null; // hence why i have used a separate variable name to clarify}, this, null, i, j); // null is priority argument ... arguments at the end pass original i,j into callback function arguments i2,j2 

also can you not just do 

var tween = game.add.tween(Kare.gameBoard[i][j]......

?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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