【cocos2d-x 3.7 飞机大战】 决战南海I (十三) 分数场景

前端之家收集整理的这篇文章主要介绍了【cocos2d-x 3.7 飞机大战】 决战南海I (十三) 分数场景前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

分数场景要用到TableView,这个之前也没用过,主要参考网上的代码。cocos2d-x的功能十分强大,以后还有好多东西要学啊!


分数场景类的.h文件中的内容

  1. class scoreScene : public Layer,public TableViewDataSource,public TableViewDelegate
  2. {
  3. public:
  4. scoreScene();
  5. ~scoreScene();
  6. public:
  7. static Scene * createScene();
  8. bool init();
  9. CREATE_FUNC(scoreScene);
  10. public:
  11. void tableCellTouched(TableView *table,TableViewCell * cell){};
  12. TableViewCell * tableCellAtIndex(TableView * table,ssize_t index);
  13. Size tableCellSizeForIndex(TableView * table,ssize_t index);
  14. ssize_t numberOfCellsInTableView(TableView * table);
  15. void scrollViewDidScroll(ScrollView *){};
  16. void scrollViewDidZoom(ScrollView *){};
  17.  
  18. //对返回键的响应
  19. void onKeyReleased(EventKeyboard::KeyCode keyCode,Event * pEvent);
  20. private:
  21. Size size;


.cpp文件

  1. scoreScene::scoreScene()
  2. {
  3. }
  4.  
  5. scoreScene::~scoreScene()
  6. {
  7. _eventDispatcher->removeEventListenersForTarget(this);
  8. }
  9.  
  10. Scene * scoreScene::createScene()
  11. {
  12. auto scene = Scene::create();
  13. auto layer = scoreScene::create();
  14. scene->addChild(layer);
  15. return scene;
  16. }
  17.  
  18. bool scoreScene::init()
  19. {
  20. if (!Layer::init())
  21. return false;
  22.  
  23. //背景音乐
  24. CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  25. CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/BackgroundMusic2.mp3",true);
  26.  
  27. size = Director::getInstance()->getWinSize();
  28.  
  29. //添加背景图片
  30. auto background = Sprite::createWithSpriteFrameName("backgroundTollgateTwo.png");
  31. background->setPosition(Point(size.width / 2,size.height / 2));
  32. background->setAnchorPoint(Vec2(0.5,0.5));
  33. this->addChild(background);
  34.  
  35. //创建tableView并设置一些参数
  36. auto tableView = TableView::create(this,Size(size.width,size.height*0.8));
  37. //设置滑动方向
  38. tableView->setDirection(ScrollView::Direction::VERTICAL);
  39. //设置TableViewDelegate
  40. tableView->setDelegate(this);
  41. //index的大小是从上到下依次增大
  42. tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
  43. //用当前的配置刷新cell
  44. tableView->reloadData();
  45. this->addChild(tableView);
  46.  
  47. //排名
  48. auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");
  49.  
  50. auto rankNum = Label::createWithTTF(
  51. ((__String*)(dictionary->objectForKey("rankNum")))->getCString(),"fonts/DFPShaoNvW5-GB.ttf",40);
  52. rankNum->setColor(Color3B(255,0));
  53. rankNum->setPosition(Point(size.width*0.4,size.height*0.9));
  54. this->addChild(rankNum);
  55.  
  56. //得分
  57. auto rankscore = Label::createWithTTF(
  58. ((__String*)(dictionary->objectForKey("rankscore")))->getCString(),40);
  59. rankscore->setPosition(Point(size.width*0.8,size.height*0.9));
  60. rankscore->setColor(Color3B(255,0));
  61. this->addChild(rankscore);
  62.  
  63. //对返回键的响应
  64. auto listener = EventListenerKeyboard::create();
  65. listener->onKeyReleased = CC_CALLBACK_2(scoreScene::onKeyReleased,this);
  66. _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
  67.  
  68. return true;
  69. }
  70.  
  71. //对返回键的响应
  72. void scoreScene::onKeyReleased(EventKeyboard::KeyCode keyCode,Event * pEvent)
  73. {
  74. if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE)
  75. {
  76. //背景音乐
  77. CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
  78. CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_start.mp3",true);
  79.  
  80. //场景弹出
  81. Director::getInstance()->popScene();
  82. }
  83. }
  84.  
  85. //用来设置每个cell的内容
  86. TableViewCell * scoreScene::tableCellAtIndex(TableView * table,ssize_t index)
  87. {
  88. //设置每条记录前边的文本内容
  89. auto index_text = __String::createWithFormat("%ld",index + 1);
  90.  
  91. TableViewCell * cell = table->dequeueCell();
  92. if (cell == NULL)
  93. {
  94. //创建一个cell
  95. cell = new TableViewCell();
  96. cell->autorelease();
  97.  
  98. //创建显示排名的文本信息
  99. auto text = Label::createWithTTF(index_text->getCString(),24);
  100. text->setTag(1024);
  101. text->setColor(Color3B(255,0));
  102. //文本信息在cell的中间
  103. text->setPosition(Point(size.width*0.4,size.height*0.025));
  104. cell->addChild(text);
  105.  
  106. //显示用户得分的文本信息
  107. auto index_score = __String::createWithFormat("%d",index);
  108. //根据index值获得得分的文本,因为这个时候的score是int型,所以还需要转化一下类型
  109. int i_score = UserDefault::getInstance()->getIntegerForKey(index_score->getCString());
  110. auto * str = __String::createWithFormat("%d",i_score);
  111. auto score = Label::createWithTTF(
  112. str->getCString(),24);
  113. score->setTag(2048);
  114. //设置坐标
  115. score->setPosition(Point(size.width*0.8,size.height*0.025));
  116. score->setColor(Color3B(255,0));
  117. cell->addChild(score);
  118.  
  119. }
  120. //这里获得的cell是原来的cell,所以原来cell的文本信息等还是原来的,所以要做一些改变
  121. else
  122. {
  123. //通过tag值获得文本,并且改变
  124. auto text = (Label *)cell->getChildByTag(1024);
  125. text->setString(index_text->getCString());
  126.  
  127. //改变分数
  128. auto * score = (Label *)cell->getChildByTag(2048);
  129. auto * index_score = __String::createWithFormat("%d",index);
  130. int i_score = UserDefault::getInstance()->getIntegerForKey(index_score->getCString());
  131. auto * str = __String::createWithFormat("%d",i_score);
  132. score->setString(str->getCString());
  133.  
  134. if (cell->getChildByTag(100) != NULL)
  135. {
  136. Sprite * sprite = (Sprite *)cell->getChildByTag(100);
  137. sprite->removeFromParentAndCleanup(true);
  138. }
  139. }
  140.  
  141. if (index == 0 || index == 1 || index == 2)
  142. {
  143. Sprite * sprite;
  144. switch (index)
  145. {
  146. case 0:
  147. sprite = Sprite::createWithSpriteFrameName("gold.png");
  148. break;
  149. case 1:
  150. sprite = Sprite::createWithSpriteFrameName("silvere.png");
  151. break;
  152. case 2:
  153. sprite = Sprite::createWithSpriteFrameName("copper.png");
  154. break;
  155. }
  156. sprite->setPosition(Point(size.width*0.15,size.height*0.025));
  157. sprite->setTag(100);
  158. cell->addChild(sprite);
  159. }
  160.  
  161. return cell;
  162. }
  163.  
  164. //这个函数是用来设置每个cell的大小的
  165. Size scoreScene::tableCellSizeForIndex(TableView * table,ssize_t index)
  166. {
  167. return Size(size.width,size.height*0.05);
  168. }
  169.  
  170. //这个函数是用来设置cell的个数的
  171. ssize_t scoreScene::numberOfCellsInTableView(TableView * table)
  172. {
  173. //个数是从XML文件中读取到的,有多少条记录,就设置多少个cell,如果刚开始没有count这个字段,就返回0
  174. int count = UserDefault::getInstance()->getIntegerForKey("count",0);
  175.  
  176. return count;
  177. }


好了,这个游戏到这里就已经完成啦!再有的就是移植到其它平台了。我是将这个游戏移植到了Android平台。



代码以及资源文件

资源文件

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