笨木头 Little Runner

前端之家收集整理的这篇文章主要介绍了笨木头 Little Runner前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 写笔记主要是为了同强大的忘性作斗争,学习《Cocos2d-x 3.x游戏开发之旅》所记。

2. 以下参照书本,跑出来的效果图,所以主要还是把代码贴上来,方便日后查询



3.下面开始贴代码

创建基类,为人物和金币所继承.Entity 继承与Node.

  1. #ifndef _ENTITY_H_
  2. #define _ENTITY_H_
  3.  
  4. #include"cocos2d.h"
  5. USING_NS_CC;
  6.  
  7. class Entity : public Node
  8. {
  9. public:
  10. Entity();
  11. ~Entity();
  12. Sprite* getSprite();
  13. void bindSprite(Sprite* sprite);
  14. private:
  15. Sprite* m_sprite;
  16. };
  17. #endif
  18.  
  19. #include"Entity.h"
  20. #include"Player.h"
  21.  
  22. Entity::Entity()
  23. {
  24. m_sprite = NULL;
  25. }
  26.  
  27. Entity::~Entity()
  28. {
  29. }
  30.  
  31. Sprite* Entity::getSprite()
  32. {
  33. return this->m_sprite;
  34. }
  35.  
  36. void Entity::bindSprite(Sprite* sprite)
  37. {
  38. this->m_sprite = sprite;
  39. this->addChild(m_sprite);
  40. /*由于需要做碰撞检测,而Node的默认宽高属性(0,0),所以需要获取精灵的宽高,设置给Entity.这样Entity就有了Sprite对象的宽高。*/
  41. Size size = m_sprite->getContentSize();
  42. m_sprite->setPosition(Point(size.width*0.5f,size.height*0.5f));
  43. this->setContentSize(size);
  44. }

2.创建主角类Player和金币类Monster.
  1. #ifndef _PLAYER_H_
  2. #define _PLAYER_H_
  3. #include"cocos2d.h"
  4. #include"Entity.h"
  5.  
  6. using namespace cocos2d;
  7.  
  8. #define JUMP_ACTION_TAG 1
  9. class Player : public Entity
  10. {
  11. public:
  12. Player();
  13. ~Player();
  14. CREATE_FUNC(Player);
  15. virtual bool init();
  16. void jump();
  17. void hit();
  18. int getiHP();
  19. void resetData();
  20. private:
  21. bool b_isJumping;
  22. int m_iHP;
  23. };
  24. #endif
  25.  
  26. #include"Player.h"
  27. //#include"FlowWord.h"
  28. Player::Player()
  29. {
  30. b_isJumping = false;
  31. m_iHP = 1000;
  32. }
  33.  
  34. Player::~Player()
  35. {}
  36.  
  37. bool Player::init()
  38. {
  39. return true;
  40. }
  41.  
  42. void Player::hit()//碰撞时,人物的动作
  43. {
  44. if(getSprite() == NULL)
  45. {
  46. return;
  47. }
  48.  
  49. m_iHP -= 15;
  50. if(m_iHP < 0)
  51. {
  52. m_iHP = 0;
  53. }
  54.  
  55. //FlowWord* flowWord = FlowWord::create();
  56. //this->addChild(flowWord);
  57. //flowWord->showWord("-15",GetSpoolFileHandle);
  58.  
  59. auto backMove = MoveBy::create(0.1f,Point(-20,0));
  60. auto forwardMove = MoveBy::create(0.1f,Point(20,0));
  61. auto backRotate = RotateBy::create(0.1f,-5,0);
  62. auto forwardRotate = RotateBy::create(0.1f,5,0);
  63.  
  64. auto backAction = Spawn::create(backMove,backRotate,NULL);
  65. auto forwardAction = Spawn::create(forwardMove,forwardRotate,NULL);
  66. auto actions = Sequence::create(backAction,forwardAction,NULL);
  67.  
  68. stopAllActions();
  69. resetData();
  70. runAction(actions);
  71. }
  72.  
  73. void Player::resetData()
  74. {
  75. if(b_isJumping)
  76. {
  77. b_isJumping = false;
  78. }
  79. setPosition(Point(200,140));
  80. setScale(1.0f);
  81. setRotation(0);
  82. }
  83.  
  84. int Player::getiHP()
  85. {
  86. return m_iHP;
  87. }
  88.  
  89. void Player::jump()
  90. {
  91. if(getSprite() == NULL)
  92. return;
  93.  
  94. if(b_isJumping)
  95. return;
  96.  
  97. b_isJumping = true;
  98.  
  99. auto jump = JumpBy::create(2.0f,Point(0,0),250,1);//原地跳跃,高度250像素,跳跃一次
  100. auto callFunc = CallFunc::create([&]()
  101. {
  102. b_isJumping = false;
  103. });
  104.  
  105. auto jumpAction = Sequence::create(jump,callFunc,NULL);
  106. this->runAction(jumpAction);
  107. }
  108. <pre name="code" class="cpp">#ifndef _MONSTER_H_
  109. #define _MONSTER_H_
  110. #include"Entity.h"
  111. #include"Player.h"
  112. class Monster:public Entity
  113. {
  114. public:
  115. Monster();
  116. ~Monster();
  117. CREATE_FUNC(Monster);
  118. virtual bool init();
  119. public:
  120. void show();
  121. void hide();
  122. void reset();
  123. bool isAlive();
  124. bool isCollideWithPlayer(Player* player);
  125. private:
  126. bool b_isAlive;
  127. };
  128. #endif
  129.  
  130. #include"Monster.h"
  131.  
  132. Monster::Monster()
  133. {
  134. b_isAlive = false;
  135. }
  136.  
  137. Monster::~Monster()
  138. {}
  139.  
  140. bool Monster::init()
  141. {
  142. return true;
  143. }
  144. void Monster::show()
  145. {
  146. if(getSprite() != NULL)
  147. {
  148. setVisible(true);
  149. b_isAlive = true;
  150. }
  151. }
  152.  
  153. void Monster::hide()
  154. {
  155. if(getSprite() != NULL)
  156. {
  157. setVisible(false);
  158. reset();
  159. b_isAlive = false;
  160. }
  161. }
  162.  
  163. void Monster::reset()
  164. {
  165. if(getSprite() != NULL)
  166. {
  167. setPosition(Point(800+CCRANDOM_0_1() * 2000,200-CCRANDOM_0_1()*100));
  168. }
  169.  
  170. }
  171.  
  172. bool Monster::isAlive()
  173. {
  174. return b_isAlive;
  175. }
  176.  
  177. bool Monster::isCollideWithPlayer(Player* player)
  178. {
  179. if(player == NULL || getSprite() == NULL)
  180. return false;
  181. Rect entityRect = player->getBoundingBox();//获取碰撞检测对象的boundingBox
  182. Point monsterPos = getPosition();
  183. return entityRect.containsPoint(monsterPos);
  184. }

