cocos2d-x学习笔记(19)--label 、label atlas
cocos2d-x学习笔记(19)--label 、label atlas
在正式开讲前,还是要做一些准备工作。
首先讲讲一个ccV3F_C4B_T2F_Quad类的使用,大家不要被这个长长的名称吓到,只需来个刨根问底,就对它的使用方法一目了然了。下面我把,官方文档上的资料整理了一下放在一起:
[plain]
ccV3F_C4B_T2F_Quad
member list:
ccV3F_C4B_T2F tl(top left)
ccV3F_C4B_T2F bl(bottom left)
ccV3F_C4B_T2F tr(top rignt)
ccV3F_C4B_T2F br(bottom right)
Detailed Description:a Point with a vertex point, a tex coord point and a color 4B
(ccV3F_C4B_T2F这个点是有顶点、纹理坐标和颜色设置三个组成)
ccV3F_C4B_T2F
member list:
ccVertex3F vertices
ccColor4B colors
ccTex2d texCoords
Detailed Description:A vertex composed of 2 floats: x, y.
(ccVertex3F这个顶点是有x、y坐标组成(注:2D游戏只用到平面坐标,无需z坐标))
ccVertex3F
member list:
GLfloat x
GLfloat y
GLfloat z
Detailed Description:RGBA color composed of 4 bytes.
ccColor4B
member list:
GLubyte r
GLubyte g
GLubyte b
GLubyte a
Detailed Description:A texcoord composed of 2 floats: u, y.
ccTex2F
member list:
GLfloat u
GLfloat v
就是说ccV3F_C4B_T2F_Quad这个类有四个成员属性,它们都是ccV3F_C4B_T2F类,分别表示左上,左下,右上,右下;
而ccV3F_C4B_T2F这个类有三个成员属性,它们分别是设置顶点坐标、纹理坐标和颜色;
然后就是对应上面三个类(ccVertex3F、ccColor4B和ccTex2d)的成员属性了;
第二个要补充的是ASICC表,这里我先上传,用处到后面再解释:
接下来就开始进入主题了..................
step1:创建cocos2d-x工程,命名为labelAtlas;
step2:在HelloWorldScene.h中添加四个类:
[plain]
class BasicLayer:public CCLayer
{
public:
BasicLayer();
~BasicLayer();
void backMenuCallback(CCObject* pSender);
void restartMenuCallback(CCObject* pSender);
void nextMenuCallback(CCObject* pSender);
};
class TextureAtlasTest:public BasicLayer
{
protected:
CCTextureAtlas* m_atlas;
public:
TextureAtlasTest();
~TextureAtlasTest();
virtual void draw();
};
class LabelAtlasTest:public BasicLayer
{
protected:
ccTime m_time;
public:
LabelAtlasTest();
~LabelAtlasTest();
void update(ccTime dt);
};
class LabelTest:public BasicLayer
{
protected:
ccTime m_time;
public:
LabelTest();
~LabelTest();
void update(ccTime dt);
};
step3:在HelloWorldScene.cpp中添加下面代码:
先添加一些常量和全局变量
[plain]
static int index = 1;
const int MAX_INDEX = 3;
const int tagAtlas = 1;//用于标记CCLabelAtlas对象
const int tagLabel = 2;//用于标记CCLabelTFT对象
[plain]
CCLayer* runThisTest(int index)
{
switch(index)
{
case 1:
return new TextureAtlasTest();
case 2:
return new LabelAtlasTest();
case 3:
return new LabelTest();
}
return NULL;
}
[plain]
CCLayer* runThisTest(int index)
{
switch(index)
{
case 1:
return new TextureAtlasTest();
case 2:
return new LabelAtlasTest();
case 3:
return new LabelTest();
}
return NULL;
}
然后添加成员函数:
[plain]
/************************************************************************/
/*BasicLayer */
/************************************************************************/
BasicLayer::BasicLayer()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF* backLabel = CCLabelTTF::labelWithString("back", "Arial", 30);
CCLabelTTF* restartLabel = CCLabelTTF::labelWithString("restart", "Arial", 30);
CCLabelTTF* nextLabel = CCLabelTTF::labelWithString("next", "Arial", 30);
CCMenuItemLabel* backItem = CCMenuItemLabel::itemWithLabel(backLabel, this, menu_selector(BasicLayer::backMenuCallback));
CCMenuItemLabel* restartItem = CCMenuItemLabel::itemWithLabel(restartLabel, this, menu_selector(BasicLayer::restartMenuCallback));
CCMenuItemLabel* nextItem = CCMenuItemLabel::itemWithLabel(nextLabel, this, menu_selector(BasicLayer::nextMenuCallback));
CCMenu* backMenu = CCMenu::menuWithItem(backItem);
CCMenu* restartMenu = CCMenu::menuWithItem(restartItem);
CCMenu* nextMenu = CCMenu::menuWithItem(nextItem);
addChild(backMenu,1);
addChild(restartMenu,1);
addChild(nextMenu,1);
backItem->setPosition(ccp(size.width / 2 - 100, 50));
restartItem->setPosition(ccp(size.width / 2 , 50));
nextItem->setPosition(
补充:移动开发 , 其他 ,