Jump to content

callAll for functions and events


Rocco
 Share

Recommended Posts

it doesn't work the way i tried it, can someone help me with this?

 

here is my attempt on calling this two things for the whole group:

// it works for a single entitystone.anchor.set(0.5);stone.events.onInputDown.add(this.stone_rotate, this);// but not for the group as i tried       this.stones.callAll('anchor.set','', 0.5);this.stones.callAll('events.onInputDown.add','',this.stone_rotate(this));
Link to comment
Share on other sites

* @method Phaser.Group#callAll
* @param {string} method - A string containing the name of the function that will be called. The function must exist on the child.
* @param {string} [context=null] - A string containing the context under which the method will be executed. Set to null to default to the child.
* @param {...*} parameter - Additional parameters that will be passed to the method.

 

As far as i see it's ok, if you pass no 1 param (method), then callAll returns without doing anything,

but with my approach it should stick with the context to default setting, which should be ok,

and then the additional parameters should be passed as functionparams.

 

thx for your suggestion, but it doesn't work either.

Link to comment
Share on other sites

As far as I got so far from my own experience, that will work only with functions that are directly children of these objects. For a function that is a child of a child, that won't work.

So I've done something similar this way:

stones.forEach(function (item) { item.anchor.set(0.5); }, this);
Link to comment
Share on other sites

thank you both, both your suggestions work well for the anchor setting. :)

but this one is a tough one i guess:

this.stones.callAll('events.onInputDown.add','events',this.stone_rotate(this));

fortunatly i can do a workaround and set this during placing, but i'm curious how it would work with callAll or forEach

Link to comment
Share on other sites

Hard to say how it would work if we can't see stone_rotate.
It looks like you are trying to get it to call stone_rotate on input down, but what you are actually doing is passing in the return value of stone_rotate, which may very well be null.

If this is the case you may want to try:

stones.callAll('events.onInputDown.add','events.onInputDown',function(){this.stone_rotate(this)});
Link to comment
Share on other sites

From my pov it's totally the same, either 

stones.forEach(function (item) { item.events.onInputDown.add(this.stone_rotate, this); }, this);

or

stones.callAll('events.onInputDown.add', 'events.onInputDown', this.stone_rotate(this));

though I didn't really try either one.

 

wow the first one works, thank you very much, i tried it false and did'nt get it to run.

 

the second one throws this error -> Uncaught Error: listener is a required param of add() and should be a Function.

Link to comment
Share on other sites

 yes thank you very much, i'm newbie and learning a lot from this examples.

 

Hard to say how it would work if we can't see stone_rotate.
It looks like you are trying to get it to call stone_rotate on input down, but what you are actually doing is passing in the return value of stone_rotate, which may very well be null.

If this is the case you may want to try:

stones.callAll('events.onInputDown.add','events.onInputDown',function(){this.stone_rotate(this)});

 

i get this error with it -> Uncaught TypeError: Object [object global] has no method 'stone_rotate'

 

at the moment stone_rotate doesn't do much except rotating (will expand later), so it looks like this:

    stone_rotate: function(s) {        s.angle +=90;       // this.text.text = "Stone ID is " + s.id_ + "\nAngle is " + s.angle + "!";    },
Link to comment
Share on other sites

basically group.callAll is also working for me, but what i want to achieve is to make this single call (what works very well):

stone.events.onInputDown.add(this.stone_rotate, this);

to run on the whole group of stones with callAll and this is the difficulty.

Link to comment
Share on other sites

I see.

The reason :

stones.callAll('events.onInputDown.add','events.onInputDown',function(){this.stone_rotate(this)});

doesn't work is because you need a reference to the stone in order to rotate it. You don't get the same "this" as in foreach. I think a good rule of thumb would be to use callAll when you just need to pass literals, and forEach whenever you need to access values for each individual child. I'm curious if there are any objective advantages to callAll, but you'll save grief and have more readable code if you ignore it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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