# \[AS3\] Oh boy, here we go again, depth handling!

**URL:** <https://forum.kirupa.com/t/as3-oh-boy-here-we-go-again-depth-handling/274029>\
**Category:** flash\
**Created:** [October 23, 2008, 10:10pm UTC](https://forum.kirupa.com/t/as3-oh-boy-here-we-go-again-depth-handling/274029 "2008-10-23T22:10:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valaran](https://avatars.discourse-cdn.com/v4/letter/v/ee59a6/32.png) [@Valaran](https://forum.kirupa.com/u/Valaran)\
**Post date:** [October 23, 2008, 10:10pm UTC](https://forum.kirupa.com/t/as3-oh-boy-here-we-go-again-depth-handling/274029/1 "2008-10-23T22:10:28Z")

</div>

Hello again,

I’m having trouble .\_. anyway I have an array of objects with different values, and I’m trying to perform a very specific sort, please look at the two pictures I attached, the first picture should show no errors, the second however… Anyway, the difference between the pictures is that, that in the first picture, the middle tile has a Z value of 4, just like the other 8 around it, on the second picture it has a Z value of 2, which means it should be ‘below’ the others, therefore not be visible like it is. The sort function I’m using now looks like this:

[AS]  
private function sortDepth():void {  
depth\_grid.sort(customDepthSort); // depth\_grid contains all the objects  
var ilen:uint = depth\_grid.length;  
for(var i:uint = 0; i \< ilen; i++){  
this.setChildIndex(depth\_grid\*, i);  
}  
}  
/\* Tile is just an extension of the sprite class which contains more values like Z, etc \*/  
private function customDepthSort(a:Tile, b:Tile):int {

```
    /* DEFAULT_ELEVATION is the default Z of the tiles, which is set to 3 right now */
var compare:Boolean = (a.z - b.z) &gt; DEFAULT_ELEVATION;
       
if(a.y &lt; b.y) {
    if(compare){
        return 1;
    }
    return -1;
} else if(a.y &gt; b.y) {
    if(compare){
        return 1;
    }
    return 1;
} else {
    if(compare){
        return 1;
    }
    return 0;
}

```

}  
[/AS]

I’ve tested loads of variations, and I’ve been trying my best, but I simply cannot get my head around it, if you don’t understand the above, its currently sorting based on the Y, and the Z values. The lower the Y value, the lower down in the displaylist, generally, but then when the Z comes into play I lose it.

As I’ve not been able to solve this myself, I’d like some guidance,

Thanks for any replies.