3.金币管理类,负责金币的创建,管理。

  1. #ifndef _MANAGERMONSTER_H_
  2. #define _MANAGERMONSTER_H_
  3.  
  4. #include "cocos2d.h"
  5. #include "Monster.h"
  6.  
  7. USING_NS_CC;
  8.  
  9. #define MAX_MONSTER_NUM 10
  10. class MonsterManager:public Node
  11. {
  12. public:
  13. CREATE_FUNC(MonsterManager);
  14. virtual bool init();
  15. virtual void update(float dt);
  16.  
  17. private :
  18. void createMonster();
  19. public:
  20. void bindPlayer(Player* player);
  21. private:
  22. Vector<Monster*> m_monsterArr;//Vector容器的应用
  23. Player* m_player;
  24. };
  25. #endif
  26.  
  27. #include"MonsterManager.h"
  28. #include"Player.h"
  29. #include"Monster.h"
  30.  
  31. bool MonsterManager::init()
  32. {
  33. createMonster();
  34. this->scheduleUpdate();
  35. return true;
  36. }
  37.  
  38. void MonsterManager::createMonster()
  39. {
  40. Monster* monster = NULL;
  41. Sprite* sprite = NULL;
  42.  
  43. for(int i = 0; i < MAX_MONSTER_NUM; i++)
  44. {
  45. monster = Monster::create();
  46. monster->bindSprite(Sprite::create("monster.png"));
  47. monster->reset();
  48. this->addChild(monster);
  49. m_monsterArr.pushBack(monster);
  50. }
  51. }
  52. void MonsterManager::update(float dt)
  53. {
  54. for(auto monster:m_monsterArr)
  55. {
  56. if(monster == NULL)
  57. continue;
  58. if(monster->isAlive())
  59. {
  60. monster->setPositionX(monster->getPositionX() - 4);
  61.  
  62. if(monster->getPositionX() < 0)
  63. {
  64. monster->hide();
  65. }
  66. }
  67. else
  68. {
  69. monster->show();
  70. }
  71.  
  72. if(monster->getPositionX() < 0)
  73. {
  74. monster->hide();
  75. }
  76. else if(monster->isCollideWithPlayer(m_player))
  77. {
  78. m_player->hit();
  79. monster->hide();
  80. }
  81. }
  82. }
  83.  
  84. void MonsterManager::bindPlayer(Player* player)
  85. {
  86. m_player = player;
  87. }

