<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="474" height="526" creationComplete="initApp();"> <mx:Script> <![CDATA[ import flash.media.Sound; private var my_sound:Sound=new Sound(); private var my_sound1:Sound=new Sound(); var zmb:Array = new Array("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"); var fenshu:int = 0; var b:Boolean = false; private function initApp():void { // 最好把事件绑定到需要响应事件的组件上, 而不是 application // 否则文本框中也会响应按键动作, // 尝试把 keyTest 改成 application 然后在文本框中输入 //click="event.target.setFocus();" //stage.focus = this; panel.addEventListener(KeyboardEvent.KEY_UP, keyHandler); my_sound.load(new URLRequest("bz.mp3")); my_sound1.load(new URLRequest("ok.mp3")); } public function startGame():void { start.visible = false; close.visible = false; gameName.visible = false; panel.visible = true; stage.focus = panel; run(); } public function reinit():void { start.visible = true; close.visible = true; gameName.visible = true; panel.visible = false; } private function closeIE():void { var request:URLRequest = new URLRequest("javascript:window.close()"); navigateToURL(request,"_self"); } private function keyHandler(event:KeyboardEvent):void { super.keyDownHandler(event); trace(event.keyCode.toString() + ":" + moveTag.text.charCodeAt(0).toString()); if (event.keyCode.toString().localeCompare(moveTag.text.charCodeAt(0).toString()) == 0) { fenshu++; fen.text = "计分" + fenshu; b = true; moveEffect2.stop(); } switch (event.keyCode){ /* case 27: reinit(); break; */ case 13: reinit(); break; } } private function run():void { fenshu = 0; fen.text = "计分:0"; moveEffect2.target = moveTag; moveEffect2.duration = suiji(); moveEffect2.play(); b = false; } private function endEffectHandler():void { if (b) my_sound1.play(); else my_sound.play(); b = false; var index:int = Math.round(Math.random()*26) - 1; moveTag.visible = false; moveEffect3.yFrom = 0; moveEffect2.duration = suiji(); moveEffect2.play(); moveTag.text = zmb[index]; moveTag.visible = true; } private function suiji():int { return Math.round(Math.random()*1000) + 1000; } ]]> </mx:Script> <mx:Parallel id="moveEffect1"> <mx:Move xFrom="0" duration="1000" /> </mx:Parallel> <mx:Sequence id="moveEffect2" effectEnd="endEffectHandler();"> <mx:Move id="moveEffect3" yFrom="0" yTo="480"/> </mx:Sequence> <mx:Button id="close" visible="true" x="375" y="493" label="结束" width="89" height="23" fontSize="12" click="closeIE();"/> <mx:Button id="start" visible="true" x="375" y="462" label="开始" width="89" height="23" fontSize="12" click="startGame();"/> <mx:Label id="gameName" visible="true" creationCompleteEffect="{moveEffect1}" text="打字游戏" width="131" height="39" fontSize="24" textAlign="center" fontWeight="bold" fontFamily="Verdana" horizontalCenter="-8" verticalCenter="-234"/> <mx:Canvas id="panel" visible="false" width="100%" height="100%" click="event.target.setFocus();"> <mx:Label id="moveTag" x="209" y="0" fontWeight="bold" text="A" fontSize="23" width="29" /> <mx:Label id="fen" x="354" y="10" width="110" height="27" fontSize="19" /> </mx:Canvas> </mx:Application>