Julia Posted November 6, 2014 Share Posted November 6, 2014 Hey!I was wondering how I could loop over indices of a phaser group with sprites. The order of the sprites is important, because I would like to add a different 'pause screen' when an enemy is caught. In that pause screen I would like to show a certain piece of text which corresponds to the index of the enemy caught (they are 10 vows of our company). Currently I'm just listing them all seperated, but that's of course not the best way to do it. I'm still learning a lot, I find loops and getting my code clear not the easiest things So right now I'm just doing this: if (!enemyGroup.getAt(0).alive) {this.managePause1();}if (!enemyGroup.getAt(1).alive) {this.managePause2();}etc. Thanks in advance! Link to comment Share on other sites More sharing options...
lewster32 Posted November 6, 2014 Share Posted November 6, 2014 Each group has a .children property which is just an array of the objects inside the group, so you can treat this just like a normal array:for (var i = 0, len = group.children.length; i < len; i++) { console.log(group.children[i]);}Also, the .z property of each sprite corresponds to that sprite's index in the group for easy reference. WombatTurkey 1 Link to comment Share on other sites More sharing options...
fariazz Posted November 7, 2014 Share Posted November 7, 2014 Groups have also a forEach method that you can use to iterate their members: http://docs.phaser.io/Phaser.Group.html#forEach:enemies.forEach(function(enemy) { if(enemy.blablabla) { //etc }}, this);In your particular case it seems you are looking for the sprites that are alive, there is a method to iterate through the alive members of a group http://docs.phaser.io/Phaser.Group.html#forEachAliveenemies.forEachAlive(function(enemy) {...}, this) lewster32 and tidelake 2 Link to comment Share on other sites More sharing options...
Julia Posted November 7, 2014 Author Share Posted November 7, 2014 Thanks for your answers It's slowly all getting a little bit clearer.I was just trying to get a certain piece of text (a vow), which I've put in a phaser group, to correspond with the index of the enemy who got caught. So caught enemy 1 = show vow 1, caught enemy 2 = show vow 2.Now I've got this code: In my collisionHandler:for (var i = 0; i < enemyGroup.children.length; i++) { if (!enemyGroup.children[i].alive) { this.managePause(); }}In my managePause: game.paused = true;graphics = game.add.graphics(x, y);var x = 0;var y = 0;graphics.beginFill(000000, 0.5);graphics.drawRect(x, y, 1200, 600);graphics.fixedToCamera = true;vowGroup = game.add.group();vowGroup.fixedToCamera = true;backButton = game.add.button (600, 500, 'button-resume', this.goBack, this);backButton.anchor.setTo(0.5, 0.5);backButton.fixedToCamera = true;text1 = game.add.text(600, 300, "vow1", fontStyle, vowGroup),text2 = game.add.text(600, 300, "vow2", fontStyle, vowGroup),etc.for (var i = 0; i < vowGroup.length; i++) { vowGroup[i];}But the problem is, I don't know how to show only one vow from the vowGroup, corresponding with the index of the caught enemy. Now my code is showing all vows at the same time. Any idea how to fix this?Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts