cocos2d-x 3.2 文字显示方法代码

来源:互联网 发布:北大青鸟网络课程介绍 编辑:程序博客网 时间:2024/05/16 05:13

    方法主要有以下五种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
GameScene.h
 
#include "cocos2d.h"
USING_NS_CC;
class GameScene : public cocos2d::Layer
{
public:
    static :Scene* createScene();
     
    virtual bool init();
     
    void menuCallback(Ref* pSender);
     
    CREATE_FUNC(GameScene);
};




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
GameScene.cpp
 
#include "GameScene.h"
 Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}
 
//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
     
    //获取屏幕大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();
     
    //文字显示
    //方法一
    auto label1 = Label::createWithSystemFont("Jacedy""Consolas", 36);
    label1->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height*0.8));
    label1->setColor(Color3B(255, 0, 0));      //设置字体颜色
    this->addChild(label1);
     
    //方法二
    TTFConfig config("Marker Felt.ttf", 25);
    auto label2 = Label::createWithTTF(config, "Jacedy");
    label2->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height*0.6));
    label2->enableGlow(Color4B::BLUE);     //设置荧光效果,仅限ttf文字
    label2->enableOutline(Color4B(255, 0, 0, 255), 5);      //设置描边,描边宽度为5,仅限ttf文字
    this->addChild(label2);
     
    //方法三(高效,经常使用)
    auto label3 = Label::createWithBMFont("bitmapFontChinese.fnt""五星红旗");
    label3->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height*0.4));
    this->addChild(label3);
     
    //方法四
    auto label4 = Label::createWithCharMap("tuffy_bold_italic-charmap.plist");
    label4->setPosition(Vec2(visibleSize.width*0.6, visibleSize.height*0.8));
    this->addChild(label4);
    label4->setString("Jacedy");
     
    //方法五
    auto label5 = Label::create("Jacedy""Marker Felt", 50);
    label5->setPosition(Vec2(visibleSize.width*0.6, visibleSize.height*0.6));
    this->addChild(label5);
     
    return true;
}
0 0
原创粉丝点击