The parent
property of a DisplayObject
gets set when its added to another DisplayObjectContainer
via .addChild()
or .addChildAt()
. These calls are made implicitly by the Flash UI to add children to a visible object.
So there are a few things wrong with what you are doing, some are misunderstandings about how DisplayObjects
work in flash, and others are syntactical problems.
When you call new Child()
, you are invoking the Child
classes' constructor, in your case:
public function Child()
{
trace([parent as MovieClip].i);
}
At this point, it has not been added as a child to a DisplayObjectContainer
. Simply naming it as a child
in your parent class does not create this relationship. So in this case, inside your constructor this.parent
will alway be null because its just creating a new instance of the Child
class, it has no knowledge of any "parent"
There is a pretty big syntax problem here, as well. You are doing as as
cast (which will return null
if the types are incompatible). In this case, however, parent
is already null
, but then you are using []
, which is the way to write a literal Array
. So you are essentially creating a new Array
with one element which is null
. Then you are attempting to get a property called i
on the Array
not the parent.
Obviously, this is not what you want to do... At all. You probably meant something like this:
public function Child()
{
trace((parent as MovieClip).i);
}
But even this isn't right, because i
isn't a property on a MovieClip
, its a property on your sub-class Main
. So you really probably meant:
public function Child()
{
trace((parent as Main).i);
}
But this will still fail, because like I said before, parent == null
because it hasn't been added as a child yet.
Consider the following code:
public class Main extends MovieClip
{
public var i;
public var child:Child;
public function Main()
{
i = 4;
child = new Child();
child.doWork();
this.addChild(child)
child.doWork();
}
}
And:
public class Child extends MovieClip
{
public function Child()
{
doWork();
}
public function doWork()
{
if(parent == null) {
trace("No parent yet.")
return;
}
var mainParent:Main = (parent as Main);
if(mainParent == null) {
trace("Parent isn't a instance of Main!")
return;
}
trace(mainParent.i);
}
}
This should call the method doWork()
three times: Once in the child's constructor, then before being added to the display tree, and finally after being added to the display tree. And hopefully it'll make more sense, seeing it work like this.
There is a lot more to learn, especially about events, event bubbling, and how DisplayObjects
and DisplayObjectContainers
are related/nested. But perhaps this will give you a shove in the correct direction. Just know that parent
is just a property of a DisplayObject
(one of the ancestors of the MovieClip
and many other classes in flash). Understanding how these classes work with each other, and extending them with your own functionality, is key to really understanding flash.