用CCLabelTTF显示的数字不好看。于是就想到用图片来代理。眼下网上的实现都是把每一个数字做一个CCSprite组合的方式。
可是我想。动态生成纹理的方式。没有就仅仅好自己手动写一个。
头文件
#ifndef _X_NUMBER_H_ #define _X_NUMBER_H_ #include <cocos2d.h> #include <xtype.h> namespace cocos2d { //基于图片显示的数字 /* 这个类不是用一个一个数字拼起来,而是渲染成一个独立的纹理 zdh::XDDWord是一个64位无符号整数 */ class CCPictureNumber : public CCSprite { public: typedef CCSprite Inherited; public: CCPictureNumber(); ~CCPictureNumber(); virtual bool init(void); int BuildNumber(zdh::XDDWord paramNumber, const char * paramNumberResName); int BuildNumber(zdh::XDDWord paramNumber, CCTexture2D * paramTexture); int BuildNumber(zdh::XDDWord paramNumber); CREATE_FUNC(CCPictureNumber); void setNumberTexture(CCTexture2D * paramTexture); void setNumberTexture(const char * paramNumberResName); CCTexture2D * getNumberTexture(); zdh::XDDWord getNumber() const; void setNumber(zdh::XDDWord paramNumber); int Build(); private: CCTexture2D * m_NumberTexture; zdh::XDDWord m_Number; }; } #endif
源文件
#include "xpicture_number.h" #include <xstring.h> namespace cocos2d { //-------------------------------------------------------------------------------------- //从指定资源名称构建 int CCPictureNumber::BuildNumber(zdh::XDDWord paramNumber, const char * paramNumberResName) { this->setNumber(paramNumber); this->setNumberTexture(CCTextureCache::sharedTextureCache()->addImage(paramNumberResName)); return this->Build(); } //-------------------------------------------------------------------------------------- //从指定纹理构建 int CCPictureNumber::BuildNumber(zdh::XDDWord paramNumber, CCTexture2D * paramTexture) { this->setNumber(paramNumber); this->setNumberTexture(paramTexture); return this->Build(); } //-------------------------------------------------------------------------------------- int CCPictureNumber::BuildNumber(zdh::XDDWord paramNumber) { this->setNumber(paramNumber); return this->Build(); } //-------------------------------------------------------------------------------------- bool CCPictureNumber::init(void) { if (!Inherited::init()) return false; return true; } //-------------------------------------------------------------------------------------- CCPictureNumber::CCPictureNumber() { m_NumberTexture = nullptr; m_Number = 0; } //-------------------------------------------------------------------------------------- CCPictureNumber::~CCPictureNumber() { if (zdh::isNotNULL(m_NumberTexture)) { m_NumberTexture->release(); } } //-------------------------------------------------------------------------------------- void CCPictureNumber::setNumberTexture(CCTexture2D * paramTexture) { if (m_NumberTexture == paramTexture) return; if (zdh::isNotNULL(m_NumberTexture)) { m_NumberTexture->release(); } m_NumberTexture = paramTexture; if (zdh::isNotNULL(m_NumberTexture)) { m_NumberTexture->retain(); } } //-------------------------------------------------------------------------------------- void CCPictureNumber::setNumberTexture(const char * paramNumberResName) { this->setNumberTexture(CCTextureCache::sharedTextureCache()->addImage(paramNumberResName)); } //-------------------------------------------------------------------------------------- CCTexture2D * CCPictureNumber::getNumberTexture() { return m_NumberTexture; } //-------------------------------------------------------------------------------------- int CCPictureNumber::Build() { if (zdh::isNULL(m_NumberTexture)) return zdh::ERR_FAIL; zdh::XAnsiString strNumber(m_Number); //将整数转换为字符串 int iNumCount = strNumber.getLength(); //取得字符个数 CCSize stSize = m_NumberTexture->getContentSize(); //取得纹理大小,要求纹理中每一个数字都是等宽等高,并按照0123456789排列 int iNumWidth = (int)stSize.width / 10; //纹理中每一个数字的宽度 int iNumHeight = (int)stSize.height; //纹理中每一个数字的高度 CCRenderTexture * pRT = CCRenderTexture::create(iNumWidth * iNumCount, iNumHeight); //创建渲染纹理对象,并数字确定宽度 CCSprite * pSprite = CCSprite::create(); //创建精灵对象,用于绘制数字 pSprite->setAnchorPoint(0, 0); pSprite->setTexture(m_NumberTexture); CCRect stRect; pRT->begin(); for (int i = 0; i < iNumCount; i++) { int iNumber = strNumber[i] - '0'; //设置要显示数字的纹理区域,这个区域是指參数中paramTexture中区域 stRect.setRect(iNumber * iNumWidth, 0, iNumWidth, iNumHeight); pSprite->setTextureRect(stRect, false, stRect.size); pSprite->setPosition(i * iNumWidth, 0); //计算显示的偏移位置 pSprite->visit(); //渲染到pRT中 } pRT->end(); //取得生成的纹理 this->setTexture(pRT->getSprite()->getTexture()); //设置显示的内容 stRect.setRect(0, 0, iNumWidth * iNumCount, iNumHeight); this->setTextureRect(stRect, false, stRect.size); //默认的情况下,通过CCRenderTexture得到的纹理是倒立的。这里须要做一下翻转 this->setFlipY(true); //释放资源 delete pSprite; delete pRT; return zdh::ERR_OK; } //-------------------------------------------------------------------------------------- zdh::XDDWord CCPictureNumber::getNumber() const { return m_Number; } //-------------------------------------------------------------------------------------- void CCPictureNumber::setNumber(zdh::XDDWord paramNumber) { m_Number = paramNumber; } }
数字图片
使用样例
CCPictureNumber * pNum = CCPictureNumber::create(); pNum->BuildNumber(1234567, "ui_play_num05.png"); pNum->setPosition(200, 200); pNum->setAnchorPoint(0, 0); this->addChild(pNum, 100);