最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apply an action to every object in group for Phaser js - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 12

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);
发布评论

评论列表(0)

  1. 暂无评论