Cocos2d-x 3.2 大富翁游戏项目开发-第十二部分 显示回合计数器

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第十二部分 显示回合计数器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在右下角显示游戏进行的回合数:

实现方式:

1、 在GameBaseScene类中创建帧缓存存放十个数字的SpriteFrame,代表0-9的阿拉伯数字,放入Vector中

2、 在GameBaseScene类中定义变量gameRoundCount,初始值为0

3、 在GameBaseScene类中定义refreshRoundDisplay()方法,用来刷新回合显示

实现方式,采用数字取模,除以0不为零,直到取完,从digiteVector取得sprite对象,倒序放入refreshRoundVector中,取模完毕后,刷新显示

4、 当所有角色走完一遍后,gameRoundCount++,然后调用refreshRoundDisplay()刷新显示

下面看代码实现


  1. //1、根据数字plist文件 在帧缓存中存放数字spriteFrame,同时存入digiteRoundVector容器中
  2. void GameBaseScene::addDigiteRoundSprite()
  3. {
  4. //2、定义变量gameRoundCount,初始值为0,记录游戏进行的回合数
  5. gameRoundCount=0;
  6. auto frameCache = SpriteFrameCache::getInstance();
  7. frameCache->addSpriteFramesWithFile("map/digital_round.plist");
  8.  
  9. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_0));
  10. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_1));
  11. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_2));
  12. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_3));
  13. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_4));
  14. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_5));
  15. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_6));
  16. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_7));
  17. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_8));
  18. digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_9));
  19.  
  20. }

  1. 3refreshRoundDisplay()方法,用来刷新回合显示
  2. void GameBaseScene::refreshRoundDisplay()
  3. {
  4. // refreshRoundVector容器存放之前回合数相关的Sprite ,所以在刷新之前要把前面的清除
  5. for(auto it = refreshRoundVector.begin();it != refreshRoundVector.end();it++)
  6. {
  7. ((Sprite*) *it)->setVisible(false);
  8. }
  9.  
  10. refreshRoundVector.clear();
  11. int count = gameRoundCount;
  12. Sprite* st;
  13. //当游戏刚开始,显示回合数为0
  14. if(count ==0 )
  15. {
  16. st = Sprite::createWithSpriteFrame(digiteRoundVector.at(0));
  17. addChild(st);
  18. refreshRoundVector.pushBack(st);
  19. }
  20. //把数字转换成Sprite 存放进refreshRoundVector容器
  21. while(count!=0)
  22. {
  23. st = Sprite::createWithSpriteFrame(digiteRoundVector.at(count%10));
  24. addChild(st);
  25. refreshRoundVector.pushBack(st);
  26. count = count/10;
  27.  
  28. }
  29. //存放时由于取模计算,都是倒序存放的,所以正确显示时要把顺序倒过来
  30. refreshRoundVector.reverse();
  31.  
  32. for(int i = 0;i< refreshRoundVector.size();i++)
  33. {
  34. Sprite * sp = refreshRoundVector.at(i);
  35. sp->setPosition(ccp((tableStartPosition_x+50)+(i*25),50));
  36. sp->setVisible(true);
  37. }
  38.  
  39. }

  1. 4 当所有角色走完一遍后,gameRoundCount++,然后调用refreshRoundDisplay()刷新显示
  2. 我们在接收到要求显示go按钮的时候处理这段逻辑
  3.  
  4. void GameBaseScene::receivedMsgForGo(Object* data)
  5. {
  6. if(retMsgType ==1)
  7. {
  8. ……………
  9. diceSprite->resume();
  10. gameRoundCount++;
  11. refreshRoundDisplay();
  12. }
  13. ……………………..
  14. }



点击下载代码http://download.csdn.net/detail/lideguo1979/8307969


未完待续.....................

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