好久没玩flash

抓紧时间操练了一把,用下面代码作了一个粉简单的坦克控制:)

ACTIONSCRIPT:
  1. class Move extends MovieClip {
  2.  
  3.  private var speed = 3;
  4.  private var xMin = 0;
  5.  private var yMin = 0;
  6.  private var xMax = 400;
  7.  private var yMax = 400;
  8.  
  9.  public function onLoad(){
  10.   Key.addListener(this);
  11.   this.xMin += this._width/2;
  12.   this.xMax -= this._width/2;
  13.   this.yMin += this._height/2;
  14.   this.yMax -= this._height/2;
  15.  }
  16.  
  17.  public function onEnterFrame(){
  18.   if( this._x <this.xMin ){
  19.    this._x = this.xMin;
  20.   }
  21.   if( this._x> this.xMax ){
  22.    this._x = this.xMax;
  23.   }
  24.   if( this._y <this.yMin ){
  25.    this._y = this.yMin;
  26.   }
  27.   if( this._y> this.yMax ){
  28.    this._y = this.yMax;
  29.   } 
  30.  }
  31.  
  32.  public function setSpeed(speed:Number){
  33.   this.speed = speed;
  34.  }
  35.  
  36.  public function onKeyDown(){
  37.   switch(Key.getCode()){
  38.    case 83: // s
  39.     this._rotation = 180;
  40.     this._y += this.speed;
  41.     break;
  42.    
  43.    case 65: // a
  44.     this._rotation = -90;
  45.     this._x -= this.speed;
  46.     break;
  47.    
  48.    case 68: // d
  49.     this._rotation = 90;
  50.     this._x += this.speed;
  51.     break;
  52.    
  53.    case 87: // w
  54.     this._rotation = 0;
  55.     this._y -= this.speed;
  56.   }
  57.  }
  58.  
  59.  private function onRelease(){
  60.   trace(’test’);
  61.  }
  62. }

作者: Volcano 发表于August 3, 2002 at 8:33 am

版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原始出处作者信息及此声明

Tags:

留条评论