Fetch data of a random child from group

Hello.

I’ve have been producing a game as a part of my agile course. I would like to ask if there is a way to fetch a random child from my enemy group. The game is based on space invaders and i have already implemented a wave system so when you clear the screen of enemies the game spawns a new wave. 2 out of 6 enemy types are supposed to fire back at the player.

So here is we come to my problem. I can get them to fire using getFirstAlive(), but our product owner is not satisfied with that and i must agree it looks kinda stupid. My bullet code looks like this: Enemy bullet code

I guess you want something like this:

let children = enemies.getChildren().filter(e => e.active === true)
let randomEnemy = children[Phaser.Math.RND.integerInRange(0, children.length - 1)]
1 Like

Thanks for the fast reply!
But i already managed to make a List and use that, but i thank you for reminding me to use a filter!

1 Like
var GetAll = Phaser.Utils.Array.GetAll;
var GetRandom = Phaser.Utils.Array.GetRandom;

var enemy = GetRandom( GetAll( enemies.getChildren(), 'active', true ) );
2 Likes