1 CCLabel
A 标签CCLabelTTF
CCLabelTTF * ttf = CCLabelTTF::create("LabelTTF", "Courier", 100);
第一个参数为,要显示的字符串,第二个能数位字体,第三个参数为大小。
优点:简单易操作,无需任何额外的资源
缺点:由于它的运行原理,是先将字符转化为图片纹理,然后渲染至屏幕。所以不适合于变动的文字。易于静态的显示。
B CCLabelAtlas
CCLabelAtlas * atlas = CCLabelAtlas::create(“123.45”,”font/fps_images.png”,16,32,’.’);
第一个参数为要显示的字符串,第二个参数为图片,第三个参数每一个字符的长度,第四个为每一个字符的高度,第五个位第一个字符的ASSIC。
优点:将等宽,等高的字符,放到一张大图中去,然后通过要显示的字符的ASSIC去找相应的图片渲染到屏幕中去,这样一次加载,多次取用,相比TTF效率要高。
缺点:素材需要依赖于美工,显示内容局限性大。
C CCLabelBMFont
CCLabelBMFont * bm = CCLabelBMFont::create(“ABCD”,”fonts/bitmapFontTest.fnt”);
CC_DLL CC_DLL CCLabelBMFont 继承自CCSpriteBatchNode,所以本身采用了CCSpriteBatchNode的优化功能。第一个参数为要显示的字符串,第二个参数为要加载图片的资源文件。CCLabelBMFont中的每一个字符都是一个已加载到CCSpriteBatchNode中的CCSprite。可以通过接口取出。这种实现方式既实现了优化的效果,也更灵活。
优点:显示字体多样,内部完成优化效率高。
缺点:需要依赖美工制作fnt文件。
CCLabelBMFont * bm = CCLabelBMFont::create("Good Year","fonts/bitmapFontTest.fnt");
bm->setPosition(ccp(winSize.width/2,winSize.height/2));
addChild(bm);
bm->setTag(BM);
CCArray * array = bm->getChildren();
CCSprite * G = (CCSprite*)array->objectAtIndex(0);
CCSprite * G = (CCSprite *)bm->getChildByTag(0);
T10Label.h
#ifndef __T10Label_H__
#define __T10Label_H__
#include "cocos2d.h"
USING_NS_CC;
class T10Label :public CCLayer
{
public:
static CCScene * scene();
CREATE_FUNC(T10Label);
bool init();
enum LABEL
{
TTF, ATLAS, BM
};
void mySchedule(float dt);
};
#endif
T10Label.cpp
#include "T10Label.h"
#include "AppMacros.h"
CCScene *T10Label::scene()
{
CCScene * scene = CCScene::create();
T10Label * layer = T10Label::create();
scene->addChild(layer);
return scene;
}
bool T10Label::init()
{
CCLayer::init();
CCLabelBMFont * bm = CCLabelBMFont::create("GOOD YEAR", "fonts/bitmapFontTest.fnt");
//下面的方式是将bm存入层中
addChild(bm);
//从bm中取出各个元素
CCArray * array = bm->getChildren();
CCObject * obj;
//将元素随机放在不同的位置
CCARRAY_FOREACH(array, obj)
{
CCSprite * spr = (CCSprite *)obj;
spr->setPosition(ccp(CCRANDOM_0_1() * 480, CCRANDOM_0_1() * 320));
}
//循环将精灵放在不同的位置
CCARRAY_FOREACH(array,obj)
{
static float x = 100;
static float y = 100;
//将(CCSprite *)强转
CCSprite * spr = (CCSprite *)obj;
CCMoveTo * to = CCMoveTo::create(2, ccp(x += 30, y));
spr->runAction(to);
}
return true;
}
void T10Label::mySchedule(float dt)
{
static float count = 0;
count += dt;
CCString * str = CCString::createWithFormat("%d", (int)count);
//CCLabelTTF * ttf = (CCLabelTTF *)getChildByTag(TTF);
//ttf->setString(str->getCString());
CCLabelAtlas * atlas = (CCLabelAtlas*)getChildByTag(ATLAS);
atlas->setString(str->getCString());
}
运行结果:
当代码是如下是:
#include "T10Label.h"
#include "AppMacros.h"
CCScene *T10Label::scene()
{
CCScene * scene = CCScene::create();
T10Label * layer = T10Label::create();
scene->addChild(layer);
return scene;
}
bool T10Label::init()
{
CCLayer::init();
CCLabelTTF * ttf = CCLabelTTF::create("Score", "Courier", 20);
ttf->setPosition(ccp(winSize.width / 2, winSize.height / 2));
addChild(ttf);
//设置字体的颜色
ttf->setFontSize(50);
//设置字体的名称
ttf->setFontName("Courier New");
//设置字符串
ttf->setString("xxxx");
ttf->setFontFillColor(ccc3(255,0,0),true);
ttf->setTag(TTF);
return true;
}
void T10Label::mySchedule(float dt)
{
static float count = 0;
count += dt;
CCString * str = CCString::createWithFormat("%d", (int)count);
//CCLabelTTF * ttf = (CCLabelTTF *)getChildByTag(TTF);
//ttf->setString(str->getCString());
CCLabelAtlas * atlas = (CCLabelAtlas*)getChildByTag(ATLAS);
atlas->setString(str->getCString());
}
运行结果:
当代码改成如下的时:
#include "T10Label.h"
#include "AppMacros.h"
CCScene *T10Label::scene()
{
CCScene * scene = CCScene::create();
T10Label * layer = T10Label::create();
scene->addChild(layer);
return scene;
}
bool T10Label::init()
{
CCLayer::init();
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLabelAtlas *atlas = CCLabelAtlas::create("012345678923", "fonts/Labelatlas.png", 31, 60,'0');
atlas->setPosition(ccp(100,100));
atlas->setColor(ccc3(255, 0, 0));
this->addChild(atlas, 1);
return true;
}
void T10Label::mySchedule(float dt)
{
static float count = 0;
count += dt;
CCString * str = CCString::createWithFormat("%d", (int)count);
//CCLabelTTF * ttf = (CCLabelTTF *)getChildByTag(TTF);
//ttf->setString(str->getCString());
CCLabelAtlas * atlas = (CCLabelAtlas*)getChildByTag(ATLAS);
atlas->setString(str->getCString());
}
运行结果: