喵星战争(七):游戏分数的实现


在很多手机游戏中在右上角或者某个地方一般都有记录游戏分数的显示,今天我们要实现的也是游戏过程中分数的动态显示效果。如下图所示:

shu.png图片如下,其尺寸是(260 x 31):

实现的代码如下:

GameMark.h

#ifndef _GAME_MARK_H__
#define _GAME_MARK_H__

#include "cocos2d.h"
using namespace cocos2d;

class GameMark : public CCNode
{
public:
	GameMark(void);
	virtual ~GameMark(void);
	virtual void onEnter();
	virtual void onExit();
	CCArray *bits;
	int mark;
	void addnumber(int var);
	CCTexture2D* ui;
};

#endif

GameMark.cpp

#include "GameMark.h"
GameMark::GameMark(void)
{
}

GameMark::~GameMark(void)
{
}

void GameMark::onExit()
{
	CCNode::onExit();
}

void GameMark::onEnter()
{
	CCNode::onEnter();
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	this->setContentSize(size);
	bits = CCArray::createWithCapacity(5); //原文是CCArray::create(5),其参数不正确,按其意思应该是创建一个大小为5的数组

	//分数标题
	CCSprite *title = CCSprite::create("score.png");
	title->setPosition(ccp(size.width/2 + 40 , size.height - 15));
	title->setScale(0.5);
	addChild(title);

	//数字按位设置
	for(int i = 0; i<5; i++)
	{
		CCSprite *shu = CCSprite::create("shu.png");
		ui = shu->getTexture();
		shu->setScale(0.5);
		shu->setTextureRect(CCRectMake(234 ,0, 26, 31));
		shu->setPosition(ccp(size.width - 15 - i*15 , size.height - 15));
		bits->addObject(shu);
		addChild(shu);
	}
	bits->retain();
	mark = 0;
}

void GameMark::addnumber(int var)
{
	//分数,按位设置数字
	mark += var;
	int temp = mark%10;
	
	//设置个位
	if (temp>0)
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31));
	}
	else
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(0))->setTextureRect(CCRectMake(234, 0, 26, 31));
	}

	//设置十位
	temp = (mark%100)/10;
	if (temp>0)
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31));
	}
	else
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(1))->setTextureRect(CCRectMake(234, 0, 26, 31));
	}

	//设置百位
	temp = (mark%1000)/100;
	if (temp>0)
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(2))->setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31));
	}
	else
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(2))->setTextureRect(CCRectMake(234, 0, 26, 31));
	}

	//设置千位
	temp = (mark%10000)/1000;
	if (temp>0)
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31));
	}
	else
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(3))->setTextureRect(CCRectMake(234, 0, 26, 31));
	}

	//设置万位
	temp = mark/10000;
	if (temp>0)
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31));
	}
	else
	{
		((CCSprite *)bits->objectAtIndex(0))->setTexture(ui);
		((CCSprite *)bits->objectAtIndex(4))->setTextureRect(CCRectMake(234, 0, 26, 31));
	}
}


要理解上面实现的原来,我们必须先了解下面的知识点:

1.CCArray:
CCArray是cocos2d鼎力支持的数据结构类,它对游戏存储数组型数据做了优化。CCArray是一个面向对象包装类,继承至CCObject,并提供了一系列的接口。

 

创建

/** 创建一个数组 */
 static CCArray* create();
/** 使用一些对象创建数组 */
static CCArray* create(CCObject* pObject, …);     
/** 使用一个对象创建数组 */   
   static CCArray* createWithObject(CCObject* pObject);     
/** 创建一个指定大小的数组 */   
 static CCArray* createWithCapacity(unsigned int capacity);     
/** 使用一个现有的CCArray数组来新建一个数组 */ 
static CCArray* createWithArray(CCArray* otherArray);

插入

/** 插入一个对象 */
   void addObject(CCObject* object);
/** 插入别外一个数组里面的全部对象 */
   void addObjectsFromArray(CCArray* otherArray);
/** 在一个确定的索引位置插入一个对象 */
    void insertObject(CCObject* object, unsigned int index);

删除

 
/** 移除最后的一个对象 */
    void removeLastObject(bool bReleaseObj = true);
/**移除一个确定的对象 */
     void removeObject(CCObject* object, bool bReleaseObj = true);
/** 移除一个确定索引位置的元素 */
     void removeObjectAtIndex(unsigned int index, bool bReleaseObj = true);
/** 移除全部元素 */
    void removeObjectsInArray(CCArray* otherArray);

/** 移除所有对象 */
    void removeAllObjects();
/** 快速移除一个对象 */
    void fastRemoveObject(CCObject* object);
/** 快速移除一个确定索引位置的对象 */
   void fastRemoveObjectAtIndex(unsigned int index);
/** 返回数组内位于index 的对象 **/
    CCObject* objectAtIndex  (unsigned int  index);

2.setScale():调整图片的缩放比例

3.setTextureRect():设置可视区域的大小

所谓可视区域就是在一个完整的表面上对其设置一定大小的区域,让其只显示设置的区域的面,其他部分隐藏不显示出来;最常用的是将一张图片设置一个可视区域,只让这张图展示设置的区域;

详细介绍:http://blog.csdn.net/xiaominghimi/article/details/6911387

4.retain():其作用是保持引用,也就是说,如果想保持某个对象的引用,避免它被Cocos2d-x释放,那就要调用对象的retain函数;

菜鸟,详细描述不来,上资料:http://blog.csdn.net/musicvs/article/details/8689345(写的挺好的)

 

上面整个程序的作用原理就是:玩家记录得分后,通过求余和取整操作分别得到个、十、百、千、万上面的数字,然后通过,setTextureRect来分别显示相应的数字,由于一共有10个数字,而整个图片的尺寸是260x31的也就是每个数字图片的大小就是26x31,这就是setTextureRect(CCRectMake((temp -1)*26 , 0, 26,31))取这些参数的由来。另外在数组中放了截取到5个宽和高为26和31的精灵对象,并设置了他们放置的相对位置,当分数变化的时候,这五个位置上分别会显示截取到的数字,在连续变化的时候就有动态变化的效果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值