Jump to content

OnComplete with tween(sprite.scale)


kalel
 Share

Recommended Posts

eat_candy: function(player, candy){  if(candy.flag){    candy.flag = false;    console.log("candy eaten");    this.score += candy.scale.x*10;    this.label_score.content = this.score;    this.add.tween(candy.scale)      .to({ x: 0, y: 0}, 2000, Phaser.Easing.Linear.None)      .start()      .onComplete.add(this.kill_candy, this);   }},kill_candy: function(candy){  console.log(candy);  candy.kill();  console.log("killed");},

Hello,

 

It's my first post in this forum, I usually read the answer to resolve my problems but this time I didn't find anything about my problem.

 

When using tween(candy.scale) to tween the scale of my sprite, I'm not able to get my candy object on the function kill_candy() called by onComplete.add()

 

My console.log(candy) return me a Phaser.point object and not a Phaser.sprite and I cannot call kill() on it

Is it possible to get the candy object in the kill_candy() function ?

 

Thank you

Link to comment
Share on other sites

Hi,

you have at least two possibilities.

 

1. Pass reference of candy and not 'this' when you add the callback.

eat_candy: function(player, candy){  if(candy.flag){    //...    this.add.tween(candy.scale)      .to({ x: 0, y: 0}, 2000, Phaser.Easing.Linear.None)      .start()      //.onComplete.add(this.kill_candy, this);      //pass candy not this      .onComplete.add(this.kill_candy, candy);   }},kill_candy: function(point){  console.log(this); //this points to candy as you created the callback with a reference of candy and not this (which would be the state, game or whatever)  this.kill();  console.log("killed");}, 

By the way: onComplete calls your registered callback method with the tweened object as the only parameter. In your case this is the scale object (candy.scale) which is a Phaser.Point instance. That's the reason why you do not get the object reference itself passed.

 

2. I guess Candy is s simple Phaser.Sprite ? You could create your own class inherited from Phaser.Sprite

This class would always know about itself and you can happily kill or do whatever you want in the callback.

Example for such a class:

//Class definitionCandy = function(game,x,y){  Phaser.Sprite.call(this, game,0,0, 'balls', 0);}Candy.prototype = Object.create(Phaser.Sprite.prototype);Candy.prototype.constructor = Candy;//define two methods eat and the callback (afterEat)Candy.prototype.eat= function(){  tween = this.game.add.tween(this.scale).to( { x:0, y:0 }, 1500)  tween.onComplete.add(this.afterEat, this)  tween.start()}Candy.prototype.afterEat= function(){  console.log('i've been eaten', this) //this points to the instance of candy    //now you could trigger events or signals to notify the outer world  //that you have been eaten.}//Later in your code:candy = new Candy(game)game.world.add(candy) //or another groupc.eat()
Link to comment
Share on other sites

Thank you for your answer, I found another method before seeing your response

var a = this.add.tween(candy.scale)			.to({ x: 0, y: 0}, 2000, Phaser.Easing.Linear.None)			.start();	        a.onComplete.add(function(){candy.kill(); console.log("killed");});

Calling the candy.kill() directly in a function inside onComplete.add worked for me

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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