머리가 나뻐서 삽질 SAP(Study And Programming)
1 대 다 드래그
1 on 1 드래드롭이 아니라 여러 타겟에 모두 드랍할 수 있는 방식이다.
드래그하면 hitTestObject 메서드를 사용하는데 true를 반환하게 되면 배열을 검색해서 area객체가 null 혹은 true(이미 드랍이 완료)이면
return 시키고 아닐경우 드랍시킨다.
결과보기)
코드)
import flash.display.MovieClip; import flash.events.MouseEvent; var posX:Number; var posY:Number; var mcArr:Array = [mc0,mc1,mc2,mc3,mc4]; var areaArr:Array = [area0,area1,area2,area3,area4]; var boolArr:Array = [false,false,false,false,false]; var chk:int; var total:int = mcArr.length; end_mc.visible = false; for (var i:int = 0; i<mcArr.length; ++i) { var mc:MovieClip = mcArr[i] as MovieClip; mc.buttonMode = true; mc.n = i; mc.addEventListener(MouseEvent.MOUSE_DOWN,dragStart); mc.addEventListener(MouseEvent.MOUSE_UP,dragStop); var area:MovieClip = areaArr[i] as MovieClip; area.n = i; } function dragStart(e:MouseEvent):void { posX = e.currentTarget.x; posY = e.currentTarget.y; e.currentTarget.startDrag(); e.currentTarget.alpha = 0.3; } function dragStop(e:MouseEvent):void { var mc:MovieClip = e.currentTarget as MovieClip; mc.alpha = 1; mc.stopDrag(); overLap(mc); } function overLap($mc:MovieClip):void { var mc:MovieClip = $mc; var area:MovieClip; var areaNum:int; for (var i:uint=0; i<mcArr.length; ++i) { if (mc.hitTestObject(areaArr[i])) { area = areaArr[i]; areaNum = area.n; break; } } if( area == null || boolArr[areaNum] == true) { mc.x = posX; mc.y = posY; return; }else{ mc.x = area.x + (area.width - mc.width)/2; mc.y = area.y + (area.height - mc.height)/2; mc.removeEventListener(MouseEvent.MOUSE_DOWN,dragStart); mc.removeEventListener(MouseEvent.MOUSE_UP,dragStop); mc.mouseEnabled = false; mc = null; boolArr[areaNum] = true; chk++; chk == total ? (end_mc.visible = true) : (end_mc.visible = false); } }
123