/当游戏中需要进行滚动计分时 为了获得滚动数字 我们可以首先写一个计分函数 放入update中进行调用每帧加一个固定的分数
//加入分数记录框
auto framCache=SpriteFrameCache::getInstance();
framCache->addSpriteFramesWithFile("config.plist","config.png");
auto fram=framCache->getSpriteFrameByName("scorebar.png");
auto title=Sprite::createWithSpriteFrame(fram);
title->setPosition(Point(size.width-210,size.height-15));
addChild(title);
//初始五个0
for(int i=0;i<5;i++)
{
auto number=Sprite::create("111.png");
ui=number->getTexture();
number->setTextureRect(CCRectMake(24.8,9,35)); //一定要注意对精灵的裁剪
number->setPosition(Point(size.width-50-i*15,size.height-10));
bits[i]=number;
addChild(number,1);
}
for(int j=0;j<5;j++)
{
auto number1=Sprite::create("111.png");
ui1=number1->getTexture();
number1->setTextureRect(CCRectMake(24.8,35));
number1->setPosition(Point(size.width-275-j*15,size.height-10));
bits1[j]=number1;
addChild(number1,1);
}
mark=0; //将分数初始为0
this->scheduleUpdate(); //调用update函数
return true;
}
//计分函数(运用整除和取余的方法不停的设置个十百千万位 运用setTextureRect(CCRectMake(24.8+temp*12.3,35))对精灵进行裁剪和贴图
void GameMark::addNumber(int var)
{
mark+=var;
//个位
int temp=mark%10;
if(temp>0)
{
bits[0]->setTexture(ui);
bits[0]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
bits1[0]->setTexture(ui);
bits1[0]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
}
else
{
bits[0]->setTexture(ui);
bits[0]->setTextureRect(CCRectMake(21,13,35));
bits1[0]->setTexture(ui);
bits1[0]->setTextureRect(CCRectMake(21,35));
}
//十位
temp=(mark%100)/10;
if(temp>0)
{
bits[1]->setTexture(ui);
bits[1]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
bits1[1]->setTexture(ui);
bits1[1]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
}
else
{
bits[1]->setTexture(ui);
bits[1]->setTextureRect(CCRectMake(21,35));
bits1[1]->setTexture(ui);
bits1[1]->setTextureRect(CCRectMake(21,35));
}
//百位
temp=(mark%1000)/100;
if(temp>0)
{
bits[2]->setTexture(ui);
bits[2]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
bits1[2]->setTexture(ui);
bits1[2]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
}
else
{
bits[2]->setTexture(ui);
bits[2]->setTextureRect(CCRectMake(21,35));
bits1[2]->setTexture(ui);
bits1[2]->setTextureRect(CCRectMake(21,35));
}
//千位
temp=(mark%10000)/1000;
if(temp>0)
{
bits[3]->setTexture(ui);
bits[3]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
bits1[3]->setTexture(ui);
bits1[3]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
}
else
{
bits[3]->setTexture(ui);
bits[3]->setTextureRect(CCRectMake(21,35));
bits1[3]->setTexture(ui);
bits1[3]->setTextureRect(CCRectMake(21,35));
}
//万位
temp=mark/10000;
if(temp>0)
{
bits[4]->setTexture(ui);
bits[4]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
bits1[4]->setTexture(ui);
bits1[4]->setTextureRect(CCRectMake(24.8+temp*12.3,35));
}
else
{
bits[4]->setTexture(ui);
bits[4]->setTextureRect(CCRectMake(21,35));
bits1[4]->setTexture(ui);
bits1[4]->setTextureRect(CCRectMake(21,35));
}
}