4.创建场景类
  1. #ifndef _TOLLGATESCENE_H_
  2. #define _TOLLGATESCENE_H_
  3.  
  4. #include "cocos2d.h"
  5. using namespace cocos2d;
  6.  
  7. #include"editor-support/cocostudio/CCSGUIReader.h"
  8. #include"ui/CocosGUI.h"
  9.  
  10. using namespace cocos2d::ui;
  11. using namespace cocostudio;
  12. class Player;
  13.  
  14. class TollgateScene : public Layer
  15. {
  16. public:
  17. static Scene* createScene();
  18. virtual bool init();
  19. CREATE_FUNC(TollgateScene);
  20. virtual void update(float delta);//update函数调用
  21. private:
  22. void initBG();
  23. void loadUI();
  24. void jumpEvent(Ref*,TouchEventType type);
  25.  
  26. private:
  27. Sprite* m_bg1;
  28. Sprite* m_bg2;
  29. Player* m_player;
  30. int m_iscore;
  31. Text* m_scoreLab;
  32. LoadingBar* m_hpBar;
  33. };
  34. #endif
  35.  
  36. #include"TollgateScene.h"
  37. #include"Player.h"
  38. #include"Entity.h"
  39. #include"cocos2d.h"
  40. #include"MonsterManager.h"
  41.  
  42. Scene* TollgateScene::createScene()
  43. {
  44. // 'scene' is an autorelease object
  45. auto scene = Scene::create();
  46. // 'layer' is an autorelease object
  47. auto layer = TollgateScene::create();
  48.  
  49. // add layer as a child to scene
  50. scene->addChild(layer);
  51.  
  52. // return the scene
  53. return scene;
  54. }
  55.  
  56. bool TollgateScene::init()
  57. {
  58. if(!Layer::init())
  59. {
  60. return false;
  61. }
  62. //尽量不要使用多线程,Cocos2d-x的Node对象提供了一个update函数,在游戏的每一帧都会调用update函数,我们只要调用它便可以
  63. this->scheduleUpdate();//这句表示我们开启调用update函数功能
  64.  
  65. Size visibleSize = Director::getInstance()->getVisibleSize();
  66.  
  67. Sprite* titleSprite = Sprite::create("title.png");
  68. titleSprite->setPosition(Point(visibleSize.width/2,visibleSize.height-50));
  69. this->addChild(titleSprite,2);
  70.  
  71. m_player = Player::create();
  72. m_player->bindSprite(Sprite::create("sprite.png"));
  73. m_player->setPosition(Point(200,visibleSize.height / 4));
  74. this->addChild(m_player,3);
  75.  
  76. initBG();
  77.  
  78. MonsterManager* monsterMgr = MonsterManager::create();
  79. this->addChild(monsterMgr,4);
  80.  
  81. monsterMgr->bindPlayer(m_player);
  82.  
  83. m_iscore = 0;
  84.  
  85. return true;
  86. }
  87.  
  88. void TollgateScene::initBG()
  89. {
  90. Size visibleSize = Director::getInstance()->getVisibleSize();
  91. m_bg1 = Sprite::create("tollgateBG.jpg");
  92. m_bg1->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
  93. this->addChild(m_bg1,0);
  94.  
  95. m_bg2 = Sprite::create("tollgateBG.jpg");
  96. m_bg2->setPosition(Point(visibleSize.width+visibleSize.width/2,visibleSize.height/2));
  97. m_bg2->setFlippedX(true);
  98. this->addChild(m_bg2,0);
  99. loadUI();
  100. }
  101.  
  102. void TollgateScene::update(float delta) //<span style="font-family: Arial,Helvetica,sans-serif;">开启调用update函数功能,需要加this->scheduleUpdate()</span>
  103. {
  104. int posX1 = m_bg1->getPositionX();
  105. int posX2 = m_bg2->getPositionX();
  106.  
  107. int speed = 1;
  108.  
  109. posX1 -= speed;
  110. posX2 -= speed;
  111.  
  112. m_bg1->setPositionX(posX1);
  113. m_bg2->setPositionX(posX2);
  114.  
  115. Size mapsize = m_bg1->getContentSize();
  116.  
  117. if(posX1 <= -mapsize.width/2)
  118. {
  119. posX1 = mapsize.width + mapsize.width/2;
  120. }
  121.  
  122. if(posX2 <= -mapsize.width/2)
  123. {
  124. posX2 = mapsize.width + mapsize.width/2;
  125. }
  126.  
  127. m_bg1->setPositionX(posX1);
  128. m_bg2->setPositionX(posX2);
  129.  
  130. m_iscore += 1;
  131. m_scoreLab->setText(Value(m_iscore).asString());
  132. //m_hpBar->setPercent(100);
  133. m_hpBar->setPercent(m_player->getiHP()/1000.0f*100);
  134. }
  135.  
  136. void TollgateScene::loadUI()//加载cocostudio的控件
  137. {
  138. auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LittleRunnerUI_1.ExportJson");
  139. this->addChild(UI);
  140.  
  141. auto jumpBtn = (Button*)Helper::seekWidgetByName(UI,"JumpBtn");
  142. jumpBtn->setTitleText("Jump");
  143. jumpBtn->addTouchEventListener(this,toucheventselector(TollgateScene::jumpEvent));//跳跃动作的监听
  144.  
  145. m_scoreLab = (Text*)Helper::seekWidgetByName(UI,"scoreLab");
  146. m_hpBar = (LoadingBar*)Helper::seekWidgetByName(UI,"ProgressBar_11");
  147. }
  148.  
  149. void TollgateScene::jumpEvent(Ref*,TouchEventType type)
  150. {
  151. switch(type)
  152. {
  153. case TouchEventType::TOUCH_EVENT_ENDED:
  154. m_player->jump();
  155. break;
  156. }
  157. }

