AS3_八方向运动,多按键判断

      我的日志 2008-6-25 20:25

先看效果,WSAD控制小人运动

*********************************************************************************************************

一共两个类:Game.as      Player.as  一个fla文件

*******************************************************************************************

Game.as:

package {
 import flash.display.Sprite;
 import flash.events.TimerEvent;
 import flash.events.KeyboardEvent;
 import flash.utils.Timer;
 import flash.events.MouseEvent;
 import flash.display.MovieClip;
 public class Gameextends Sprite {
  private var mc:Player;
  private var keyarr:Array;
  private var heading:uint;
  private var step:uint;
  private var timercut:Timer;
  private var xiefangxiang:Boolean;
  public function Game() {
   keyarr=[0];
   heading=0;
   step=5;
   xiefangxiang=true;
   mc=new Player();
   mc.x=60;
   mc.y=330;
   mc.gotoAndStop("zhan");
   addChild(mc);
   timercut=new Timer(50,1);
   timercut.addEventListener(TimerEvent.TIMER,ontime);
   stage.addEventListener(KeyboardEvent.KEY_DOWN,onk);
   stage.addEventListener(KeyboardEvent.KEY_UP,onk);
  }
  public function ontime(e:TimerEvent):void {
   xiefangxiang=true;
   if (heading != 0) {
    heading=keyarr[1];
    zhou();
   }
  }
  public function onk(e:KeyboardEvent):void {
   switch (e.keyCode) {
    case 87 :// W
    case 68 :// D
    case 83 :// S
    case 65 :// A
     if (e.type=="keyDown") {
      if (keyarr.indexOf(e.keyCode) != -1) {
       return;
      }
      keyarr.push(e.keyCode);
      sethead();
     } else {
      var tempIndex:int=-1;
      tempIndex=keyarr.indexOf(e.keyCode);
      if (tempIndex > 0) {
       keyarr.splice(tempIndex,1);
       sethead();
      }
     }
   }
  }
  public function sethead():void {
   switch (keyarr.length) {
    case 1 :
     mc.dx=0;
     mc.dy=0;
     heading=0;
     mc.gotoAndStop("zhan");
     return;

    case 2 :
     heading=keyarr[1];
     zhou();
     mc.gotoAndStop("run");
     return;

    case 3 :
     if (Math.abs(keyarr[1] - keyarr[2]) > 5) {
      heading=keyarr[1] + keyarr[2];
      xie();
      mc.gotoAndStop("run");
      return;
     } else {
      heading=keyarr[1];
      zhou();
      mc.gotoAndStop("run");
      return;
     }
    case 4 :
     if (Math.abs(keyarr[1] - keyarr[2]) > 5) {
      if (heading == keyarr[1] + keyarr[2]) {
       xie();
       mc.gotoAndStop("run");
       return;
      } else {
       heading == keyarr[1] + keyarr[3];
       xie();
       mc.gotoAndStop("run");
       return;
      }
     }
   }
  }
  public function zhou(n:uint=1,clearTheOtherAxis:Boolean=true):void {// 物体改为在轴方向移动
   switch (keyarr[n]) {
    case 87 :
     mc.dy=- step;
     break;

    case 83 :
     mc.dy=step;
     break;
    case 65 :
     mc.dx=- step;
     mc.scaleX=-1;
     break;

    case 68 :
     mc.dx=step;
     mc.scaleX=1;
     break;
   }
   if (clearTheOtherAxis) {
    if (keyarr[n] > 80) {// W或S
     mc.dx=0;
    } else {
     mc.dy=0;
    }
   }
  }
  public function xie(n:uint=2):void {
   zhou(1,false);
   zhou(n,false);
  }
 }
}

***********************************************************************************

Player.as:

package {
 import flash.display.MovieClip;
 import flash.events.Event;
 public class Player extends MovieClip {
  public var dx:Number;
  public var dy:Number;
  public function Player() {
   dx=0;
   dy=0;
   this.addEventListener(Event.ENTER_FRAME,onEnter);
  }
  private function onEnter(e:Event) {
   this.x+= dx;
   this.y+= dy;
  }
 }
}

标签集:TAGS:AS3
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}