stageWidthstageHeight 속성은 Stage에 정의된 속성으로 관련 디스플레이 객체가 디스플레이 리스트에 추가가 되면서 마지막에 Stage에 추가될 때 발생하는 Event.ADD_TO_STAGE 이벤트 발생 시점부터 stage.stageWidth, stage.stageHeight 형태로 참조할 수 있게 된다. 

이 값은 ActionScript 3.0 프로젝트를 진행할 시에 정해진 Stage의 기본 폭과 높이를 알려주게 된다. 아래 블로그 글을 통해서 이 속성이 어떤 의미를 가지는지 쉽게 파악할 수 있을 것이다.

AS3.0에서 stage.stageWidth 와 stage.width 의 차이

아래 코드처럼 작성한다면 이 값들을 쉽게 참고가 가능하다. SWF 메타데이터로 100,100으로 지정했기 때문에 모든 경우가 100,100으로 나와야 하는 것이 정상이다.
package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;

	[SWF(width="100",height="100")]
	public class MainApp extends Sprite {
		public function MainApp() {
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage($e:Event):void {
			var textField:TextField;
			textField = new TextField();
			textField.text = stage.stageWidth + "," + stage.stageHeight;
			addChild( textField );
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			textField = new TextField();
			textField.text = stage.stageWidth + "," + stage.stageHeight;
			textField.y = 30;
			addChild( textField );
		}
	}
}

Flash Player 에서 직접 실행시키면 모두 100으로 나온다. 하지만 IE8에서는 처음에는 100이지만 다음에는 0이 나왔다. IE외에 다른 브라우져는 정상적으로 100이 나온다. 재미있는 것은 IE8에서 stage.scaleMode를 지정하면서 이 값이 리셋되어 버린다는 것이다. 이게 Mac의 FF3.0에서도 이런 현상이 있다고 한다.

내가 만든 Flash 애플리케이션이 모든 운영체제(Cross OS)의 모든 브라우져(Cross Browser)에서 동작하도록 하기 위해서 다음과 같은 처리가 필요하게 되었다.
package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;

	[SWF(width="100",height="100")]
	public class MainApp extends Sprite {
		public function MainApp() {
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage($e:Event):void {
			$e.target.removeEventListener( $e.type, arguments.callee );
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			var count:int = 0;
			if( stage.stageWidth === 0 && stage.stageHeight === 0 ) {
				stage.addEventListener( Event.ENTER_FRAME, function ($e:Event):void {
					if( stage.stageWidth > 0 || stage.stageHeight > 0 ) {
						stage.removeEventListener( $e.type, arguments.callee );
						_init();
					}
					trace( ++count ); //IE8에서 두번 들어왔다.
				});
			} else {
				_init();	
			}			
		}
		
		protected function _init():void {
			var textField:TextField;
			textField = new TextField();
			textField.text = stage.stageWidth + "," + stage.stageHeight;
			addChild( textField );
		}
	}
}


Flex의 경우라면 아마도 이런 처리가 다 되어 있기 때문에 신경쓰지 않고 개발하면 될 것이다. 하지만 ActionScript 3.0으로 개발할 때에는 이런 문제를 간과하면 안될 것 같다.

참고글
Firefox 3 Mac Flash Bug stageWidth and stageHeight are 0
stageWidth is zero in IE
flash.display.Stage 클래스
flash.display.DisplayObject 클래스


글쓴이 : 지돌스타(http://blog.jidolstar.com/656)

+ Recent posts