package { //动画要求: //1、左边每帧“选择项”等于右边每帧“答案” //适用动画: //1、两两对应的连线 //代码要求: //1、_chooseNum_arr必须为二维数组,并且每个一维数组都同属“选择项”或者同属“答案”的连线 //2、“选择项”第一个字符串必须跟“答案”的第一个字符串相同 //3、一维数组个数等于该一维数组可跑的帧数,二维数组的个数等于共有多少列 import flash.display.MovieClip; import flash.events.MouseEvent; import flash.filters.GlowFilter; import flash.utils.Timer; import flash.events.TimerEvent; import flash.display.Shape; public class LinkAnswer extends MovieClip { private var canvas:Shape;//连线的画布 private var beforeMc:MovieClip;//每次连线后或者初始第一个选择的mc private var IsTrue:Boolean ; private var mcNum:int = 0;//累计选择的mc总个数 private var timeNum:int;//显示结果的时间间隔。 private var resulTimer:Timer;//结果显示的时间侦听器 private var thisMc:MovieClip;//当前mc private var chooseLen:int;//选择的答案所有总多少个 private var choose_arr:Array = [];//装载选择的答案 private var chooseNum_arr:Array;//装载每条题目的选择项数与标签 public function LinkAnswer() { //init(); } //_chooseNum_arr二维数组 _thisMc当前mc,_timeNum结果时间 public function init(_chooseNum_arr:Array,_thisMc:MovieClip,_timeNum:int) { for(var i=0;i<_chooseNum_arr.length;i++) { //定位每行的元素 for(var j=0;j<_chooseNum_arr[i].length;j++) { //依次存入每个元素 choose_arr.push(_chooseNum_arr[i][j]); } } chooseNum_arr=_chooseNum_arr; thisMc=_thisMc; timeNum=_timeNum; IsTrue=true; //获取所有的mc总长度 chooseLen=choose_arr.length; //将画布添加到该mc上 canvas=new Shape(); thisMc.addChild(canvas); thisMc.setChildIndex(canvas,1); //时间侦听 resulTimer=new Timer(timeNum,1); resulTimer.addEventListener(TimerEvent.TIMER,resulTimer_fun); pushChooseArr(); } private function pushChooseArr():void { //获取所有的mc总长度 var chooseNumLen:int=chooseNum_arr.length; for(var b:int=0;b<chooseNumLen;b++) { //存入每组mc var _arr:Array = new Array(); for (var d = 1; d <= chooseNum_arr[b].length; d++) { //存入每组mc _arr.push(d); //当装载每组mc的总mc个数的数组等于每组mc的总个数时,进行mc随机放 if(d == chooseNum_arr[b].length) { for (var e = 0; e < chooseNum_arr[b].length; e++) { var j:Number = Math.round(Math.random() * (_arr.length - 1)); chooseNum_arr[b][e].gotoAndStop(_arr[j]); chooseNum_arr[b][e].addEventListener(MouseEvent.CLICK,choose_fun); chooseNum_arr[b][e].buttonMode = true; _arr.splice(j,1); } } } } } //选择mc进行连线 private function choose_fun(e:MouseEvent):void { var chooseMc:MovieClip = e.currentTarget as MovieClip; if (beforeMc) { //当当前的mc的名字第一个字符串等于之前选择的mc的第一个字符串并该mc长度不等于之前mc长度时进行连线 if (beforeMc.name.length != chooseMc.name.length && beforeMc.name.charAt(0) == chooseMc.name.charAt(0)) { canvas.graphics.lineStyle(5,0x990000); canvas.graphics.moveTo(beforeMc.x,beforeMc.y); canvas.graphics.lineTo(chooseMc.x,chooseMc.y); canvas.graphics.endFill(); //判断当前mc帧与之前mc帧是否一致 if (beforeMc.currentFrame != chooseMc.currentFrame) { IsTrue = false; } chooseMc.mouseChildren = false; chooseMc.mouseEnabled = false; beforeMc.mouseChildren = false; beforeMc.mouseEnabled = false; mcNum += 2; } beforeMc.filters = null; beforeMc = null; //判断是否做完题目 IsOVER(); } else { chooseMc.filters = [new GlowFilter ]; beforeMc = chooseMc; } } //判断是否做完题目 private function IsOVER():void { //记载的mc数目等于数组数目,则进行揭开结果resulTimer_fun if (mcNum==choose_arr.length) { resulTimer.start(); } } private function resulTimer_fun(e:TimerEvent):void { //清除画布,移除mc侦听和时间侦听 delet_fun(); var mc:MovieClip=thisMc.parent as MovieClip; if(!IsTrue) { mc.fanying("答错"); } else { mc.fanying("答对"); } } //清除画布,移除mc侦听和时间侦听 private function delet_fun():void { canvas.graphics.clear(); thisMc.removeChild(canvas); canvas=null; for(var i=0;i<choose_arr.length;i++) { choose_arr[i].removeEventListener(MouseEvent.CLICK,choose_fun); } resulTimer.removeEventListener(TimerEvent.TIMER,resulTimer_fun); } } }