[AS 3.0] 속성 참조가 안 될 때, Event.ADDED_TO_STAGE

ActionScript 3.0 — Tags: , , — Siaa @ 3:11 pm

TypeError: Error #1009: null 객체 참조의 속성이나 메서드에 액세스할 수 없습니다.

라는 에러 문구를 종종 보게 되는데…
주로 클래스를 인스턴스화해서 addChild() 하면서 동시에 root, parent, stage 의 속성을 참조하려고 하면 위의 에러 문구를 만나게 된다. 이는 addChild()가 되었으니 당근 참조가 가능하겠지.. 라고 생각했는데, 결국 뒤통수를 맞게 되는 것이다.
(배구에서 공격하려고 점프했는데 공이 아직 안 올라온거라고 할까…)
여튼, 이럴 때 Event.ADDED_TO_STAGE 를 사용하면 간단히 해결된다.

package {

	import flash.display.MovieClip;
	import flash.events.Event;

	public class Main extends MovieClip {

		public function Main():void {
			addEventListener(Event.ADDED_TO_STAGE, onAddedHandler);
		}

		private function onAddedHandler(e:Event):void {
			init();
		}

		private function init():void {
			removeEventListener(Event.ADDED_TO_STAGE, onAddedHandler);

			trace( "stage.stageHeight : " + stage.stageHeight );
			trace( "stage.stageWidth : " + stage.stageWidth );
		}
	}
}
Share

2 Comments »

  1. 좋은 정보 감사합니다~^^ 덕분에 문제해결 깔끔하게 잘 되었습니당~ㅋ

    Siaa Reply:

    후훗!! 도움이 되었다니 다행이네요^^
    좋은 주말 보내세요~~

    Comment by mjwon — 2010/02/05 @ 2:35 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 blog.flasia.com | powered by WordPress with Barecity