Cocos2d-x3.2 文字显示

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

要点:首先要把资源文件加载到工程的Resource文件夹下,切记在函数中输入的是“文件名(xxx.fnt)”,不要把路径“fonts/”也加到里面去,不然会报错!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值