Jump to content

Tween -> run function -> tween


Terumi
 Share

Recommended Posts

I don't think you can pass parameter to the function but you could use a simple variable if you simply want to modify it whitin the same scope if not you can make it global.

//variablevar x = -100;//if you need it in a complete different scope you can also make it global likewindow.globalstorage = {};globalstorage.x = -100;//tween out of stagevar tween = game.add.tween(object);tween.to({x: 1000}, 2000);tween.onComplete.add(foobar, this);tween.start();function foobar() {//teleport somewhereobject.x = x;//or if you need globalobject.x = globalstorage.x;//tween back to stagevar two = game.add.tween(object);two.to({x: 300}, 2000);two.start();}
Link to comment
Share on other sites

Yeah, that could be done that way but it looks cubersome if you want for example to animate multiple properties or something.

Wouldn't be nice to have the tween accept an object for the parameters you want to use in the callback functions?

 

something like

 

tween.onComplete.add(foobar, this, {obj: mySprite, var1: val1, var2: val2});

 

and then, on the foobar function

 

function foobar(attrs){

var obj = attrs.obj;

var myCustomValue = attrs.val1

etc,,,

}

 

That would be very helpful I think.

Link to comment
Share on other sites

Terumi: you nearly had the solution.

 

When you call 

tween.onComplete.add(foobar, {obj: mySprite, var1: val1, var2: val2});

you are passing an object as the scope for "this" in the event.

So inside the method foobar, you will be able to access this.objthis.var1this.var2, and so on.

There is no need to encapsulate anything in an additional method.

 

To wrap it up, here's poweroffear's code - slightly modified to access your custom properties from the callback function:

//tween out of stagevar tween = game.add.tween(object);tween.to({x: 1000}, 2000);tween.onComplete.add(foobar, {obj: object, targetX: 10, targetY: 10});tween.start();function foobar() {    //teleport somewhere    this.obj.x = this.targetX;    this.obj.y = this.targetY;}
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...