连续连线

package com{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.display.SimpleButton;
    import flash.display.Shape;
    import flash.geom.Point;

    public class OneTest4 extends MovieClip {

        private var thisMc:MovieClip;
        private var btnMc:MovieClip;
        private var emptyMc:MovieClip;
        private var cleanBtn:SimpleButton;
        private var line:MovieClip;
        private var eTar:MovieClip;
        private var point:Point;
        private var endPoint:Point;
        private var pushPoint:Point;
        private var eTarArr:Array;

        private var lineToNumX:Number;
        private var lineToNumY:Number;
        private var moveToNumX:Number;
        private var moveToNumY:Number;

        private var pointArr:Array;
        public function OneTest4(_thisMc:MovieClip,_btnMc:MovieClip,_emptyMc:MovieClip,_cleanBtn:SimpleButton) {
            thisMc=_thisMc;
            btnMc=_btnMc;
            cleanBtn=_cleanBtn;
            emptyMc=_emptyMc;
            eTarArr=[];
            pointArr=[];
            btnMc.buttonMode=true;
            btnMc.addEventListener(MouseEvent.CLICK,onBtn);
        }
        private function onBtn(e:MouseEvent) {
            eTar=e.target as MovieClip;
            if (eTarArr.length==0) {
                point=new Point(e.target.x,e.target.y);
                point=btnMc.localToGlobal(point);

                line=new MovieClip  ;
                line.graphics.lineStyle(5,0);
                line.graphics.moveTo(point.x,point.y);

            }
            eTarArr.push(eTar);
            if (eTarArr.length>1) {
                var startNum:Number=Number(eTarArr[0].name.slice(-1));//第一次点击的时候
                var finishNum:Number=Number(eTarArr[1].name.slice(-1));/// 第二次点击的时候

                endPoint=new Point(e.target.x,e.target.y);
                endPoint=btnMc.localToGlobal(endPoint);

                if (startNum-finishNum==1) {
                    moveToNumX=point.x-eTarArr[0].width/2;
                    moveToNumY=point.y;
                    lineToNumX=endPoint.x+eTarArr[1].width/2;
                    lineToNumY=endPoint.y;

                    addLine();
                } else if (startNum-finishNum==-1) {
                    moveToNumX=point.x+eTarArr[0].width/2;
                    moveToNumY=point.y;
                    lineToNumX=endPoint.x-eTarArr[1].width/2;
                    lineToNumY=endPoint.y;

                    addLine();
                } else {
                    eTarArr.shift();
                    point=new Point(e.target.x,e.target.y);
                    point=btnMc.localToGlobal(point);
                }
            }
        }
        private function addLine() {

            line.graphics.moveTo(moveToNumX,moveToNumY);
            line.graphics.lineTo(lineToNumX,lineToNumY);

            var startStr:String=eTarArr[0].name.slice(0,1);
            var finishStr:String=eTarArr[1].name.slice(0,1);
            eTarArr=[];

            var pushMove:Array=[moveToNumX,moveToNumY];
            var pushLine:Array=[lineToNumX,lineToNumY];
            if (pointArr.length>0) {
                for (var  i:uint=0; i<pointArr.length; i++) {
                    var newArr:Array=pointArr[i];
                    if (newArr.join(",")==pushMove.join(",")||newArr.join(",")==pushLine.join(",")) {
                        trace("zx");
                        break;
                    } else {
                        if (i==pointArr.length -1) {
                            pointArr.push(pushMove);
                            pointArr.push(pushLine);
                            emptyMc.addChild(line);
                            if (startStr==finishStr) {
                                trace("正确");
                            }
                        }
                    }
                }
            } else {
                pointArr.push(pushMove);
                pointArr.push(pushLine);
                emptyMc.addChild(line);
                if (startStr==finishStr) {
                    trace("正确");
                }
            }
        }

    }
}

//注意事项。1原件的中心点在原件的中心。2名字的命名a_1,a_2,a_3这是一条线

posted on 2019-08-30 18:54  zqiang0803  阅读(181)  评论(0编辑  收藏  举报

导航