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

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,实现构造方法和虚构造方法。

源码

 1 #ifndef _SHOW_TEXT_TEST
 2 #define _SHOW_TEXT_TEST
 3 
 4 #include "cocos2d.h"
 5 
 6 using namespace cocos2d;
 7 
 8 class ShowTextTestScene:public CCScene
 9 {
10 public:
11     ShowTextTestScene();
12     ~ShowTextTestScene();
13 
14     virtual void onEnter();
15 };
16 
17 class ShowTextTestLayer:public CCLayer
18 {
19 public:
20     ShowTextTestLayer();
21     ~ShowTextTestLayer();
22 };
23 
24 #endif

ShowTextTest.cpp

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

ShowTextTestScene::onEnter()

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

 

ShowTextTestLayer::ShowTextTestLayer()

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

 

源码

 1 #include "pch.h"
 2 #include "Classes\ShowTextTest.h"
 3 
 4 
 5 //------------------------------------------------------------------
 6 //
 7 // ShowTextTestLayer
 8 //
 9 //------------------------------------------------------------------
10 ShowTextTestLayer::ShowTextTestLayer()
11 {    
12     //字符串显示
13     CCLabelTTF* label=CCLabelTTF::labelWithString("ShowTextTest","Arial",24);
14     CCSize s=CCDirector::sharedDirector()->getWinSize();
15     label->setPosition(ccp(s.width/2,s.height/1.5f));
16     this->addChild(label);
17 
18     //图片显示
19     CCSprite* imgSGQ=CCSprite::spriteWithFile("imgSGQ.png");
20     imgSGQ->setPosition(ccp(s.width/2,s.height/3));
21     this->addChild(imgSGQ);
22 }
23 
24 ShowTextTestLayer::~ShowTextTestLayer()
25 {}
26 
27 //------------------------------------------------------------------
28 //
29 // ShowTextTestScene
30 //
31 //------------------------------------------------------------------
32 
33 ShowTextTestScene::ShowTextTestScene()
34 {}
35 
36 ShowTextTestScene::~ShowTextTestScene()
37 {}
38 
39 void ShowTextTestScene::onEnter()
40 {    
41     CCScene::onEnter();
42     CCLayer* pLayer=new ShowTextTestLayer();
43     this->addChild(pLayer);
44     pLayer->release();
45 }

修改起始页面

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

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

运行显示效果

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

posted @ 2013-03-15 10:01  Ghost Soar  阅读(1462)  评论(1编辑  收藏  举报