AddedToStage event object and root, parent.

In AS3, There are times that you can not use root or parent properties on a display object. It is because root and parent properties are set to null, unless they are not added to the stage. So, an error occurs when you try to use root or parent properties in the constructor of an class. To avoid this problem, use addedToStage event object, and run codes after a display object was added to the display list.
AS3의 디스플레이 오브젝트에서 root 또는 parent 속성을 참조하지 못하는 경우가 종종 있습니다. 이는 root 및 parent 속성은 디스플레이 오브젝트가 스테이지에 애드되지 않으면 null 값을 가지기 때문입니다. 그러므로 클래스 컨스트럭터에서 root 또는 parent를 참조하려 할 때 에러가 발생하는 것입니다. 이를 해결하기 위해서 addedToStage 이벤트 오브젝트를 사용하여, 디스플레이 오브젝트가 디스플레이 리스트에 추가되었을 때 필요한 코드를 실행하도록 하면 됩니다.
AS3で、ディスプレーオブジェクトがrootやparentプロパ ティを参照できない時があります。それは、ディスプレーオブジェクトがステ―ジに追加されていないと、rootとparentプロパ ティはnullに設定されると言うわけです。だから、クラスのコンストラクタの中でrootやparentプロパ ティ参照しようとする時、エラーが発生するのです。このエラーを回避するために、addedToStageイベントオブジェクトを使用し、ディスプレーオブジェクトがディスプレーリストに追加された時、コードを実行します。

root property
root:DisplayObject [read-only]

…… The root property is set to null for any display object that has not been added to the display list, unless it has been added to a display object container that is off the display list but that is a child of the top-most display object in a loaded SWF file.

quote from ActionScript 3.0 Language and Components Reference

The following codes are the codes that I use frequently. I choose _root and _parent keywords that are not used in AS3.
다음 코드는 제가 자주 사용하는 코드로, AS3에서는 사용되지 않는 _root, _parent 키워드를 사용합니다.
下のコードは私がよく使うこーどで、私はAS3では使わない_rootと_parentキーワードを選びました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package{
 
	import	flash.display.MovieClip;
 
	public class SomeClass extends MovieClip{
 
		private var _root:MovieClip;
		private var _parent:MovieClip;
 
		public function SomeClass(){
 
			addEventListener(Event.ADDED_TO_STAGE, initialize);
 
		}// end SomeClass constructor
 
 
		/**
		 * Initializes the SomeClass, when an instance is added to the display list
		 */
		private function initialize(evt:Event):void{
 
			removeEventListener(Event.ADDED_TO_STAGE, initialize);
 
			_root = root as MovieClip;
			_parent = parent as MovieClip;
 
		}// end initialize
 
	}// end SomeClass definition
 
}// end package