您的位置:首页 > 移动开发 > Cocos引擎

Learning Cocos2d-x for WP8(3)——文字篇

2013-03-15 10:01 369 查看
C#兄弟篇Learning Cocos2d-x for XNA(3)——文字篇

文字,是人类文明的象征。

文字显示,可用字符串或文字图片显示。

添加图片到Assets文件夹中





在Classes文件夹中添加用于测试的头文件(.h)和源文件*(.cpp)



ShowTextTest.h

在头文件中include “cocos2d.h”头文件,using 命名空间(namespace)cocos2d。

在其中声明继承Scene(场景)类和Layer(层)类

ShowTextTestScene继承CCScene,实现构造方法、虚构造方法和继承CCNode中的虚函数onEnter()。

ShowTextTestLayer继承CCLayer,实现构造方法和虚构造方法。

源码

#ifndef _SHOW_TEXT_TEST
#define _SHOW_TEXT_TEST

#include "cocos2d.h"

using namespace cocos2d;

class ShowTextTestScene:public CCScene
{
public:
ShowTextTestScene();
~ShowTextTestScene();

virtual void onEnter();
};

class ShowTextTestLayer:public CCLayer
{
public:
ShowTextTestLayer();
~ShowTextTestLayer();
};

#endif


[b]ShowTextTest.cpp[/b]

include头文件"pch.h"和"Classes\ShowTextTest.h"

ShowTextTestScene::onEnter()

在ShowTextTestScene::onEnter()方法实现将ShowTextTestLayer(Layer)实例化,并将对象添加到Scene(场景)中。

ShowTextTestLayer::ShowTextTestLayer()

在ShowTextTestLayer::ShowTextTestLayer()方法中Layer层的Label和Sprite的显示,其中Label以字符串显示Sprite通过图片显示文字。

源码

#include "pch.h"
#include "Classes\ShowTextTest.h"

//------------------------------------------------------------------
//
// ShowTextTestLayer
//
//------------------------------------------------------------------
ShowTextTestLayer::ShowTextTestLayer()
{
//字符串显示
CCLabelTTF* label=CCLabelTTF::labelWithString("ShowTextTest","Arial",24);
CCSize s=CCDirector::sharedDirector()->getWinSize();
label->setPosition(ccp(s.width/2,s.height/1.5f));
this->addChild(label);

//图片显示
CCSprite* imgSGQ=CCSprite::spriteWithFile("imgSGQ.png");
imgSGQ->setPosition(ccp(s.width/2,s.height/3));
this->addChild(imgSGQ);
}

ShowTextTestLayer::~ShowTextTestLayer()
{}

//------------------------------------------------------------------
//
// ShowTextTestScene
//
//------------------------------------------------------------------

ShowTextTestScene::ShowTextTestScene()
{}

ShowTextTestScene::~ShowTextTestScene()
{}

void ShowTextTestScene::onEnter()
{
CCScene::onEnter();
CCLayer* pLayer=new ShowTextTestLayer();
this->addChild(pLayer);
pLayer->release();
}


修改起始页面

打开AppDelegate.cpp,在头部include " Classes\ShowTextTest.h"。



并修改起始Scene场景,用于显示ShowTextTestScene场景。



运行显示效果



著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: