As it says, in Phaser.js how can you apply an action for each object inside a group. I want to apply the following lines to each item:
game.physics.arcade.collide(something, platforms);
game.physics.arcade.overlap(player, something, gameOver, null, this);
something.body.velocity.x = -120;
"Something" is the object name but my group name is called "obstacleGroup". I want to do this as I have another function making new objects to the group all the time so I don't necessarily know what they are being called.
As it says, in Phaser.js how can you apply an action for each object inside a group. I want to apply the following lines to each item:
game.physics.arcade.collide(something, platforms);
game.physics.arcade.overlap(player, something, gameOver, null, this);
something.body.velocity.x = -120;
"Something" is the object name but my group name is called "obstacleGroup". I want to do this as I have another function making new objects to the group all the time so I don't necessarily know what they are being called.
Share Improve this question asked Jun 22, 2014 at 21:30 Andrew HowardAndrew Howard 3,0826 gold badges40 silver badges71 bronze badges2 Answers
Reset to default 12Group.forEach is one such iteration method you could use (as @imcg pointed out before me). However you're using it for Arcade Physics collision, and that can take a Group as the parameters. So you could collide everything in your Group vs. platforms with just:
game.physics.arcade.collide(obstacleGroup, platforms);
Once, in your update loop.
Same works for overlap
.
You can use Group.forEach to iterate the objects in the group and call a function on them:
obstacleGroup.forEach(function(item) {
game.physics.arcade.collide(item, platforms);
game.physics.arcade.overlap(player, item, gameOver);
item.body.velocity.x = -120;
}, this);