游戏键盘控制类

  1. package {
  2.         import flash.display.MovieClip;
  3.         import flash.ui.Keyboard;
  4.         import flash.events.KeyboardEvent;
  5.         import flash.events.Event;
  6.         public class Keys extends MovieClip {
  7.                 private var codeArray:Array = [0,0,0,0];
  8.                 public function  Keys() {
  9.                         init();
  10.                 }
  11.                 private function init() {
  12.                         stage.addEventListener(KeyboardEvent.KEY_DOWN,onkeyDown);
  13.                         stage.addEventListener(KeyboardEvent.KEY_UP,onkeyUp);
  14.                         stage.addEventListener(Event.ENTER_FRAME,onFrame);
  15.                 }
  16.                 private function onkeyDown(event:KeyboardEvent) {
  17.                         switch (event.keyCode) {
  18.                                 case 37 ://左
  19.                                         codeArray[0]=1;
  20.                                         break;
  21.                                 case 38 ://上
  22.                                         codeArray[1]=1;
  23.                                         break;
  24.                                 case 39 ://右
  25.                                         codeArray[2]=1;
  26.                                         break;
  27.                                 case 40 ://下
  28.                                         codeArray[3]=1;
  29.                                         break;
  30.                         }
  31.                 }
  32.                 private function onkeyUp(event:KeyboardEvent) {
  33.                         switch (event.keyCode) {
  34.                                 case 37 :
  35.                                         codeArray[0]=0;
  36.                                         break;
  37.                                 case 38 :
  38.                                         codeArray[1]=0;
  39.                                         break;
  40.                                 case 39 :
  41.                                         codeArray[2]=0;
  42.                                         break;
  43.                                 case 40 :
  44.                                         codeArray[3]=0;
  45.                                         break;
  46.                         }
  47.                 }
  48.                 private function onFrame(event:Event){
  49.                         myKeyTest(codeArray.join(""));
  50.                         }
  51.                 private function myKeyTest(string:String) {
  52.                         switch (string) {
  53.                                 case "1000" :
  54.                                         trace("左");
  55.                                         Move("Left",1.2);
  56.                                         break;
  57.                                 case "0100" :
  58.                                         trace("上");
  59.                                         Move("Up",1.2);
  60.                                         break;
  61.                                 case "0010" :
  62.                                         trace("右");
  63.                                         Move("Right",1.2);
  64.                                         break;
  65.                                 case "0001" :
  66.                                         trace("下");
  67.                                         Move("Down",1.2);
  68.                                         break;
  69.                                 case "1100" :
  70.                                         trace("左上");
  71.                                         Move("Left",1.2);
  72.                                         Move("Up",1.2);
  73.                                         break;
  74.                                 case "0110" :
  75.                                         trace("右上");
  76.                                         Move("Right",1.2);
  77.                                         Move("Up",1.2);
  78.                                         break;
  79.                                 case "0011" :
  80.                                         trace("右下");
  81.                                         Move("Right",1.2);
  82.                                         Move("Down",1.2);
  83.                                         break;
  84.                                 case "1001" :
  85.                                         trace("左下");
  86.                                         Move("Down",1.2);
  87.                                         Move("Left",1.2);
  88.                                         break;
  89.                         }
  90.                 }
  91.                 private function Move(drecation:String,speed:Number) {
  92.                         var angle:Number =car.rotation;
  93.                         var radius:Number =angle*Math.PI/180;
  94.                         var vx:Number=Math.cos(radius)*speed;
  95.                         var vy:Number=Math.sin(radius)*speed;
  96.                         switch (drecation) {
  97.                                 case "Right" :
  98.                                         car.rotation++;
  99.                                         break;
  100.                                 case "Left" :
  101.                                         car.rotation--;
  102.                                         break;
  103.                                 case "Up" :
  104.                                         car.x+=vx;
  105.                                         car.y+=vy;
  106.                                         break;
  107.                                 case "Down" :
  108.                                         car.x-=vx
  109.                                         car.y-=vy;
  110.                                         break;
  111.                         }
  112.                 }
  113.         }
  114. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值