- package {
- import flash.display.MovieClip;
- import flash.ui.Keyboard;
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- public class Keys extends MovieClip {
- private var codeArray:Array = [0,0,0,0];
- public function Keys() {
- init();
- }
- private function init() {
- stage.addEventListener(KeyboardEvent.KEY_DOWN,onkeyDown);
- stage.addEventListener(KeyboardEvent.KEY_UP,onkeyUp);
- stage.addEventListener(Event.ENTER_FRAME,onFrame);
- }
- private function onkeyDown(event:KeyboardEvent) {
- switch (event.keyCode) {
- case 37 ://左
- codeArray[0]=1;
- break;
- case 38 ://上
- codeArray[1]=1;
- break;
- case 39 ://右
- codeArray[2]=1;
- break;
- case 40 ://下
- codeArray[3]=1;
- break;
- }
- }
- private function onkeyUp(event:KeyboardEvent) {
- switch (event.keyCode) {
- case 37 :
- codeArray[0]=0;
- break;
- case 38 :
- codeArray[1]=0;
- break;
- case 39 :
- codeArray[2]=0;
- break;
- case 40 :
- codeArray[3]=0;
- break;
- }
- }
- private function onFrame(event:Event){
- myKeyTest(codeArray.join(""));
- }
- private function myKeyTest(string:String) {
- switch (string) {
- case "1000" :
- trace("左");
- Move("Left",1.2);
- break;
- case "0100" :
- trace("上");
- Move("Up",1.2);
- break;
- case "0010" :
- trace("右");
- Move("Right",1.2);
- break;
- case "0001" :
- trace("下");
- Move("Down",1.2);
- break;
- case "1100" :
- trace("左上");
- Move("Left",1.2);
- Move("Up",1.2);
- break;
- case "0110" :
- trace("右上");
- Move("Right",1.2);
- Move("Up",1.2);
- break;
- case "0011" :
- trace("右下");
- Move("Right",1.2);
- Move("Down",1.2);
- break;
- case "1001" :
- trace("左下");
- Move("Down",1.2);
- Move("Left",1.2);
- break;
- }
- }
- private function Move(drecation:String,speed:Number) {
- var angle:Number =car.rotation;
- var radius:Number =angle*Math.PI/180;
- var vx:Number=Math.cos(radius)*speed;
- var vy:Number=Math.sin(radius)*speed;
- switch (drecation) {
- case "Right" :
- car.rotation++;
- break;
- case "Left" :
- car.rotation--;
- break;
- case "Up" :
- car.x+=vx;
- car.y+=vy;
- break;
- case "Down" :
- car.x-=vx
- car.y-=vy;
- break;
- }
- }
- }
- }
游戏键盘控制类
最新推荐文章于 2020-11-10 18:03:45 发布