A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [AS3] Swapping/Arraging Depths?

Hybrid View

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    3

    Question [AS3] Swapping/Arraging Depths?

    Hey there FK forums! It's late, so I'm just going to jump right to the point.
    I'm an AS3 noob and I'm making a game - Game Link, click to place blocks - and I'm having issues with it (Surprisingly!).

    Basically, I'm trying to get the depths of all the blocks to be at the correct levels at the correct times so that the 3d effect is maintained.
    I've tried many things, but have had little success.

    All the instances of the blocks are stored in an array, but since it's adding them in accordance with when they're added to the stage, it doesn't help me locate them to swap depths. Is there a way to arrange them by location on the board?

    Does anyone have any suggestions about how get the depths right?
    Thank you in advance.

  2. #2
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    Hmm... I want to say you can always add to level 0, but that may cause some weird popping issues. Maybe not, have you tried that?

    So addChildAt(box, 0)...
    I (Love | Hate) Flash.
    ----
    Save a version back so others may help you!

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    getChildIndex will return the index of the parsed child.

    ex:
    var depth=boardMC.getChildIndex(block2);
    You have power within you to do whatever you wants to do.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You want your grid to be sorted by distance (in grid-tiles) from the centermost square (the one that's dead center - 4th row, 4th col):

    PHP Code:
    6  5  4  3  4  5  6
    5  4  3  2  3  4  5
    4  3  2  1  2  3  4
    3  2  1  0  1  2  3
    4  3  2  1  2  3  4  
    5  4  3  2  3  4  5  
    6  5  4  3  4  5  6 
    So if your blocks are all in an array already, you can just run that through a custom sort comparison to get them in the order you want:

    PHP Code:
    //  copy the array
    var temp:Array = blocksArray.concat();

    //  sort in descending order of distance from center
    temp.sort(customSort);

    //  reset the depths
    for(var i:int 0temp.length; ++i){
        
    addChild(temp[i]);
    }

    function 
    customSort(a:blockb:block):Number{
        
    //  calculate how far a is from middle column
        
    var xdist:int Math.abs(a.row);
        var 
    ydist:int Math.abs(a.column);
        var 
    aDistanceFromCenter:int xdist ydist;

        
    //  repeat for b
        
    var xdist:int Math.abs(b.row);
        var 
    ydist:int Math.abs(b.column);
        var 
    bDistanceFromCenter:int xdist ydist;

        if(
    aDistanceFromCenter == bDistanceFromCenter) return 0;
        return((
    aDistanceFromCenter bDistanceFromCenter) ? : -1);

    Please use [php] or [code] tags, and mark your threads resolved 8)

  5. #5
    Member
    Join Date
    Dec 2009
    Posts
    50
    Since the 3D part of the block is the part behind it, and that is all the same colour, with no distinguishing marks, couldn't you just separate the front from the back, and make the back follow the front or something? That way the front parts of the blocks will never have the back parts of other blocks over them...
    (Doesn't answer your question, but it might work easier...)

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