Parent and classes

Hello,

I have searched for this problem, and found no solution. I am sure it is not an uncommon question, however.

I am having difficulty with ‘parent’ in Actionscript 3 accostiated with items that are added via addChild().

The following call to ‘tracer’ from the MC ‘Box’ works:


// Document Class

package {
	import flash.display.MovieClip;

	public class MyProject extends MovieClip
	{
		public function MyProject()
		{
		}
		
		public function tracer():void
		{
			trace("Testing...");
		}
	}
}


// Box Class - An instance of 'Box' was added manually to the stage in Flash

package
{
	import flash.display.MovieClip;
		
	public class Box extends MovieClip
	{
		public function Box()
		{
		       MovieClip(this.parent).tracer();
		}

	}
}

HOWEVER, when the ‘Box’ movie is NOT added to the stage manually, but added via addChild() from the document class, like this…


// Document Class
package {
	import flash.display.MovieClip;

	public class MyProject extends MovieClip
	{
		public function MyProject()
		{
			var b:Box = new Box();
			addChild(b);
		}
		
		public function tracer():void
		{
			trace("Testing...");
		}
	}
}

… it returns the error:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at Box()
	at MyProject()

Is it possible to call ‘tracer()’ from ‘Box’ when the ‘Box’ is added via addChild()?

Thank you for your help!