bymafmaf Posted October 29, 2015 Share Posted October 29, 2015 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 More sharing options...
jmp909 Posted October 29, 2015 Share Posted October 29, 2015 try this [update: ignore, broken ] 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 ittween.onComplete.add(function (obj) { obj.destroy(); obj = null;}, this);personally i'd do it like thistween.onComplete.add(onTweenComplete, this)function onTweenComplete(obj) { obj.destroy(); obj = null;} Link to comment Share on other sites More sharing options...
bymafmaf Posted October 29, 2015 Author Share Posted October 29, 2015 First code didn't work, in google developer tools it says i2 is a Phaser Pointer and j2 is a tween, i don't know why. The other two I had already tried, doesn't work. Link to comment Share on other sites More sharing options...
jmp909 Posted October 29, 2015 Share Posted October 29, 2015 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 usetween.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 More sharing options...
bymafmaf Posted October 30, 2015 Author Share Posted October 30, 2015 Yeah, it worked. So, it was all about order of parameters. Thanks for suggestion Link to comment Share on other sites More sharing options...
Recommended Posts