Flash笔记之刷新人物

来源:互联网 发布:最好吃的泡面知乎 编辑:程序博客网 时间:2024/05/09 10:48
 

clockWise//是否顺时针播放

rameCount="8" 表示八方向,每方向8帧。
frameRate="6" 表示人物刷新频率。_curFrame(当前帧和frameRate比较)
isLoop="1"    表示是否循环播放。如果人物死亡,就为0 ,不播放。


1--8代表图片m9444_stand_(1--8)

      7   0(1-8)    1(9-16)
  \  |   /
   \ |  /
 6-----------2(17-24)
   / | \
        5  4(33-40)  3(25-32)

 

    

5(40-47)
6(48-55)
7(56-63)

if(_dir >= ScaleEnum.CLOCKWISE_BEGIN_SCALE_DIR && _dir <= ScaleEnum.CLOCKWISE_END_SCALE_DIR){
 if(_dir == 5){
     curDir = 3;
 }else if(_dir == 6){
     curDir = 2;
 }else if(_dir == 7){
     curDir = 1;
 }
 _scaleFrame = curDir * _frameNum(八方向) + 1;
 this.hasFlip = true;
 1.因为5是镜像3的图片,6是镜像2的图片,7是镜像1的图片
 2.如果如果当前是_dir =5 那么久镜像3图片
 3._scaleFrame =25帧(为什么要去25帧,这是因为找到m9811_stand_25这张png图片,再根据 this.hasFlip=true
 把图片this.scaleX = -1;当this.scaleX 为负数时候,翻转功能。
 )    
 4.this.hasFlip = true镜像一定为true
 5.如果是5,那么读取的还是3的xml值。
}else{     
 _scaleFrame = _dir * _frameNum + 1;
 this.hasFlip = false;
 1.如果当前是_dir =4
 2._scaleFrame =33帧
 3.this.hasFlip = false 不镜像
}
this._scaleIndex = _scaleFrame;

this._beginFrame = _dir * _frameNum + 1;         //如果当前方向是4(33--40),那么_beginFrame:33
this._loopFrame = _dir * _frameNum + _frameNum;  //如果当前方向是4(33--40),那么_loopFrame:40 

this._frameIndex = _beginFrame;//_frameIndex = 33

if(this.keyFrame > 0)
this.curKeyFrame = this._frameIndex + this.keyFrame;//this.curKeyFrame=34
   

