Isometric Depth Sorting

I am curious if there is a more efficient way to do isometric depth sorting. Here is how I have done it…

The avatar and the other in game npc’s that move and all visible game objects are added to an array and in the same container object. They are all given an isoDepth variable which is Row+Col.


                if (Moving == true) {
                    userAvatar.x+=avatarVel.x;
                    userAvatar.y+=avatarVel.y;

                    //If past halfway point to next rowCol in path
                    if (gameMath.distanceBetweenPoints(avatarPoint,targetPoint)<gameMath.distanceBetweenPoints(previousTargetPoint,targetPoint)/2) {
                        //Set iso depth index to next point in path
                        if (pointInPath < pathLength) {
                            //Depth index
                            isoDepth=isoFunctions.getDepthIndexFromRowCol(avatarPath[pointInPath+1]);
                            if (isoDepth != lastIsoDepth) {
                                //Depth check/update
                                Game.gameObjectsManager.checkUpdateDepth(-1,isoDepth);
                                lastIsoDepth=isoDepth;
                            }
                        }
                    }
Etc...
}

So, if the iso depth for any moving element in game changes…

That object then calls checkUpdateDepth in the game objects manager.


public function checkUpdateDepth(Tag:int, isoDepth:int){
            //Update iso depth
            var preIndex:int = arrayFunctions.getIndexByTag(visibleObjects,Tag);
            visibleObjects[preIndex].isoDepth = isoDepth;
            
            //Sort array
            visibleObjects.sortOn("isoDepth",Array.NUMERIC);
            
            var arrayIndex:int = arrayFunctions.getIndexByTag(visibleObjects,Tag);
            var childIndex:int = Game.objectsContainer.getChildIndex(visibleObjects[arrayIndex].Ref);
            
            //If array and child index do not match... Change index
            if (arrayIndex != childIndex){
                Game.objectsContainer.setChildIndex(visibleObjects[arrayIndex].Ref,arrayIndex);
            }
        }

If the child index and sorted array index do not match the child index is updated. It seems to work perfectly. It’s pretty efficient because the function is only called when any moving game characters cross the halfway point between cells.

Anyone have any ideas if this could be improved?