5 启动

  1. #ifndef _APP_DELEGATE_H_
  2. #define _APP_DELEGATE_H_
  3.  
  4. #include "cocos2d.h"
  5.  
  6. /**
  7. @brief The cocos2d Application.
  8.  
  9. The reason for implement as private inheritance is to hide some interface call by Director.
  10. */
  11. class AppDelegate : private cocos2d::Application
  12. {
  13. public:
  14. AppDelegate();
  15. virtual ~AppDelegate();
  16.  
  17. /**
  18. @brief Implement Director and Scene init code here.
  19. @return true Initialize success,app continue.
  20. @return false Initialize @R_404_159@,app terminate.
  21. */
  22. virtual bool applicationDidFinishLaunching();
  23.  
  24. /**
  25. @brief The function be called when the application enter background
  26. @param the pointer of the application
  27. */
  28. virtual void applicationDidEnterBackground();
  29.  
  30. /**
  31. @brief The function be called when the application enter foreground
  32. @param the pointer of the application
  33. */
  34. virtual void applicationWillEnterForeground();
  35. };
  36.  
  37. #endif // _APP_DELEGATE_H_
  38.  
  39. #include "AppDelegate.h"
  40. #include "HelloWorldScene.h"
  41. #include "TollgateScene.h"
  42. USING_NS_CC;
  43.  
  44. AppDelegate::AppDelegate() {
  45.  
  46. }
  47.  
  48. AppDelegate::~AppDelegate()
  49. {
  50. }
  51.  
  52. bool AppDelegate::applicationDidFinishLaunching() {
  53. // initialize director
  54. auto director = Director::getInstance();
  55. auto glview = director->getOpenGLView();
  56. if(!glview) {
  57. glview = GLView::create("My Game");
  58. glview->setFrameSize(800,500);//只针对windos平台,移植到手机后这些设置是无效的。由手机屏幕来决定大小
  59. director->setOpenGLView(glview);
  60. }
  61.  
  62. // turn on display FPS
  63. director->setDisplayStats(false);
  64.  
  65. // set FPS. the default value is 1.0/60 if you don't call this
  66. director->setAnimationInterval(1.0 / 60);
  67.  
  68. // create a scene. it's an autorelease object
  69. auto scene = TollgateScene::createScene();
  70.  
  71. // run
  72. director->runWithScene(scene);
  73.  
  74. return true;
  75. }
  76.  
  77. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  78. void AppDelegate::applicationDidEnterBackground() {
  79. Director::getInstance()->stopAnimation();
  80.  
  81. // if you use SimpleAudioEngine,it must be pause
  82. // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  83. }
  84.  
  85. // this function will be called when the app is active again
  86. void AppDelegate::applicationWillEnterForeground() {
  87. Director::getInstance()->startAnimation();
  88.  
  89. // if you use SimpleAudioEngine,it must resume here
  90. // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  91. }
6.不要在写代码的时候,把所有的功能都往一个类里面写,多分开几个类来写,多写一些注释,以便后面要修改或者查阅看不懂。

猜你在找的Cocos2d-x相关文章