A callback function to call after the moving of a sprite

I can move a sprite using code similar to this:

var actionTo = new cc.MoveTo(0.3, cc.p(
    player_position[0] * TILE_SIZE,
    win_size.height - player_position[1] * TILE_SIZE
));
player_sprite.runAction(new cc.Sequence(actionTo));

Is it possible to have a callback function which will be called after the movement is finished?

Just add a CallFunc into the sequence. I don’t know the exact syntax, but something like this:

var callback = new cc.CallFunc(myCallbackFunc);
player_sprite.runAction(new cc.Sequence(actionTo, callback));

Thanks, @stevetranby. Yes, that works.

By the way, is there a default easing defined for MoveTo action? I would like to move it without easing at equal intervals.

You’ll need to wrap in an Ease action (EaseIn, EaseOut, etc)

i have tried doing like this… but its not workin for me… please help…

var func=new cc.CallFunc(this.move());
this.sprite.runAction(cc.Sequence(cc.moveTo(2, 100, 200)),func);
var a = 0;

“inside move function”

move:function(){
if (a==0) {
this.sprite.runAction(cc.rotateTo(2, 90, 90));
}

1 Like

You need to pass the target object as well. In your case that would be “this”. Here is an example:

this.mysprite.runAction(
   cc.sequence(
      cc.fadeIn(0.1),
      cc.sequence(
         cc.scaleTo(0.2,1.1),
         cc.scaleTo(0.2,1)
      ).repeat(5),
      cc.callFunc(this.done, this)
   )
);

done: function() {
    cc.log("Done!");
    this.mysprite....
}

You may also use the sprite itself as the target object, so in the called function “this” will already be mysprite. Especially useful if you used a local variable for the sprite, like:

cc.callFunc(mysprite.removeFromParent, mysprite)

And sometimes you may also want to pass another parameter to the function:

cc.callFunc(this.done, this, param)

Just note that in this case you will get two paramters passed to the function “this” and “param”, so your function must take both like:

done: function(self, param) { ... }
2 Likes

thanks for the reply… i tried doing your example but it is showing errors…
error: wrong number of arguments: 2, was expecting 1
i need to make the function call after the movement…is there any way to do this??

1 Like

Try this

//moveTo(duration, position)
var actionMoveTo = cc.moveTo(2, cc.p(80, 80));
var actionCallFunc = cc.callFunc(this.moveDone, this);
this.sprite.runAction(cc.sequence(actionMoveTo, actionCallFunc));

moveDone : function()
{
//do your thing here
}
2 Likes

thanks for the reply … it works well… :grinning:

1 Like

can this method used to call a function after completing a function… like for example…

             var action=cc.moveBy(2,cc.p(100, 200));
            var callfunc=cc.callFunc(this.move,this);
            var callfuncro=cc.callFunc(this.rotate,this);
        	this.sprite.runAction(cc.sequence(action,callfunc,callfuncro));

move:function(){
cc.log(“hndasjkd”);
this.sprite.runAction(cc.rotateTo(2, 90, 90));
},
rotate:function(){
cc.log(“4646”)
this.sprite.runAction(cc.moveTo(2, 300, 66));
}

please help… i tried this code but both the fuctions are working at the same time…

1 Like

The sequence will not wait the function to complete as it does not know when it complete.

So, you have to delay it.

var callBackDelay = cc.delayTime(2.0);
this.sprite.runAction(cc.sequence(action,callfunc,callBackDelay, callfuncro));
2 Likes

i have tried using delay before and worked,i just wanted to know if there exist any other method rather than delay to make a function call after finishing a primary function… anyway thanks for the reply…

1 Like

If you don’t want to use delay, then i think you should use sequence in the callback function.

move:function()
{
var callfuncro=cc.callFunc(this.rotate,this);
this.sprite.runAction(cc.sequence(cc.rotateTo(2, 90, 90), ), callfuncro);
},

rotate:function()
{
this.sprite.runAction(cc.moveTo(2, 300, 66));
}
2 Likes

thanks a lot!!.. let me see if this works out :smile: