1
votes

Hello I have a problem with, as the title.

In my some class when I call parent. or MovieClip(root). in constructor, trace return error NULL but if I call mentioned up appeals in other private or public function (for instance, function from addEventListener(Event.ENTER_FRAME, func); it works, why ?

Can someone please explain ?

1

1 Answers

1
votes

I don't clearly understand your question, but if you would call:

this.parent or this.root

in your constructor method, it will always be null because (from Adobe ref):

  • root

    property is the top-most display object in the portion of the display list's tree structure represented by that SWF file

  • parent

    property indicates the DisplayObjectContainer object that contains this display object

Your DisplayObject can not be added anywhere before it gets constructed. First you call

var myDisplayObject:DisplayObject = new MyDisplayObject();

and then you add it to another DisplayObject or Stage (DisplayObjectContainer)

anotherDisplayObject.addChild(myDisplayObject); /* now you have myDisplayObject parent and root property defined */


package {
import flash.display.Sprite;

public class Picture extends Sprite
{
    public function Picture() 
    {
        trace(parent) // parent is null
        addEventListener(Event.ADDED, onAdded)
    }

    private function onAdded(e:Event):void 
    {
        trace(parent) // returns parent
    }
}

}