Jump to content

getRandomAlive?


Secretmapper
 Share

Recommended Posts

Hello!

 

Since Group works as a pool too, I've worked with two games that required a getRandomAlive function. However, the group doesn't really have that method, so I was left to rolling out my own.

 

Any tips? I can think of two things personally, and I used the first in my former game.

Basically, I use a do while loop until I get an alive sprite using getRandom(). It's simple and direct, but I can imagine the game taking a performance hit, because of rejection sampling.

 

Another idea is to use forEachAlive and append each of the elements in an array, then get a random element from that array. Sounds wasteful though, and it's like, what, O(n) operation?

 

I guess the reason this might be tricky to do is because group uses linked list (if I'm correct?). If that's the case, another Idea is to go to a random index, and then iterate until I hit an alive element. But I'm not really sure how to do that. (Phaser.Group has a next and prev method, but as far as the docs there is not get element in cursor method!? Or will iterating the cursor move it to change the getFirstAlive Result - as in relative to current cursor).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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