在右下角显示游戏进行的回合数:
实现方式:
1、 在GameBaseScene类中创建帧缓存存放十个数字的SpriteFrame,代表0-9的阿拉伯数字,放入Vector中
2、 在GameBaseScene类中定义变量gameRoundCount,初始值为0
3、 在GameBaseScene类中定义refreshRoundDisplay()方法,用来刷新回合显示
实现方式,采用数字取模,除以0不为零,直到取完,从digiteVector取得sprite对象,倒序放入refreshRoundVector中,取模完毕后,刷新显示
4、 当所有角色走完一遍后,gameRoundCount++,然后调用refreshRoundDisplay()刷新显示
下面看代码实现
//1、根据数字plist文件 在帧缓存中存放数字spriteFrame,同时存入digiteRoundVector容器中
void GameBaseScene::addDigiteRoundSprite()
{
//2、定义变量gameRoundCount,初始值为0,记录游戏进行的回合数
gameRoundCount=0;
auto frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("map/digital_round.plist");
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_0));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_1));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_2));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_3));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_4));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_5));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_6));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_7));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_8));
digiteRoundVector.pushBack(frameCache->getSpriteFrameByName(DIGITAL_9));
}
3、refreshRoundDisplay()方法,用来刷新回合显示
void GameBaseScene::refreshRoundDisplay()
{
// refreshRoundVector容器存放之前回合数相关的Sprite ,所以在刷新之前要把前面的清除
for(auto it = refreshRoundVector.begin();it != refreshRoundVector.end();it++)
{
((Sprite*) *it)->setVisible(false);
}
refreshRoundVector.clear();
int count = gameRoundCount;
Sprite* st;
//当游戏刚开始,显示回合数为0
if(count ==0 )
{
st = Sprite::createWithSpriteFrame(digiteRoundVector.at(0));
addChild(st);
refreshRoundVector.pushBack(st);
}
//把数字转换成Sprite 存放进refreshRoundVector容器
while(count!=0)
{
st = Sprite::createWithSpriteFrame(digiteRoundVector.at(count%10));
addChild(st);
refreshRoundVector.pushBack(st);
count = count/10;
}
//存放时由于取模计算,都是倒序存放的,所以正确显示时要把顺序倒过来
refreshRoundVector.reverse();
for(int i = 0;i< refreshRoundVector.size();i++)
{
Sprite * sp = refreshRoundVector.at(i);
sp->setPosition(ccp((tableStartPosition_x+50)+(i*25),50));
sp->setVisible(true);
}
}
4、 当所有角色走完一遍后,gameRoundCount++,然后调用refreshRoundDisplay()刷新显示
我们在接收到要求显示go按钮的时候处理这段逻辑
void GameBaseScene::receivedMsgForGo(Object* data)
{
if(retMsgType ==1)
{
……………
diceSprite->resume();
gameRoundCount++;
refreshRoundDisplay();
}
……………………..
}
点击下载代码 http://download.csdn.net/detail/lideguo1979/8307969
未完待续.....................