본문 바로가기

AS3/ActionScript

객체지향 설계, 클래스의 재사용, setChildIndex, 예제



BaseDocument > McGroup > Mc ( 위임 관계에 있다 )
package classes {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import classes.display.McGroup;
	public class BaseDocument extends Sprite {
		private var _mcGroup:McGroup;
		public function BaseDocument() {
			init();
		}
		private function init():void
		{
			_mcGroup = new McGroup(6, 30, 30);
			addChild(_mcGroup);
		}
	}
}




BaseDocument > McGroup > Mc ( 위임 관계에 있다 )
package  classes.display
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class McGroup extends Sprite
	{
		private var _count:int;
		private var _w:int;
		private var _h:int;
		private var _arr:Array;
		
		public function McGroup(inCount:int=10, inWidth:int=100, inHeight:int=100) 
		{
			_count = inCount;
			_w = inWidth;
			_h = inHeight;
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			_arr = [];
			for (var i:int = 0; i < _count; i++) {
				var mc:Mc = new Mc(_w, _h);
				mc.x = Math.random() * stage.stageWidth;
				mc.y = Math.random() * stage.stageHeight;
				mc.addEventListener(MouseEvent.MOUSE_DOWN, onDownHandler);
				_arr.push(mc);
				addChild(mc);
			}
		}
		
		private function onDownHandler(e:MouseEvent):void 
		{
			this.setChildIndex(e.currentTarget as DisplayObject, this.numChildren - 1);
		}
		
	}
	
}


BaseDocument > McGroup > Mc ( 위임 관계에 있다 )
package classes.display {
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	
	public class Mc extends Sprite
	{
		private var _w:int;
		private var _h:int;
		
		public function Mc(inWidth:int=100, inHeight:int=100) 
		{
			addEventListener(MouseEvent.MOUSE_DOWN, onDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, onUpHandler);
			
			_w = inWidth;
			_h = inHeight;
			
			this.width = inWidth;
			this.height = inHeight;
			changeColor();
		}

		private function changeColor():void
		{
			var colorTransfotm:ColorTransform = new ColorTransform();
			colorTransfotm.color = Math.random() * 0xFFFFFF;
			this.transform.colorTransform = colorTransfotm;
		}
		
		private function onDownHandler(e:MouseEvent):void 
		{
			this.startDrag();
			this.width = _w*1.2;
			this.height = _h*1.2;
	//		this.parent.setChildIndex(this, this.parent.numChildren - 1);
		}
		
		private function onUpHandler(e:MouseEvent):void 
		{
			this.stopDrag();
			this.width = _w;
			this.height = _h;
		}
	}
	
}




아래 Array 함수를 이용해서 무비클립 복제와 Drag시키기 를 좀더 객체지향적으로 만들어 보기위해
사용자가 직접 사각형의 갯수와 크기를 지정해줄 수 있도록 설계가 되었다.
세계의 클래스로 구성되어 있고 BaseDocument가 더큐먼트 클래스이고 McGroup는 Mc 클래스를 랜덤하게
생성하는 클래스이고 Mc는 사각형 Sprite와 자신을 클릭했을시에 크기와 드래그 기능을 가지고 있다.

여기서 참고할 부분은 McGroup에서 사각형을 클릭했을시에 해당 이벤트 객체가 최상위 뎁스로 올라오게끔 되어있는데
Mc 클래스에 그 기능을 넣어도 되지 않을까 하는 의문점이 들겠지만 setChildIndex로 사각형의 갯수를 파악하기 위해서
parent 부모 객체로 넘어가서 그값을 전달 받을 순 잇지만 여러가지 문제점이 생길 소지가 있으므로 그 기능은 차라리
McGroup에서 관리하는게 관리와 수정차원에서 편할것이다.




자수님 강의 자료를 바탕으로 작성했습니다^^