ActionScript: how can this fail?

actionscript

Solution

`contains()` will return true if the subject is a descendant of the caller. This will return true for indirect descendants too, children of children etc.

Determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance. Grandchildren, great-grandchildren, and so on each return true.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#contains%28%29

You could check the parent:

if(modalMessage && modalMessage.parent && modalMessage.parent == this)

Or, for a more general purpose disposal solution:

if(modalMessage) {
    if(modalMessage.parent) DisplayObjectContainer(modalMessage.parent).removeChild(modalMessage);
    modalMessage = null;
}

Problem

This code throws an error: ``` if (modalMessage != null && contains(modalMessage)) { removeChild(modalMessage); // the error is here modalMessage = null; } ``` The Error is: ``` [Fault] exception, information=ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. ``` how can this be? i am checking if it is a child beforehand.

Original source