cocos2dx3.3开发FlappyBird总结六:设计共享小鸟类(主角)
2015-03-20 10:42
417 查看
主角小鸟有三种状态:idle、fly、die。
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总结十:背景层设计
- cocos2dx3.3开发FlappyBird总结十二:状态层设计
- cocos2dx3.3开发FlappyBird总结三:项目剖析
- cocos2dx3.3开发FlappyBird总结九:欢迎场景(WelcomeScene)
- cocos2dx3.3开发FlappyBird总结四:资源管理类
- cocos2dx3.3开发FlappyBird总结十七:结束语
- cocos2dx3.3开发FlappyBird总结七:说说游戏流程
- cocos2dx3.3开发FlappyBird总结十五:记录玩家得分
- cocos2dx3.3开发FlappyBird总结一:背景介绍
- cocos2dx3.3开发FlappyBird总结八:载入场景LoadingScene
- cocos2dx3.3开发FlappyBird总结十三:数字特效类
- cocos2dx3.3开发FlappyBird总结十四:常量定义
- cocos2dx3.3开发FlappyBird总结五:说说屏幕适配
- cocos2dx3.3开发FlappyBird总结十六:游戏层实现
- cocos2dxFlappyBird开发总结二:开发环境介绍
- 【cocos2dx-3.0beta-制作flappybird】英雄都是孤独的?--小鸟单例类的设计以及小鸟类的设计
- WinForm控件开发总结(十一)-----调试控件的设计时行为
- WinForm控件开发总结(十一)-----调试控件的设计时行为
- 02.21 收费系统二次开发总结 MVC UML 设计模式 .NET