cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角)

cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角),第1张

概述主角小鸟有三种状态:idle、fly、die。 idle状态下,小鸟会挥动翅膀,原地不动,且不受重力的影响。 fly状态下,也就是游戏过程中小鸟移动,此状态下小鸟挥动翅膀飞行移动且受重力的影响。 die状态下,游戏结束了,小鸟死亡倒地了。 所以先设计一个枚举来表示小鸟的三种状态: /** * The leading role, bird's three action state */typede

主角小鸟有三种状态:IDle、fly、dIE。
IDle状态下,小鸟会挥动翅膀,原地不动,且不受重力的影响。
fly状态下,也就是游戏过程中小鸟移动,此状态下小鸟挥动翅膀飞行移动且受重力的影响。
dIE状态下,游戏结束了,小鸟死亡倒地了。
所以先设计一个枚举来表示小鸟的三种状态:

/** * The leading role,bird's three action state */typedef enum {  kActionStateIDle = 1,/* the IDle state,wave wing but gravity */  kActionStateFly,/* the fly state,wave wing and be affected by gravity */  kActionStateDIE  /* the dIE state,the bird dIE in the ground */} ActionState;

先重点说说创建小鸟对象方法:

// 小鸟有三种颜色,因此每次游戏开始时,会随机生成一种颜色.// 然后创建小鸟精灵,如果创建成功,则创建小鸟主角在游戏中需要执行的动作:IDle和wing。bool BirdSprite::createBird() {  // Randomly generate a bird color  srand((unsigned)time(NulL));  int type = rand() % 3;  char birdname[10];  char birdnameFormat[10];  sprintf(birdname,"bird%d_%d",type,type);  sprintf(birdnameFormat,"bird%d_%%d",type);  auto spriteFrame = AtlasLoader::getInstance()->getSpriteFrame(birdname);  // Create a bird sprite  auto isInitSuccessful = Sprite::initWithSpriteFrame(spriteFrame);  if (isInitSuccessful) {    // init IDle status    // create the bird IDle animation    auto animation = createAnimation(birdnameFormat,3,10);    _IDleAction = RepeatForever::create(Animate::create(animation));    // create the bird waving wing animation    auto upAction = MoveBy::create(0.4f,Vec2(0,8));    auto downAction = upAction->reverse();    _wingAction = RepeatForever::create(Sequence::create(upAction,downAction,NulL));    return true;  }  return false;}

接下来很重要的一个方法就是修改小鸟主角状态的方法:

// 修改小鸟的状态,会对应执行相应的动作// IDle状态,也就是游戏准备开始时的状态,小鸟挥动翅膀原地不动// fly状态,游戏开始了,小鸟受重力影响,由玩家控制// dIE状态,游戏结束,撤消小鸟所有的动作voID BirdSprite::setActionState(ActionState state) {  _actionState = state;  switch (state) {    case kActionStateIDle:      // The IDle state,the bird waves the wing and doesn't being affected by gravity      this->runAction(_IDleAction);      this->runAction(_wingAction);      break;    case kActionStateFly:      // The fly state,the bird waves the wing,affected by gravity      this->stopAction(_wingAction);      this->getPhysicsBody()->setGravityEnable(true);      break;    case kActionStateDIE:      // Thd dIE state,the bird get down to the ground.      this->stopAllActions();      break;    default:      break;  }}

下一步,说说游戏的流程

总结

以上是内存溢出为你收集整理的cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角)全部内容,希望文章能够帮你解决cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1050062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存