创建敌人基类

来源:互联网 发布:stc单片机pwm控制舵机 编辑:程序博客网 时间:2024/04/28 12:20
class EnemyBase : public Sprite  {  public:          virtual bool init() override;      CREATE_FUNC(EnemyBase);      Animation* createAnimation(std::string prefixName, int framesNum, float delay);      void changeDirection(float dt);      Node* currPoint();      Node* nextPoint();      void runFllowPoint();      void setPointsVector(Vector<Node*> points);      private:      Vector<Node*> pointsVector;  protected:      int pointCounter;      Animation *animationRight;      Animation *animationLeft;      CC_SYNTHESIZE(float, runSpeed, RunSpeed);      };
复制代码
复制代码
//实现Node* EnemyBase::currPoint()  {      return this->pointsVector.at(pointCounter);  }  Node* EnemyBase::nextPoint()  {      int maxCount = this->pointsVector.size();  pointCounter++;  if (pointCounter < maxCount  ){  auto node =this->pointsVector.at(pointCounter);          return node;      }      else{          pointCounter = maxCount -1 ;      }      return NULL;  }
复制代码
复制代码
//小偷是敌人要继承敌人基类class Thief : public EnemyBase  {  public:      virtual bool init() override;          static Thief* createThief(Vector<Node*> points);  };
复制代码
复制代码
//在基类已经给出了敌人的各种逻辑方法,所以在Thief中,我们只需要初始化变量,实现具体的方法,就可以实现一个很普通的敌人了。   bool Thief::init()  {  if (!Sprite::init())  {  return false;  }  // 1      setRunSpeed(6);      animationRight = createAnimation("enemyRight1", 4, 0.1f);  AnimationCache::getInstance()->addAnimation(animationRight, "runright");      animationLeft = createAnimation("enemyLeft1", 4, 0.1f);  AnimationCache::getInstance()->addAnimation(animationLeft, "runleft");  // 2      schedule(schedule_selector(EnemyBase::changeDirection), 0.4f);  return true;  }
复制代码
复制代码
//创建小偷的接口函数Thief* Thief::createThief(Vector<Node*> points)  {      Thief *pRet = new Thief();      if (pRet && pRet->init())      {       // 设置小偷的路径点集          pRet->setPointsVector(points);          // 让小偷沿着路径点移动          pRet->runFllowPoint();          pRet->autorelease();          return pRet;      }      else      {          delete pRet;          pRet = NULL;          return NULL;      }  }
0 0
原创粉丝点击