0 votes
by (250 points)
I'm creating an rpg-like combat system, and I am currently attempting to make the "fastest" participants in a battle move first. To do this, I have given each entity (Both player/party and enemy characters) a "speed" variable, but I don't know how to make it so that $turn1 is equal to the enemy with the highest speed, $turn2 is equal to the enemy with the second-lowest speed, and so on, up to $turn8.


Enemy1, 2, 3, and 4 all have their numbers (enemy1, 2, 3, and 4) stored as variables under their objects, so all I need is a way to assign them to the $turn variables based on the values output by the speed calculations.



1 Answer

+1 vote
by (2.4k points)
selected by
Best answer

I think this link could be of interest for you : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

I believe your answer can be found there. There's a "Objects can be sorted given the value of one of their properties." demonstration, and then lots of examples showing how to proceed.

(I got lost on my own thoughts, you might not have understood why I showed you a link talking about arrays. My idea was to get your speed values into an array $turnOfCharacter[].  Then have those classified by the sort method, and use those, as your array $turnOfCharacter[] will have been modified and contain each speed value in order)
by (250 points)
Thanks a ton! Really helped.