喵星战争(七):游戏分数的实现
2013-08-17 00:06
253 查看
在很多手机游戏中在右上角或者某个地方一般都有记录游戏分数的显示,今天我们要实现的也是游戏过程中分数的动态显示效果。如下图所示:
shu.png图片如下,其尺寸是(260 x 31):
实现的代码如下:
GameMark.h
GameMark.cpp
要理解上面实现的原来,我们必须先了解下面的知识点:
1.CCArray:
CCArray是cocos2d鼎力支持的数据结构类,它对游戏存储数组型数据做了优化。CCArray是一个面向对象包装类,继承至CCObject,并提供了一系列的接口。
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的精灵对象,并设置了他们放置的相对位置,当分数变化的时候,这五个位置上分别会显示截取到的数字,在连续变化的时候就有动态变化的效果了。
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的精灵对象,并设置了他们放置的相对位置,当分数变化的时候,这五个位置上分别会显示截取到的数字,在连续变化的时候就有动态变化的效果了。
相关文章推荐
- 喵星战争(八):游戏主模块的实现-2
- 喵星战争(九):游戏主菜单的实现
- cocos2dx - 制作纵版射击游戏:喵星战争 (四)
- cocos2dx - 制作纵版射击游戏:喵星战争 (七)
- 喵星战争(十):关于界面的实现
- cocos2dx - 制作纵版射击游戏:喵星战争 (五)
- 喵星战争(一):游戏简介
- cocos2d-x学习日志(10) --射击游戏(喵星战争)
- cocos2d-x学习日志(10) --射击游戏(喵星战争)
- cocos2dx - 制作纵版射击游戏:喵星战争 (三)
- cocos2dx - 制作纵版射击游戏:喵星战争 (二)
- cocos2dx - 制作纵版射击游戏:喵星战争 (六)
- 游戏 匹配算法 实现(基于ELO分数、等待时长)
- 游戏地图拼接与战争迷雾的实现
- 喵星战争(六):子弹的实现
- cocos2dx - 制作纵版射击游戏:喵星战争 (一)
- iOS关灯游戏AI的设计和实现
- Cocos2d-x简单游戏<植物大战僵尸>代码实现|第八部分:子弹类<后续会提供源码下载链接>
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之开发游戏界面(二)
- 利用计划任务实现游戏监控