as3.0拼图

package com{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class SXPT extends MovieClip {

        private var mouseMc:MovieClip;
        private var roundInt:int;
        private var roundArr:Array;
        private var startXYarr:Array;
        private var newMc:MovieClip;
        private var point:Point;
        private var timer:Timer;
        public function SXPT(_mouseMc:MovieClip) {
            mouseMc=_mouseMc;
            roundInt=0;
            roundArr=[];
            startXYarr=[];
            while (roundArr.length<12) {
                roundInt=1+Math.random()*12;
                if (roundArr.indexOf(roundInt)==-1) {
                    roundArr.push(roundInt);
                }
            }
            for (var i:uint=1; i<=12; i++) {
                mouseMc["darg_"+roundArr[i-1]].x=mouseMc["di_"+i].x;
                mouseMc["darg_"+roundArr[i-1]].y=mouseMc["di_"+i].y;
            }
            mouseMc.addEventListener(MouseEvent.MOUSE_DOWN,onMouseMc);
            mouseMc.addEventListener(MouseEvent.MOUSE_UP,onMouseMc);
        }
        private function onMouseMc(e:MouseEvent) {
            var str:String=e.target.name;
            if (str.slice(0,2)=="da") {
                if (e.type=="mouseDown") {
                    newMc=e.target as MovieClip;
                    mouseMc.setChildIndex(newMc,mouseMc.numChildren-1);
                    startXYarr.push(newMc.x,newMc.y);
                    newMc.startDrag();
                } else if (e.type=="mouseUp") {
                    point=new Point(newMc.x+newMc.width/2,newMc.y+newMc.height/2);
                    point=mouseMc.localToGlobal(point);
                    for (var i:uint=1; i<=12; i++) {
                        if (mouseMc["darg_"+i].hitTestPoint(point.x,point.y)) {
                            if (newMc.name.slice(5,7)!=String(i)) {
                                newMc.x=mouseMc["darg_"+i].x;
                                newMc.y=mouseMc["darg_"+i].y;

                                mouseMc["darg_"+i].x=startXYarr[0];
                                mouseMc["darg_"+i].y=startXYarr[1];
                                break
                            }
                        } else {

                            newMc.x=startXYarr[0];
                            newMc.y=startXYarr[1];
                        }
                    }
                    stopDrag();
                    startXYarr=[];
                }
            }
        }
    }
}

 

posted on 2018-08-08 17:05  zqiang0803  阅读(227)  评论(0编辑  收藏  举报

导航