Sign in to follow this  
Julia

Looping over group indices

Recommended Posts

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  :mellow:

So right now I'm just doing this: 

if (!enemyGroup.getAt(0).alive) {this.managePause1();}if (!enemyGroup.getAt(1).alive) {this.managePause2();}

etc.  :D 

Thanks in advance!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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#forEachAlive

enemies.forEachAlive(function(enemy) {...}, this)

Share this post


Link to post
Share on other sites

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!  ^_^

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.