최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday

티스토리 뷰

 

 

 

DisplayObject끼리의 충돌을 검사하여 교차하면 true, 그렇지 않으면 false를 반환하는 메서드이다.

이것을 가지고 기초적인 책의 샘플을 응용하여 도형 맞추기를 만들어봄.


import flash.display.MovieClip;
import flash.events.MouseEvent;

var posX:Number;
var posY:Number;

var myInstance:Array = new Array(poly_mc,rect_mc,star_mc,cir_mc);


for(var i:int = 0; i<myInstance.length; ++i){
 var mc:MovieClip = myInstance[i] as MovieClip;
 mc.buttonMode = true;
 mc.addEventListener(MouseEvent.MOUSE_DOWN,dragStart);
 mc.addEventListener(MouseEvent.MOUSE_UP,dragStop);
}


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 findIndex:int = mc.name.indexOf("_");// _까지인 "cir_" = 3을 반환
 var target:MovieClip = this[mc.name.substring(0, findIndex) + "_area"];
 //var target:MovieClip = this[mc.name.slice(0, findIndex) + "_area"];
 //var target:MovieClip = this[mc.name.substr(0, findIndex) + "_area"];
 if(mc.hitTestObject(target)){
    mc.x = target.x;
    mc.y = target.y;
    mc.removeEventListener(MouseEvent.MOUSE_DOWN,dragStart);
    mc.removeEventListener(MouseEvent.MOUSE_UP,dragStop);
    mc.mouseEnabled = false;
    }else{
     mc.x = posX;
     mc.y = posY;
    }
 
}

 

설명준비중

 

hitTestObject_-_indexOf와_substring을_이용한_드래그)

 

댓글