mx.core.View.destroyChildAt() bugfix

Actionscript

Ok, this is an obscure post, but someone somewhere might be having problems with this method while extending mx.core.View and happen to google search for it. :D

I came across this while creating and destroying objects with createChild() and destroyChildAt(). The depths seem to get messed up and you end up placing one object on top of another in certain circumstances, causing it to vanish.


And so I present a tweaked version. I've only tested it with my application but if for some weird reason you're having problems with your views you can try it.

AS:


function destroyChildAt(childIndex:Number):Void
{
        if (!(childIndex >= 0 && childIndex < numChildren))
                return;

        var childName:String = childNameBase + childIndex;
        var nChildren:Number = numChildren;

        destroyObject(childName);

        // Shuffle all higher numbered children down
        for (var i = childIndex; i < (nChildren - 1); i++)
        {
                var c = this[childNameBase + i] = this[childNameBase + (i + 1)];
                c.swapDepths(i+depth-nChildren);
        }

        // Delete the leftover slot
        delete this[childNameBase + (nChildren - 1)];
        depth-=1;
}

This entry was posted on Tuesday, March 1st, 2005 at 10:22 pm and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

One Response to “mx.core.View.destroyChildAt() bugfix”

  1. JesterXL Says:

    Good to know. I incorporated my own versions of the 3 methods Flex' adds to View, and low-and-behold, 1 was failing, and I bet this is the culprit.

Leave a Reply