Using .stage not giving desired result

100 Views Asked by At

I make a global variable equal to a movieclip

A function gets run with an if statement checking if [x] movieclip exists, if it doesn't add it. Despite the MovieClip being on the stage it continues using the if statement.

Document class

public static var skinHolder:MovieClip = new test;

Seperate Class (function runs every frame)

    function animHandler():void
    {
        if (! Game.skinHolder.stage)
        {
            // if its not on the stage we add it to the stage
            addChild(Game.skinHolder);
        }

    }
2

There are 2 best solutions below

1
Philarmon On

The stage is not set immediately after a MovieClip is added, usually it happens on the next frame.

It is an odd way to check if an object exist though, you should do something like this:

private var mySkinHolder:MovieClip;

if (!mySkinHolder)
{
    // if it not exist we add it to the stage
    mySkinHolder = addChild(new test());
}
2
Patang On

I don't completely understand your structure, but to check if a DisplayObjectContainer (MovieClip in your case) has a specific child, you can use the contains() method.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#contains()

if (!contains(Game.skinHolder)) {
    addChild(Game.skinHolder);
}