A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] sorting movieclips according to y values

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    33

    resolved [RESOLVED] sorting movieclips according to y values

    In the project I'm working on, there are towers the user can place. However, it you place a tower higher then another one, it appears infront of that one, which looks weird.

    I want to be able to arrange all the towers so that that does not happen.

    There is an sprite called towerHolder holding, which all the towers are children of, and an array called towerArray, which the towers get pushed to when they are created.

    I tried clearing towerHolder, then running the sortOn() function on the array, and then adding each tower back to the towerHolder, but the towers just seem to be randomly placing themselves irrelevent to their height.

    Here's my code for the relevant parts:

    Actionscript Code:
    function clearTowers():void {
        while (towerHolder.numChildren > 0) {
            towerHolder.removeChildAt(0);
        }
    }

    function orderTowers():void {
        clearTowers();
        towerArray = towerArray.sortOn(y, Array.NUMERIC);
        for each (var tower in towerArray) {
            towerHolder.addChild(tower);
        }
    }
    So yeah if you could help me so that I have a function I can called to order the towers correctly, that would be great.
    Last edited by Jezzamon; 11-06-2010 at 11:45 PM. Reason: tyops

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    you need to put "y" in quotes - otherwise y will be the y value of "this"

    PHP Code:
    towerArray.sortOn("y", Array.NUMERIC); 

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    33
    wow thanks works completely. C:

  4. #4
    ___________________
    Join Date
    May 2004
    Posts
    3,174

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center