arr_Button.Reset(i*50+25,250); } } public function OnKeyboardDown(e:KeyboardEvent):void { for each (var button in arr_Button) { if (button.visible==true&&button.buttonState==0) { //当场景里存在可见对象为静止的时候,即等待的元件都到位之后,再执行判断键值的操作 switch (e.keyCode) { case 38 : CheckKey(1); break; case 39 : CheckKey(2); break; case 40 : CheckKey(3); break; case 37 : CheckKey(4); break; } break; }
} } public function OnKeyboardUp(e:KeyboardEvent):void { bool_In=true; } //令按键释放时可再次输入 public function createTimer():void { var MyTimer:Timer=new Timer(50,0); MyTimer.addEventListener("timer",timerHandler); MyTimer.start(); } public function timerHandler(e:TimerEvent):void { //此函数用于判断和文本框处发生碰撞的按钮,并更新成绩 for each (var button in arr_Button) { if (button.visible==false) { continue; } button.GoTo(); if (button.hitTestPoint(t_Lose.x+100,t_Lose.y+20,true)) { button.visible=false; lose++; t_Lose.text="失败"+String(lose)+"次!"; } if (button.hitTestPoint(t_Win.x+100,t_Win.y+20,true)) { button.visible=false; win++; t_Win.text="成功"+String(win)+"次!"; } } } public function CheckKey(k:int) { if (bool_In==false) { return; } bool_In=false; for each (var button in arr_Button) { //筛选与方框中心碰撞的元件,即当前选定的元件,并用它的方向和用户输入的方向相比,相同则将其目标点设置为成功文本附近,不通则设置为失败文本附近,并播放相应声音 if (button.hitTestPoint(mc_ZhunXing.x,mc_ZhunXing.y,true)) { if (button.buttonDirection==k) { button.SetAim(t_Win.x+100,t_Win.y+20); AddButton(25,250); Pat.play(); } else { button.SetAim(t_Lose.x+100,t_Lose.y+20); AddButton(25,250); Erro.play(); } } } } public function AddButton(ox:int,oy:int) { //用于游戏开始后添加新按钮 for each (var button in arr_Button) { //挑选未使用的按钮,并在最左侧将其添加 if (button.visible==true) { continue; } button.Reset(ox-50,oy); break; } for each (var button2 in arr_Button) { //将本元件引导至屏幕上按钮队列的最左边位置 if (button2.visible==true&&button2.buttonState==0) { button2.SetAim(button2.x+50,button2.y); } } } } }