Jump to content

Group.callAll pass item as argument?


Ansis100
 Share

Recommended Posts

I have a user-defined function which takes an enemy as an argument (inside a prototype):

attack: function () {
    enemy.damage(10);
}

In the update:

this.enemies.callAll(this.attack(), null, enemy);  

'enemy' being the child from the group the function is being called on. I can't just use forEach because then, for example, if I attack 2 enemies at once, they don't register. (At least I presume that's what happens, to be honest, I just have no idea why, in a similiar but more complicated setup, the only child that's being damaged is the first one.) I want the function to be called on all group children at the same time.

Edited by Ansis100
Accidentally posted
Link to comment
Share on other sites

Hey,

As far as I know according to the docs http://phaser.io/docs/2.6.2/Phaser.Group.html#callAll first argument of callAll is method name as string to be called on child object, second parameter is context where null means that this will refer to current child and then go the additional parameters

function Enemy(){
   ///...
}

Enemy.prototype.attackedByPlayer = function (player){
    player.attack(this);
};

function Player(){
   ///...
}

Player.prototype.attack = function (enemy){
   enemy.damage(10);
};

and in the update you can do:

this.enemies.callAll('attackedByPlayer', null, player);

Please some1 correct me if Im wrong ;)

--

Best regards,

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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