package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.*;
import flash.utils.Timer;
public class AppleGame extends Sprite {
var arrAZ:Array=new Array();
var AppleContain:Sprite = new Sprite ;
var arrLevel:Array=new Array();//當期等級蘋果數
var Level:uint = 1;//等級
var WindowContain:Sprite=new Sprite;
var Num_Apples:uint=0;
var Num_RIGHT:uint=0;
var Num_Timer:uint=0;
var T_score:TextField = Text.Return("文本Text",20,370,0x00ff00,false);
var T_Rate:TextField=Text.Return("文本Text",220,370,0x00ff00,false);
var TIMER:Timer=new Timer(1000);
var P_stop:Boolean=false;
public function AppleGame() {
arrLevel = [1,3,5];
addChild(WindowContain);
addChild(AppleContain);
window();
arrAZ = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
stage.addEventListener(KeyboardEvent.KEY_DOWN,CatchKey);
stage.addEventListener(Event.ENTER_FRAME,enterframe);
TIMER.start();
TIMER.addEventListener(TimerEvent.TIMER,H_TIMER);
stage.addEventListener(KeyboardEvent.KEY_DOWN,H_PStop);
}
private function enterframe(e:Event):void {
P_stop=true;
addapple();
appleDown();
T_Rate.text="正確率:"+Math.floor(Num_RIGHT/(Num_Apples-3)*100)+"%";
T_Rate.setTextFormat(TF.Return(20,"宋體",true));
}
private function addapple() {
if (AppleContain.numChildren < arrLevel[Level]) {
var apple:Apple=new Apple();
Num_Apples++;
apple.x = Math.random() * 450 + 50;
AppleContain.addChild(apple);
var Txt:TextField = Text.Return("文本Text",-8,-10,0x0000ff,false);
Txt.text = arrAZ[Math.floor(Math.random() * 26)];
Txt.setTextFormat(TF.Return(20,"宋體",true));
apple.addChild(Txt);
apple.TYPE = Txt.text;
}
}
private function appleDown() {
for (var i:int=0; i<AppleContain.numChildren; i++) {
var MovingApple:Apple = AppleContain.getChildAt(i) as Apple;
MovingApple.y += Level + 3;
if (MovingApple.y > 400) {
AppleContain.removeChild(MovingApple);
}
}
}
private function CatchKey(ek:KeyboardEvent):void {
for (var j:int=0; j<AppleContain.numChildren; j++) {
var MovingApple:Apple = AppleContain.getChildAt(j) as Apple;
if (MovingApple.TYPE == String.fromCharCode(ek.charCode).toUpperCase()) {
Num_RIGHT++;
AppleContain.removeChild(MovingApple);
break;//檢測到壹個後跳出循環
}
}
}
private function H_PStop(ek:KeyboardEvent):void{
if(ek.keyCode==32){
if(P_stop){
TIMER.stop();
stage.removeEventListener(Event.ENTER_FRAME,enterframe);
stage.removeEventListener(KeyboardEvent.KEY_DOWN,CatchKey);
P_stop=false;
}else{
TIMER.start();
stage.addEventListener(Event.ENTER_FRAME,enterframe);
stage.addEventListener(KeyboardEvent.KEY_DOWN,CatchKey);
}
}
if(ek.keyCode==13){
Num_Timer=Num_RIGHT=0;
Num_Apples=3;
}
}
private function H_TIMER(et:TimerEvent):void{
Num_Timer++;
T_score.text="速度:"+Math.floor(Num_RIGHT/Num_Timer*60)+"字/分鐘";
T_score.setTextFormat(TF.Return(20,"宋體",true));
}
private function window(){
T_score.width=180;
WindowContain.addChild(T_score);
T_Rate.width=140;
WindowContain.addChild(T_Rate);
}
}
}