samjarman Posted June 25, 2014 Share Posted June 25, 2014 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 More sharing options...
lewster32 Posted June 26, 2014 Share Posted June 26, 2014 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 }}); samjarman 1 Link to comment Share on other sites More sharing options...
samjarman Posted June 27, 2014 Author Share Posted June 27, 2014 Perfect, thanks! But how then would I get to birdgroup, if I didn't want to have a global reference to it between files? Is there method on game like game.getGroup(1) or something? Link to comment Share on other sites More sharing options...
lewster32 Posted June 27, 2014 Share Posted June 27, 2014 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 More sharing options...
Recommended Posts