Sorting array of objects in Javascript mode

edited October 2017 in JavaScript Mode

I'm building a little game in JS mode and have my enemies as objects in an array. I'd like to sort them by their Y coordinate to draw them in vertical order (and not overlap and look weird). Since they are generated and respawned over the course of the game, I need to sort this array repeatedly.

I've done this before in the regular Java mode using Arrays.sort() and Comparable, but that doesn't work in JS mode.

Any ideas? I found this suggestion but can't figure out how to implement it: enemies.sort(function(a,b) { return parseFloat(a.yPos) - parseFloat(b.yPos) } );

Thanks all!

Tagged:

Answers

  • edited January 2014

    Even though it's called JS Mode, it's still written under Java syntax! Then to be compiled as JavaScript! @-)
    And Java doesn't allow functions/methods to be passed as arguments, neither stored in variables.
    There's no Function data-type anyways! 8-}

    I've got no real experience w/ that mode myself yet.
    Only thing I know is that we can actually create a tab w/ ".js" extension and write real JS code there!
    But I've got no idea how to use make them communicate to each other! :o3

    Nonetheless, I've been playing w/ CoffeeScript mode for some time now. It's also compiled as JS too! =:)
    Using that mode, we've got complete access to all of JavaScript has to offer. And w/ a cleaner & easier syntax as well! :)>-

  • edited July 2014

    More about JavaScript Array

    http://www.corelangs.com/js/basics/arrays.html

    Walsh

Sign In or Register to comment.