Jump to content

Get random dead sprite from a group


hellspawn_bg
 Share

Recommended Posts

There's Group#getFirstDead which will get the first one - not sure why you'd need to get a random dead sprite since I would assume it doesn't matter if they're dead, but if you want it to pick a random one you'd do something like this:

Phaser.Math.getRandom(group.children.filter(function(e) {  return !e.alive;}));
Link to comment
Share on other sites

I thought about it when creating enemy pool in my current game. And with this approach you are creating more sprites in pool. Currently, to have control about amount of each sprite I'm using non standard random distribution. So when I decide that at any given moment I want max 15 enemies on screen I will need only 15 sprites in group even when I have 100 types of enemies. I'm not saying that your reply is wrong because it's not, but I treat this forum as a place to learn and discuss best approach to solve problem.

Link to comment
Share on other sites

  • 11 months later...

 

You have getRandom() in Phaser 2.4.4, but, how can you filter to pick a dead one?

//EDIT with the answergroup.filter(function(e) { return !e.alive });
 var dead = Phaser.ArrayUtils.getRandomItem(this.fruitsGroup.filter(function(e){                    return !e.alive;                }));

How about this ? what I try something mysprite = dead.revive(); to revive the dead sprite, it occurs error says Uncaught TypeError: Cannot read property. 'revive' of null

Link to comment
Share on other sites

  • 9 months later...

This worked for me in Phaser.

do {var NAME = NAMEOFGROUP.getRandom();} while (NAME.alive === true)

Simply gets a random, and if it is alive, gets another one in a loop till a dead one is found.

I had to create this as the examples stated before did not work for me (errors for literally days). If they are all alive then the loop will run forever and crash your game.

If it doesn't work for you, reply

 

Link to comment
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...
 Share

  • Recently Browsing   0 members

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