Apply action to each object in the group for Phaser js

As they say, in Phaser.js how you can apply an action to each object within a group. I want to apply the following lines to each element:

game.physics.arcade.collide(something, platforms);
game.physics.arcade.overlap(player, something, gameOver, null, this);
something.body.velocity.x = -120;

“Something” is the name of the object, but my group name is called “obstruction”. I want to do this because I have another function that constantly creates new objects for the group, so I don’t necessarily know that they are called.

+4
source share
2 answers

You can use Group.forEach to iterate objects in a 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);
+9
source

Group.forEach - , ( @imcg ). Arcade Physics, . , :

game.physics.arcade.collide(obstacleGroup, platforms);

.

overlap.

+11

Source: https://habr.com/ru/post/1545592/


All Articles