goToFrame(this._frameIndex,this._scaleIndex,this.hasFlip);
this._curFrame = 0;

 package mmo.render.core.avatar.body {
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.geom.Point;
 import flash.geom.Rectangle;
 
 import mmo.render.core.avatar.IAvatarBody;
 import mmo.render.core.enums.ActionType;
 import mmo.render.core.enums.AvatarEnum;
 import mmo.render.core.enums.ScaleEnum;
 import mmo.utils.SpaceKit;
 
 public class AvatarBody extends AvatarPart implements IAvatarBody
 {
  /*附属动画,可以一齐播放,要求fps与frames完全一致 */
  private var partArr:Array = null;
  
  /**发送命中包的关键帧*/
  public var keyFrame:int = -1;
  private var curKeyFrame:int = -1;
  
  private var _clockWise:Boolean = false;  //是否顺时针播放
  private var _curFrame:int = 0;
  protected var _scaleIndex:int = 0;  //当前播放帧
  protected var hasFlip:Boolean = false;
  protected var _dir:int = -1;  //方向
  private var _frameIndex:int = 0;  //当前播放帧
  private var _loopFrame:int = 0;
  private var _beginFrame:int = 0;
  
  private var _scaleFrame:int = 0;  //用作取图
  
  private var _isLoop:Boolean;  //是否循环播放
  private var _isKeyFrame:Boolean;  //是否启用关键帧监听
  //  private var _isPause:Boolean;  //是否暂停播放
  private var p:Point = null;
  private var _isOver:Boolean;
  
  private var dic_frameRate:Object;
  private var dic_frameNumber:Object;
  private var dic_isLoop:Object;
  
  public function AvatarBody(renderType:uint/*_shape_mc:Shape*/)
  {
   super(renderType/*_shape_mc*/);
   partArr = new Array();
   _isLoop = true;
   _isKeyFrame = false;
   //   _isPause = false;
   
   dic_frameRate = {};
   dic_frameNumber = {};
   dic_isLoop = {};
  }
  
  public function set clockWise(value:int):void{
   if(value == 1)
    this._clockWise = true;
   else
    this._clockWise = false;
  }
  
  public function initActionData(action:String,frameNumber:int,frameRate:int,isLoop:Boolean=true):void{
   this.dic_frameRate[action] = frameRate;
   this.dic_frameNumber[action] = frameNumber;
   this.dic_isLoop[action] = isLoop;
  }
  
  public function addPart(part:Bitmap):void
  {
   if(this.partArr.indexOf(part)!=-1){
    return;
   }
   
   //   trace("添加avatar组件");
   this.partArr.push(part);
  }
  
  public function removedPart(part:Bitmap):void
  {
   var ind:int = this.partArr.indexOf(part);
   if(ind!=-1){
    //    trace("移除avatar组件");
    this.partArr.splice(ind, 1);
   }
  }
  
  /**
   * 单位图刷新  isOnebit是开关,true是采用但位图刷新机制
   * @param pointer
   * @param scaleIndex
   * @param hasScale
   * updated by chenyong
   */  
  public override function goToFrame(pointer:int,scaleIndex:int,hasScale:Boolean):void
  {
   if(!isOnebit)
   {
    super.goToFrame(pointer,scaleIndex,hasScale);
    for each(var part:AvatarPart in this.partArr){
     part.goToFrame(pointer,scaleIndex,hasScale);
    }
    return;
   }
           
   /**单位图刷新机制,从坐骑,人体,其他部件依次绘制 ->added by chenyong*/
   lastMovex = 0;
   lastMovex = 0;
   drawcount = 0;
   if(rideBody){//refresh坐骑
    updataBody(rideBody,pointer,scaleIndex,hasScale);
    updatePart(this,pointer,scaleIndex,hasScale);
   }
   else{
    //refresh 人avatarBody
    updataBody(this,pointer,scaleIndex,hasScale);   
   }
   //refresh部件,例如:刀
   updataParts(pointer,scaleIndex,hasScale);
  }
  /**
   * 绘制主体,主体是Avatar的基础,其他部分都会以主体为基础绘制
   * @param body 主体对象
   * @param pointer
   * @param scaleIndex
   * @param hasScale
   * added by chenyong
   */       
  private function updataBody(body : AvatarPart,pointer:int,scaleIndex:int,hasScale:Boolean):void
  {
   var bmdObject : Object = body.getPartData(pointer,scaleIndex,hasScale);
   bodyBmd = bmdObject.bitmapdata;
   bodyTx = bmdObject.tx;
   bodyTy = bmdObject.ty;
   setBaseXY();   
  }
  /**
   * 绘制部件,比如刀
   * @param pointer
   * @param scaleIndex
   * @param hasScale
   * added by chenyong
   */  
  private function updataParts(pointer:int,scaleIndex:int,hasScale:Boolean):void
  {
   if(partArr.length > 0)
   {
    for each (var part:AvatarPart in this.partArr)
    {
     updatePart(part,pointer,scaleIndex,hasScale);
    }
   }   
  }
  
  private function setBaseXY():void
  {
   if(partArr.length <= 0)
   {
    this.bitmapData = bodyBmd;
    if (this.scaleX == -1) {
     this.x = -bodyTx;
    } else {
     this.x = bodyTx;
    }
    this.y = bodyTy;
   }   
  }
  
  private var isOnebit:Boolean = true;//是否采用单位图渲染的开关
  private var bodyBmd : BitmapData;
  private var bodyTx : Number;
  private var bodyTy : Number;
  private var lastMovex : Number;
  private var lastMovey : Number;
  private var drawcount : int;
  private var rideBody : AvatarPart;
  public function setRideBody(body : AvatarPart) : void
  {
   rideBody = body;
  }
  /*
   * 单位图刷新方法 added by yong 2011-4-11
   */  
  private function updatePart(part : AvatarPart, pointer:int,scaleIndex:int,hasScale:Boolean):void
  {
   if(!bodyBmd)return;
   drawcount++;
   var tempObject : Object = part.getPartData(pointer, scaleIndex, hasScale);
   var bmd2 : BitmapData = tempObject.bitmapdata;
   var tx : Number = tempObject.tx;
   var ty : Number = tempObject.ty;
   var offx : Number = tx - bodyTx;//绘制部件与人物身体远点的偏移量
   var offy : Number = ty - bodyTy;
   var movex : Number = offx > 0 ? offx : -offx;//绘制部件在宽度上多出的值
   var movey : Number = offy > 0 ? 0 : -offy;
   
   var b1_width : Number = bodyBmd.width;//被绘制的部件宽度
   var b1_height : Number = bodyBmd.height;
   var b2_width : Number = bmd2.width + offx;//绘制部件的宽度
   var b2_height : Number = bmd2.height + offy;
   
   var bwidth : Number = b1_width > b2_width ? b1_width : b2_width;
   var bheight : Number = b1_height > b2_height ? b1_height : b2_height;
   
   var movexoff : Number = movex > lastMovex ? movex - lastMovex : 0;
   var moveyoff : Number = movey > lastMovey ? movey - lastMovey : 0;
   if(drawcount == 1)
   {
    movexoff = 0;
    moveyoff = 0; 
   }
   var offTx : Number = movex > lastMovex ? movex : lastMovex; 
   var offTy : Number = movey > lastMovey ? movey : lastMovey; 
   if(drawcount == 1)
   {
    offTx = movex;
    offTy = movey; 
   }      
   var baseX : Number = drawcount == 1?movex:movexoff;
   var baseY : Number = drawcount == 1?movey:moveyoff;    
   var destP1 : Point = new Point(baseX, baseY);
   var destP2 : Point = new Point(offTx + offx, offTy + offy);
   //trace(destP1,destP2);    
   var offwidth : Number;
   var offheight : Number;
   if(drawcount == 1)
   {
    offwidth = movex;
    offheight = movey; 
   }
   else
   {
    offwidth = movex > lastMovex ? movex - lastMovex : 0; 
    offheight = movey > lastMovex ? movey - lastMovey : 0;      
   }
   
   lastMovex = offTx;
   lastMovey = offTy;
   
   //通过以上计算最终需要获得位图的宽度和高度,以及部件绘制的原点坐标
   var bmd : BitmapData = new BitmapData(bwidth + offwidth, bheight + offheight, true, 0x00000000);
   var rec1 : Rectangle = new Rectangle(0, 0, bodyBmd.width, bodyBmd.height);
   var rec2 : Rectangle = new Rectangle(0, 0, bmd2.width, bmd2.height);
   //开始绘制到位图数据bmd上
   bmd.lock();
   bmd.copyPixels(bodyBmd, rec1, destP1, null, null, true);
   bmd.copyPixels(bmd2, rec2, destP2, null, null, true);
   bodyBmd = bmd;
   bmd.unlock();
   //计算位图的坐标,使其被加到该容器原点上时保持正确的坐标
   if (this.scaleX == -1) {
    this.x = -bodyTx + offTx;
   } else {
    this.x = bodyTx - offTx;
   }
   this.y = bodyTy - offTy;
   this.bitmapData = bmd;     
  }
  
  public function setDirection(dir:int):void
  {
   if(this._dir != dir){
    this._dir = dir;
    resetFrameIndex();
   }
  }
  
  public function getDirection():int{
   return this._dir;
  }
  
  public override function setAction(action:String,fnOver:Function = null,fnKey:Function = null):void
  {
   this.fnOver = fnOver;
   this.fnKey = fnKey;
   
//   if(this._action == ActionType.DEAD) return;
   if(this._action != action){
    //    trace(getTimer()+"改变动作:"+action);
    //    Debug.log("AvatarBody setAction " + action);
    
    this._action = action;
    for each(var part:AvatarPart in this.partArr){
     part.setAction(this._action);
    }
    if(rideBody)
    {
     rideBody.setAction(this._action);
    }
    //    Logger.debug("改变动作:"+this._action+"&_isLoop:"+_isLoop+"&_isKeyFrame:"+_isKeyFrame);
    resetFrameIndex();
   }
  }
  
  /*public override function setSex(sex:String):void
  {
  this._sex = sex;
  for each(var part:AvatarPart in this.partArr){
  part.setSex(sex);
  }
  }*/
  
  public override function setStyle(style:String):void
  {
   this._style = style;
   for each(var part:AvatarPart in this.partArr){
    part.setStyle(style);
   }
  }
  private var count:int = 0;
  /**判定动作是否循环,如果是不可循环的,该变量起作用,将停止人物动作的刷新-->added by chenyong*/
  private var _isEnd : Boolean = false;
  private function getFrameIndex():int
  {
   _frameIndex++;
   _scaleIndex++;
   
   if(_frameIndex > _loopFrame - 1)
   {
    if(!_isLoop){/*trace("----------->dead animation end");*/ _isEnd = true;}
   }
   
   if (_frameIndex > _loopFrame)
   {
    _frameIndex = _beginFrame;
//    if(this.keyFrame > 0) this.curKeyFrame = this._frameIndex + this.keyFrame;
    _scaleIndex = _scaleFrame;
    this.fnOver && fnOver();
   }
   /**播放到命中帧*/
   if(keyFrame > 0 && /* && */this._action == ActionType.ATTACK){
    if(_frameIndex == this.curKeyFrame)
     this.fnKey && this.fnKey();
   }
   /*else if (_frameIndex < _dir * _frameNum + 1)
   {
   _frameIndex=_beginFrame;
   }*/
   return _frameIndex;
  }
  
  private function resetFrameIndex():void{
   this._frameNum = this.dic_frameNumber[this._action];
   this._frameRate = this.dic_frameRate[this._action];
   this._isLoop = this.dic_isLoop[this._action];
   _isEnd = false;
   var curDir:int;
   if(_clockWise){
    if(_dir >= ScaleEnum.CLOCKWISE_BEGIN_SCALE_DIR && _dir <= ScaleEnum.CLOCKWISE_END_SCALE_DIR){
     if(_dir == SpaceKit.HEADING_BOTTOM){
      curDir = SpaceKit.HEADING_RIGHT;
     }else if(_dir == SpaceKit.HEADING_BOTTOM_LEFT){
      curDir = SpaceKit.HEADING_TOP_RIGHT;
     }else if(_dir == SpaceKit.HEADING_LEFT){
      curDir = SpaceKit.HEADING_TOP;
     }
     _scaleFrame = curDir * _frameNum + 1;
     this.hasFlip = true;
    }else{
     _scaleFrame = _dir * _frameNum + 1;
     this.hasFlip = false;
    }
   }else{
    if(_dir >= ScaleEnum.BEGIN_SCALE_DIR && _dir <= ScaleEnum.END_SCALE_DIR){
     if(_dir == SpaceKit.HEADING_RIGHT){
      curDir = SpaceKit.HEADING_BOTTOM;
     }else if(_dir == SpaceKit.HEADING_TOP_RIGHT){
      curDir = SpaceKit.HEADING_BOTTOM_LEFT;
     }else if(_dir == SpaceKit.HEADING_TOP){
      curDir = SpaceKit.HEADING_LEFT;
     }
     _scaleFrame = curDir * _frameNum + 1;
     this.hasFlip = true;
    }else{
     _scaleFrame = _dir * _frameNum + 1;
     this.hasFlip = false;
    }
   }
   
   this._scaleIndex = _scaleFrame;
   
   this._beginFrame = _dir * _frameNum + 1;
   this._loopFrame = _dir * _frameNum + _frameNum;
   this._frameIndex = _beginFrame;
   if(this.keyFrame > 0) this.curKeyFrame = this._frameIndex + this.keyFrame;
   
   //   Debug.log("重新计算帧数 当前动作Action:"+this._action+"&FrameIndex"+this._frameIndex+"&BeginFrame"+this._beginFrame+"&LoopFrame"+this._loopFrame);
   goToFrame(this._frameIndex,this._scaleIndex,this.hasFlip);
   this._curFrame = 0;
  }
  
  public function refresh():void{
   if(_isEnd)return;
   
   this._curFrame++;
   if(this._curFrame<=this._frameRate){
    return;
   }
   this._curFrame = 0;
   goToFrame(getFrameIndex(),_scaleIndex,this.hasFlip);
  }
  
  private function getKeyFrameByAction():int{
   //   _dir * _frameNum + 1
   switch(this._action){
    case ActionType.ATTACK : {
     return _dir * _frameNum + 1 + AvatarEnum.ATTACK_KEY_FRAME;
    }
     /*case ActionType.MAGIC : {
     return _dir * _frameNum + 1 + AvatarEnum.MAGIC_KEY_FRAME;
     }*/
     /*default : {
     return -1;
     }*/
   }
   return -1;
  }
 }
}

 

 

 

 

 

原创粉丝点击