Rocco Posted March 27, 2014 Share Posted March 27, 2014 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 More sharing options...
drhayes Posted March 27, 2014 Share Posted March 27, 2014 Try passing in the context as the second argument?this.stones.callAll('anchor.set', this.stones, 0.5);If you pass in an empty string, callAll returns without doing anything: https://github.com/photonstorm/phaser/blob/master/src/core/Group.js#L913 Link to comment Share on other sites More sharing options...
Rocco Posted March 27, 2014 Author Share Posted March 27, 2014 * @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 More sharing options...
localGhost Posted March 27, 2014 Share Posted March 27, 2014 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); Rocco 1 Link to comment Share on other sites More sharing options...
adamyall Posted March 27, 2014 Share Posted March 27, 2014 This works, but it's definitely not intuitive: stars.callAll('anchor.set','anchor',.5) Rocco 1 Link to comment Share on other sites More sharing options...
Rocco Posted March 27, 2014 Author Share Posted March 27, 2014 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 More sharing options...
localGhost Posted March 27, 2014 Share Posted March 27, 2014 From my pov it's totally the same, either stones.forEach(function (item) { item.events.onInputDown.add(this.stone_rotate, this); }, this);orstones.callAll('events.onInputDown.add', 'events.onInputDown', this.stone_rotate(this));though I didn't really try either one. Rocco 1 Link to comment Share on other sites More sharing options...
adamyall Posted March 27, 2014 Share Posted March 27, 2014 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 More sharing options...
Rocco Posted March 27, 2014 Author Share Posted March 27, 2014 From my pov it's totally the same, either stones.forEach(function (item) { item.events.onInputDown.add(this.stone_rotate, this); }, this);orstones.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 More sharing options...
adamyall Posted March 27, 2014 Share Posted March 27, 2014 I think my previous post may clear up why the second approach did not work. Please try it out and let me know if that works (for posterity). Link to comment Share on other sites More sharing options...
Rocco Posted March 27, 2014 Author Share Posted March 27, 2014 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 More sharing options...
JP91 Posted March 27, 2014 Share Posted March 27, 2014 ehhh group.callAll is working well for me, can you show more code ? Link to comment Share on other sites More sharing options...
Rocco Posted March 27, 2014 Author Share Posted March 27, 2014 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 More sharing options...
JP91 Posted March 27, 2014 Share Posted March 27, 2014 mmmm .. now I can not put code here but check the example in phaser-examples, group/callAll it would be the same for a group Link to comment Share on other sites More sharing options...
adamyall Posted March 27, 2014 Share Posted March 27, 2014 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. Rocco 1 Link to comment Share on other sites More sharing options...
Recommended Posts