英雄状态机

来源:互联网 发布:国家政务数据开放网站 编辑:程序博客网 时间:2024/04/29 21:31
游戏在进行过程中,会涉及各种状态的切换,比如一个人有攻击,移动,站立三种状态,攻击可以和移动互相转换状态,移动和站立也可以互相转动状态,攻击和站立也可以互相转换动态,这就符合有限状态机的时候,当情况简单的时候,可以在一个类中进行这三种状态的转换,但是当情况复杂,那么可以每种状态一个类,再把拥有各种状态的类,也就是人这个对象,传到状态里,再进行操作。请看以下代码。
[javascript] view plain copy
  1. var Player = cc.Sprite.extend({  
  2.     currentState:null,//当前状态  
  3.     addDistance:0,  
  4.     ctor:function(){          
  5.         this._super();  
  6.         this.actionArr = ["攻击1-斜方刺击","攻击2_直线刺击","横劈","攻击4_跃起砍击"];  
  7.         this.armature = ccs.Armature.create("zhaoyun");  
  8.         this.addChild(this.armature);  
  9.         this.breathe();  
  10.     },  
  11.     //更新  
  12.     update:function(enemyArr){  
  13.         this.seekEnemy(enemyArr);  
  14.         if(this.direction == 1){  
  15.             this.x += 5;  
  16.         }else if(this.direction == -1){  
  17.             this.x -= 5;  
  18.         }  
  19.     },  
  20.       
  21.     //探索敌人  
  22.     seekEnemy:function(enemyArr){  
  23.         if(!enemyArr){  
  24.             return;  
  25.         }  
  26.         for(var i in enemyArr){  
  27.             var enemy = enemyArr[i];  
  28.             if(Math.abs(this.x - enemy.x) <= 200){//如果自己的距离敌人足够近了  
  29.                 this.attack(enemy);  
  30.             }  
  31.         }  
  32.     },  
  33.       
  34.     //攻击  
  35.     attack:function(enemy){  
  36.         if(this.isCanAttack()){  
  37.             if(this.currentState == "run" || this.currentState == "breathe"){//简单状态机,从其他模式转化为攻击模式  
  38.                 this.currentState = "attack";  
  39.                 this.armature.getAnimation().play("攻击1-斜方刺击");  
  40.                 var func = function(armature, movementType){  
  41.                     if (movementType == ccs.MovementEventType.loopComplete && this.currentState == "attack") {  
  42.                         this.breathe();  
  43.                         enemy.hurt();  
  44.                     }  
  45.                 }    
  46.                 this.armature.getAnimation().setMovementEventCallFunc(func,this);  
  47.             }  
  48.         }  
  49.     },  
  50.       
  51.     //是否可以攻击  
  52.     isCanAttack:function(){  
  53.         if(this.currentState == "run"){  
  54.             return false;  
  55.         }  
  56.         return true;  
  57.     },  
  58.       
  59.     //呼吸  
  60.     breathe:function(){  
  61.         if(!this.currentState || this.currentState == "run" || this.currentState == "attack"){//简单状态机,从其他模式转变为呼吸模式  
  62.             this.currentState = "breathe";  
  63.             //this.direction = 0;  
  64.             this.armature.getAnimation().play("呼吸");//默认站着呼吸  
  65.         }  
  66.     },  
  67.       
  68.     //站立  
  69.     stand:function(){  
  70.         this.direction = 0;  
  71.         this.breathe();  
  72.     },  
  73.       
  74.     //跑步  
  75.     run:function(){  
  76.         if(this.currentState == "attack" || this.currentState == "breathe"){//简单状态机,从其他模式转变为跑步模式  
  77.             this.currentState = "run";  
  78.             this.armature.getAnimation().play("跑步_改");//默认站着呼吸  
  79.         }  
  80.     },  
  81.       
  82.     //向左移动  
  83.     moveLeft:function(){  
  84.         this.direction = -1;  
  85.         this.run();  
  86.     },  
  87.       
  88.     //向右移动  
  89.     moveRight:function(){  
  90.         this.direction = 1;  
  91.         this.run();  
  92.     }  
  93. }) 
0 0
原创粉丝点击