본문 바로가기

AS3/ActionScript

removeChild, removeEventListener를 통한 메모리상 객체지우기


package 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	
	public class RemoveChild extends Sprite
	{
		private var sprite:Sprite;
		
		public function RemoveChild()
		{
			init();
		}
		private function init():void
		{
			sprite = new Sprite();
			addChild(sprite);
			sprite.graphics.beginFill( 0xff0000);
			sprite.graphics.drawRect( 0, 0, 100, 100 );
			sprite.graphics.endFill();
			sprite.x = 100;
			sprite.y = 100;
			stage.addEventListener( MouseEvent.CLICK, onClickHandler );
		}
		private function onClickHandler( event:MouseEvent ):void
		{
			removeDisplayObject(stage, sprite, MouseEvent.CLICK, onClickHandler);
		}
		private function removeDisplayObject(inParent:DisplayObject, inDS:DisplayObject, inType:String, inFunc:Function):void {
			inParent.removeEventListener(inType, inFunc);
			removeChild(inDS);
		}
	}
}



removeChild와 removeEventListener 를 함수로 등록하여 효율적으로 메모리상에서 제거하는 함수를 만들었다.