点击字进框,点击框返回点击的地方

package com{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    public class XYX extends MovieClip {

        private var thisMc:MovieClip;
        private var clickMc:MovieClip;
        private var txtMc:MovieClip;
        private var stepNum:Number;
        private var roundInt:int;
        private var roundArr:Array;
        private var answerStr:String;
        private var removeArr:Array;
        public static var wordStr:String;
        public static var answerArr=[];

        public function XYX(_thisMc:MovieClip,_clickMc:MovieClip,_txtMc:MovieClip) {
            thisMc=_thisMc;
            clickMc=_clickMc;
            txtMc=_txtMc;
            txtMc.addEventListener(MouseEvent.CLICK,onTxt);
            clickMc.addEventListener(MouseEvent.CLICK,onClick);

            init();
        }

        private function rightInit() {
            //txtMc 变空
            removeArr=[];
            answerStr="";
            for (var i:uint=0; i<txtMc.numChildren; i++) {
                var txt:TextField=txtMc.getChildAt(i) as TextField;
                txt.text="";
            }
        }

        private function init() {

            roundArr=[];
            stepNum=0;
            answerStr="";

            rightInit();
            //选择空数字
            while (roundArr.length<clickMc.numChildren) {
                roundInt=Math.random()*clickMc.numChildren;
                if (roundArr.indexOf(roundInt)==-1) {
                    roundArr.push(roundInt);
                }
            }
            //内容填进点击的
            for (var j:uint=0; j<clickMc.numChildren; j++) {
                var _txt:TextField=clickMc.getChildAt(j) as TextField;
                _txt.text=wordStr.slice(roundArr[j],roundArr[j]+1);
            }

        }
        private function onClick(e:MouseEvent) {
            var eTarTxt:TextField=e.target as TextField;
            removeArr.push(eTarTxt);
            clickMc.removeChild(eTarTxt);
            for (var i:uint=0; i<txtMc.numChildren; i++) {
                var txt:TextField=txtMc.getChildAt(i) as TextField;
                if (txt.text=="") {
                    txt.text=eTarTxt.text;
                    if (i==txtMc.numChildren-1) {
                        for (var j:uint=0; j<txtMc.numChildren; j++) {
                            answerStr+=TextField(txtMc.getChildAt(j)).text;
                            if (j==txtMc.numChildren-1) {
                                trace(answerStr);
                                if (answerArr.indexOf(answerStr)!=-1) {
                                    trace("正确");
                                    rightInit();
                                } else {
                                    for (var k:Number=0; k<removeArr.length; k++) {
                                        clickMc.addChild(removeArr[k]);
                                    }
                                    init();
                                    trace("错误");
                                }
                            }
                        }
                    }
                    break;
                }
            }
        }
        private function onTxt(e:MouseEvent) {
            var eTarTxt:TextField=e.target as TextField;
            for (var i:Number=0; i<removeArr.length; i++) {
                var txt:TextField=removeArr[i] as TextField;
                if (txt.text==eTarTxt.text) {
                    clickMc.addChild(txt);
                    var n:Number=removeArr.indexOf(txt);
                    removeArr.splice(n,1);
                    eTarTxt.text="";
                    break;
                }
            }
        }


    }
}

 

posted on 2019-05-10 14:45  zqiang0803  阅读(191)  评论(0编辑  收藏  举报

导航