Cocos2dx之文字显示

前端之家收集整理的这篇文章主要介绍了Cocos2dx之文字显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

C++之旅 QQ群:201704374

伸手党的救星,1T资料等你来战



一、显示文字的控件

1、CCLabel

2、CCLabelTTF

3、CCLabelAtlas


二、文字显示实现

1、CCLabel

#ifndef __T07CCLabel_H__
#define __T07CCLabel_H__
#include "cocos2d.h"
USING_NS_CC;

class T07CCLabel : public CCLayer
{
public:
	static CCScene* scene();
	CREATE_FUNC(T07CCLabel);
	bool init();
	bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
	void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
};

#endif

#include "T07CCLabel.h"

static const char* _font[] = {
	"AppleGothic","HiraKakuProN-W6","HiraKakuProN-W3","MarkerFelt-Thin","STHeitiK-Medium","STHeitiK-Light","TimesNewRomanPSMT","Helvetica-Oblique","Helvetica-BoldOblique","Helvetica","Helvetica-Bold","TimesNewRomanPS-BoldMT","TimesNewRomanPS-BoldItalicMT","TimesNewRomanPS-ItalicMT","Verdana-Bold","Verdana-BoldItalic","Verdana","Verdana-Italic","Georgia-Bold","Georgia","Georgia-BoldItalic","Georgia-Italic","ArialRoundedMTBold","TrebuchetMS-Italic","TrebuchetMS","Trebuchet-BoldItalic","TrebuchetMS-Bold","STHeitiTC-Light","STHeitiTC-Medium","GeezaPro-Bold","GeezaPro","Courier","Courier-BoldOblique","Courier-Oblique","Courier-Bold","ArialMT","Arial-BoldMT","Arial-BoldItalicMT","Arial-ItalicMT","STHeitiJ-Medium","STHeitiJ-Light","ArialHebrew","ArialHebrew-Bold","CourierNewPS-BoldMT","CourierNewPS-BoldItalicMT","CourierNewPSMT","Thonburi-Bold","AmericanTypewriter","AmericanTypewriter-Bold","STHeitiSC-Medium","STHeitiSC-Light","HelveticaNeue","HelveticaNeue-Bold","Zapfino"
};
CCScene* T07CCLabel::scene()
{
	CCScene* scene = CCScene::create();
	T07CCLabel* layer = T07CCLabel::create();
	scene->addChild(layer);
	return scene;
}

bool T07CCLabel::init()
{
	CCLayer::init();
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	
#if 0
	CCLabelTTF* ttf = CCLabelTTF::create("Content","Arial",24);
	addChild(ttf);
	ttf->setPosition(ccp(winSize.width/2,winSize.height/2));
#endif

	int count = sizeof(_font)/sizeof(*_font);
	for (int i =0; i < count; i++)
	{
		CCLabelTTF* ttf = CCLabelTTF::create(_font[i],_font[i],24);
		addChild(ttf);
		ttf->setPosition(ccp(winSize.width/2,i * 24 + 12));
	}

	setTouchEnabled(true);
	setTouchMode(kCCTouchesOneByOne);
	return true;
}
bool T07CCLabel::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
	return true;
}
void T07CCLabel::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)
{
	this->setPositionY(getPositionY() + pTouch->getDelta().y);
}

2、CCLabelTTF

3、CCLabelAtlas

#ifndef __T08CCLabelAtlas_H__
#define __T08CCLabelAtlas_H__

// CCLabelTTF CCLabelAtlas CCLabelBMFont

#include "cocos2d.h"
USING_NS_CC;

class T08CCLabelAtlas : public CCLayer
{
public:
	static CCScene* scene();
	CREATE_FUNC(T08CCLabelAtlas);
	bool init();

};

#endif
#include "T08CCLabelAtlas.h"


CCScene* T08CCLabelAtlas::scene()
{
	CCScene* s = CCScene::create();
	T08CCLabelAtlas* layer = T08CCLabelAtlas::create();
	s->addChild(layer);
	return s;
}

bool T08CCLabelAtlas::init()
{
	CCLayer::init();

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();

	CCLabelAtlas* atlas = CCLabelAtlas::create("123abb123","labelatlasimg.png",24,32,'0');
	addChild(atlas);
	atlas->setPosition(ccp(winSize.width / 2,winSize.height / 2));

//	CCSprite* sp = CCSprite::create("CloseNormal.png");
//	addChild(sp);
//	sp->setPosition(ccp(winSize.width / 2,winSize.height / 2));

	return true;
}


三、应用场合

1、CCLabel

固定不变是文字显示

2、CCLabelTTF

每个文字可以是精灵,经行精灵操作

3、CCLabelAtlas

分数显示

原文链接:https://www.f2er.com/cocos2dx/341982.html

猜你在找的Cocos2d-x相关文章