喵星战争(七):游戏分数的实现
来源:互联网 发布:给水管工程量算法 编辑:程序博客网 时间:2024/04/27 18:20
在很多手机游戏中在右上角或者某个地方一般都有记录游戏分数的显示,今天我们要实现的也是游戏过程中分数的动态显示效果。如下图所示:
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的精灵对象,并设置了他们放置的相对位置,当分数变化的时候,这五个位置上分别会显示截取到的数字,在连续变化的时候就有动态变化的效果了。
- 喵星战争(七):游戏分数的实现
- 喵星战争(九):游戏主菜单的实现
- 喵星战争(八):游戏主模块的实现-1
- 喵星战争(八):游戏主模块的实现-2
- 喵星战争(一):游戏简介
- 喵星战争(五):敌人狗博士的实现
- 喵星战争(六):子弹的实现
- 喵星战争(十):关于界面的实现
- 游戏地图拼接与战争迷雾的实现
- 2D游戏的战争迷雾实现方式
- 战争迷雾的实现
- (NO.00002)iOS游戏精灵战争雏形(七)
- cocos2d-x学习日志(10) --射击游戏(喵星战争)
- 喵星战争(三):主角小猫的初始化-onEnter()
- libgdx游戏引擎(七)之多游戏界面的实现
- cocos2dx 游戏当中的战争迷雾
- [体感游戏]关于体感游戏的一些思考(四) --- 克隆战争!
- 暴雪和黑客的战争七:Warden和外挂的进化(二)
- Share a link about Resposive Web Design
- NYOJ-死神来了
- hdu1162 Eddy's picture (最小生成树之prim 算法)
- 路由/协议系列--持续更新
- POJ1678 I Love this Game!
- 喵星战争(七):游戏分数的实现
- 《虚拟智慧:VMware vSphere运维实录》(附光盘)下载
- Web负载均衡 -- 《构建高性能Web站点》读书笔记
- 英文对程序员的重要性
- Ubuntu安装心得
- wince 下WIFI 休眠和启用
- InletexEMC 多人屏幕共享工具
- Linux下实现C++类的动态链接
- 什么是MySQL触发器