Jump to content

Get back objects with their key - and maybe regex?


samjarman
 Share

Recommended Posts

So I have a sprites of birds flying around in my game, and I'd like to perform an operation over all of them and conditionally do things based on their x co-ord. 

 

Can I:

1) Get out objects with their key after adding them?

2) Get out groups of objects with a key and a regex (eg starting with 'bird_')?

 

 

Or is there a more JS way or Phaser way to do this? (Very new to both) Maybe with groups?

 

Thanks!

 

 

Link to comment
Share on other sites

Add all of your birds to a Group, and then use group.forEach or one of its more specific versions (forEachAlive etc):

var birdGroup = game.add.group();for (var i = 0; i < 10; i++) {  // create birds at random locations with a key like 'bird_1', 'bird_3' etc  birdGroup.create(game.world.randomX, game.world.randomY, 'bird_' + game.rnd.integerInRange(1, 5));}birdGroup.forEach(function(bird) {  console.log(bird.key); // output the bird's key  if (bird.x < 100) {    // do something to this bird if its x position is below 100  }});
Link to comment
Share on other sites

If you're using states, you'd use 'this' within your state functions and simply attach the reference to your states. You could also attach it to the game object itself if you want to reference it between multiple states. How you architecture your game is entirely up to you - Phaser doesn't place any restrictions on coding style.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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