hellspawn_bg Posted November 3, 2014 Share Posted November 3, 2014 Hi guys, Does anyone know a smart way to get a random sprite from the dead pool of a group? Thanks Link to comment Share on other sites More sharing options...
lewster32 Posted November 3, 2014 Share Posted November 3, 2014 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;})); chongdashu and hellspawn_bg 2 Link to comment Share on other sites More sharing options...
hellspawn_bg Posted November 3, 2014 Author Share Posted November 3, 2014 Thanks, Lewster. I needed this functionality to revive the dead sprite from the group pool. It needs to be random, because the sprites in the pool have different images. Link to comment Share on other sites More sharing options...
Odk Posted November 3, 2014 Share Posted November 3, 2014 Hi,For this type of problems I just use atlas to store all sprites and use sprite frameName property to set random image on revive. Link to comment Share on other sites More sharing options...
lewster32 Posted November 4, 2014 Share Posted November 4, 2014 Yeah that's what I typically do Odk, but I guess if you've added several different types of sprite to a group already you may as well just revive a random dead sprite, and it means you can more easily control the amount of each type in the pool. Link to comment Share on other sites More sharing options...
Odk Posted November 4, 2014 Share Posted November 4, 2014 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 More sharing options...
totallybueno Posted October 28, 2015 Share Posted October 28, 2015 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 }); Link to comment Share on other sites More sharing options...
shohan4556 Posted October 28, 2015 Share Posted October 28, 2015 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 More sharing options...
drhayes Posted October 28, 2015 Share Posted October 28, 2015 If nothing is dead, then the array will be empty and getRandomItem will return null. You'll have to put an if statement to guard against that. Link to comment Share on other sites More sharing options...
Flummox Posted August 7, 2016 Share Posted August 7, 2016 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 More sharing options...
drhayes Posted August 8, 2016 Share Posted August 8, 2016 A for loop will work better than randomly pulling them out, seeing if they're dead, then asking again. ( = Link to comment Share on other sites More sharing options...
Recommended Posts