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.
Group.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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With