Marty
Marty

Reputation: 39456

Presorting an array in AS3

I'm trying to pre-sort an array of items are added into an array using the following:

draw(texture:LTexture, position:Point, depth:int = 0):void

At the moment, calling this adds the texture into a queue. Once the items in my queue are ready to be rendered, I use the following to sort my array:

queue.sortOn("depth");

This works fine, but it's slow, especially when my queue has over 2000 textures to be drawn.


I'm trying to presort my array by adding some new logic into draw(). Basically my queue is now a 2D array, where the first layer represents depth and each array within this contains the textures to draw. For example:

queue[1] = [texture, texture];
queue[4] = [texture];
queue[2] = [texture, texture, texture, texture];

I then have a function which combines these arrays into a new queue:

function collate():Array
{
    var collated:Array = [];

    for each(var i:Array in _block)
        collated = collated.concat(i);

    return collated;
}

This seems to work ok, but there are a few issues so far:

  1. When I use vastly varying depths, e.g. 5 and 17, the foreach loop seems to concatenate my inner arrays in whichever order they were created, rather than by their index (weird - works fine if I use depths closer together).
  2. If the depths specified are really high and/or greatly varied, my queue ends up having a tonne of empty slots that my foreach loop will have to include in the process of iteration when concatenating my arrays, eg: ,,,,,,,,,,,texture2,texture3,,,,,,texture0,texture1,,,,,texture4

How can I better approach what I am trying to achieve? That being, I want to end up with an array containing all of my textures sorted by depth.

Upvotes: 2

Views: 327

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

Although I can't imagine a reason to have 2000 textures to be z-sorted (seems like an awful lot), I came up with these ideas:

  1. Use a linked list instead of an array, and have the textures find their respective positions at the time you add them (add() each new texture to the first node, then let the linked nodes pass it on until it reaches its place at the correct depth).

    If you have the time to get into linked lists, this is probably the solution I would choose, because it allows you to keep existing structures and add or remove textures as they come along, without having to sort the entire list again and again.

  2. You can combine linked list with a dictionary: Create a linked list as described above and collect references to the first node of each depth in a Dictionary (node_dictionary[depth] = node;). That way, you can add() new nodes at a given depth, instead of having to pass them on from the beginning of the list.

  3. Use a second array to collect only your existing queue indices (push the index each time you add a texture) and sort that simple array to get rid of the empty slots.

    var index_array : Array = [];
    ...
    queue[17].push(texture);
    index_array.push (17);
    ...
    queue[1].push(texture);
    index_array.push (1);
    ...
    queue[17].push(texture); // probably more than one texture for each index
    index_array.push (17);
    
    index_array.sort(Array.NUMERIC|Array.UNIQUESORT);
    
    var i:int = -1;
    while (++i < index_array.length) drawTextures(queue[i]);
    

Upvotes: 2

Related Questions