티스토리 뷰

728x90
반응형
기존엔 StageRefer라는 클래스를 만들어서
public static var stage:Stage;
라고 static 변수에 대입하여 접근하는 방식을 취하였었다~
하지만 아래클래스와 같이 수정해보았다.
( 물론 외쿡분의 라이브러리를 배껴서 우아하게 조금? 수정해 보았다~ㅋㅋ )


 

package oddeye.utils 
{
	import flash.display.Stage;
	
	
	/**
	 * ...
	 * @author ....
	 */
	public class StageRefer 
	{
		public static const STAGE_ID:String = "stageID";
		private static var _stageKey:Object;
		
		
		public static function getKey():Object
		{
			if ( StageRefer._stageKey == null)
			    StageRefer._stageKey = { };
			return StageRefer._stageKey;
		}
		
		
		/**
		 * 초기에 stage 자체를 대입. 그래야 StageRefer.getStage()로 어디서든지 stage를 접근할 수 있다.
		 * @param	stage
		 * @param	id
		 */
		public static function setStage( stage:Stage, id:String=StageRefer.STAGE_ID ):void
		{
			StageRefer.getKey()[id] = stage;
		}
		
		/**
		 * stage에 접근한다.
		 * @param	id
		 * @return
		 */
		public static function getStage( id:String=StageRefer.STAGE_ID ):Stage
		{
			if ( StageRefer.getKey()[id] == null )
			    throw new Error( "StageRefer클래스에서 에러::Stage값이 없습니다.\n Document class에서 StageRefer.setStage()에 stage값을 대입하세요~" );
			return StageRefer.getKey()[id];
		}
	}

}
728x90
반응형
댓글