日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

2.标签CCLabelTTF,CCLabelAtlas,CCLabelBMFont

發(fā)布時(shí)間:2024/9/27 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2.标签CCLabelTTF,CCLabelAtlas,CCLabelBMFont 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1 CCLabel

A 標(biāo)簽CCLabelTTF

CCLabelTTF * ttf = CCLabelTTF::create("LabelTTF", "Courier", 100);

第一個(gè)參數(shù)為,要顯示的字符串,第二個(gè)能數(shù)位字體,第三個(gè)參數(shù)為大小。

優(yōu)點(diǎn):簡(jiǎn)單易操作,無(wú)需任何額外的資源

缺點(diǎn):由于它的運(yùn)行原理,是先將字符轉(zhuǎn)化為圖片紋理,然后渲染至屏幕。所以不適合于變動(dòng)的文字。易于靜態(tài)的顯示。

B CCLabelAtlas

CCLabelAtlas * atlas = CCLabelAtlas::create(“123.45”,”font/fps_images.png”,16,32,’.’);

第一個(gè)參數(shù)為要顯示的字符串,第二個(gè)參數(shù)為圖片,第三個(gè)參數(shù)每一個(gè)字符的長(zhǎng)度,第四個(gè)為每一個(gè)字符的高度,第五個(gè)位第一個(gè)字符的ASSIC

優(yōu)點(diǎn):將等寬,等高的字符,放到一張大圖中去,然后通過(guò)要顯示的字符的ASSIC去找相應(yīng)的圖片渲染到屏幕中去,這樣一次加載,多次取用,相比TTF效率要高。

缺點(diǎn):素材需要依賴(lài)于美工,顯示內(nèi)容局限性大。

C CCLabelBMFont

CCLabelBMFont * bm = CCLabelBMFont::create(“ABCD”,”fonts/bitmapFontTest.fnt”);

CC_DLL? CC_DLL CCLabelBMFont 繼承自CCSpriteBatchNode,所以本身采用了CCSpriteBatchNode的優(yōu)化功能。第一個(gè)參數(shù)為要顯示的字符串,第二個(gè)參數(shù)為要加載圖片的資源文件。CCLabelBMFont中的每一個(gè)字符都是一個(gè)已加載到CCSpriteBatchNode中的CCSprite。可以通過(guò)接口取出。這種實(shí)現(xiàn)方式既實(shí)現(xiàn)了優(yōu)化的效果,也更靈活。

優(yōu)點(diǎn):顯示字體多樣,內(nèi)部完成優(yōu)化效率高。

缺點(diǎn):需要依賴(lài)美工制作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中取出各個(gè)元素

??? CCArray * array = bm->getChildren();

?

??? CCObject * obj;

??? //將元素隨機(jī)放在不同的位置

??? CCARRAY_FOREACH(array, obj)

??? {

??????? CCSprite * spr = (CCSprite *)obj;

??????? spr->setPosition(ccp(CCRANDOM_0_1() * 480, CCRANDOM_0_1() * 320));

??? }

?

??? //循環(huán)將精靈放在不同的位置

??? CCARRAY_FOREACH(array,obj)

??? {

??????? static float x = 100;

??????? static float y = 100;

??????? //將(CCSprite *)強(qiáng)轉(zhuǎn)

??????? 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());

}

運(yùn)行結(jié)果:

當(dāng)代碼是如下是:

#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);

??? //設(shè)置字體的顏色

??? ttf->setFontSize(50);

??? //設(shè)置字體的名稱(chēng)

??? ttf->setFontName("Courier New");

??? //設(shè)置字符串

??? 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());

}

運(yùn)行結(jié)果:

當(dāng)代碼改成如下的時(shí):

#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());

}

運(yùn)行結(jié)果:

?

總結(jié)

以上是生活随笔為你收集整理的2.标签CCLabelTTF,CCLabelAtlas,CCLabelBMFont的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。