MAC_COCOS2D-X学习——Cocos2dx文本显示

    这次来讲讲cocos2dx中的文本,我会使用LabelBMFont,LabelTTF,LabelAtlas来创建文本。仅仅是简单的例子而已。

    先复制一下资源文件到font


    那么开始吧,首先试试LabelBMFont,修改HelloWorld.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    auto sprite = Sprite::create("HelloWorld.png");
    if (sprite == nullptr)
    {
        problemLoading("'HelloWorld.png'");
    }
    else
    {
        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        this->addChild(sprite, 0);
    }
    
    
    LabelBMFont *bmfont=LabelBMFont::create("我是DRW.KW", "fonts/BMFont.fnt");
    bmfont->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    addChild(bmfont);
    
    
    
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
    #endif
}

    效果如下,不知道各位有没有注意到文本缺少了“我的”,这是因为我们的资源文件中没有中文,把“我的”去掉吧


    在添加一下效果,修改helloworld.cpp如下

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    auto sprite = Sprite::create("HelloWorld.png");
    if (sprite == nullptr)
    {
        problemLoading("'HelloWorld.png'");
    }
    else
    {
        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        this->addChild(sprite, 0);
    }
    
    
    LabelBMFont *bmfont=LabelBMFont::create("DRW.KW", "fonts/BMFont.fnt");
    bmfont->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    addChild(bmfont);
    
    //取出第一个字并添加旋转动作
    Sprite *wo=(Sprite *)bmfont->getChildByTag(0);
    wo->runAction(RepeatForever::create(RotateBy::create(0.4, 180)));
    
    
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
    #endif
}

    看看效果


    接下来看看,LabelTTf显示文本

    修改HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    auto sprite = Sprite::create("HelloWorld.png");
    if (sprite == nullptr)
    {
        problemLoading("'HelloWorld.png'");
    }
    else
    {
        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        this->addChild(sprite, 0);
    }
    
    
    LabelTTF *labelttf=LabelTTF::create("我是DRW.KW", "bitfont", 30);
    labelttf->setColor(Color3B(255,0,0));//设置颜色
    labelttf->setDimensions(Size(200,100));
    labelttf->setVerticalAlignment(TextVAlignment::TOP);//设置纵向分布
    labelttf->setHorizontalAlignment(TextHAlignment::CENTER);//设置横向分布
    labelttf->setPosition(Point(visibleSize.width/2,visibleSize.height/2));//设置位置
    addChild(labelttf);
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
    #endif
}

    看看效果


    接下来看看LabelAtlas显示文本,修改代码如下

    HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Scene::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();


    auto sprite = Sprite::create("HelloWorld.png");
    if (sprite == nullptr)
    {
        problemLoading("'HelloWorld.png'");
    }
    else
    {
        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        this->addChild(sprite, 0);
    }
    
    
    LabelAtlas *atlas=LabelAtlas::create("LOVE","fonts/BMFont.png",100,50,65);
    atlas->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    addChild(atlas);
    
    
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
    #endif
}
好了,就到这里了,DRW



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d是一种流行的2D游戏引擎,它支持多平台开发,包括iOS、Android、Windows、Mac OS X等。Cocos2d-python是Cocos2d-x游戏引擎的Python版本,它提供了许多强大的功能,如游戏场景、动画、精灵、物理引擎等,非常适合开发2D游戏。 下面是一个简单的Cocos2d-python的helloworld程序: 1. 安装Cocos2d-python 在安装Cocos2d-python之前,需要先安装Python和pip。然后使用pip命令安装Cocos2d-python: ``` pip install cocos2d ``` 2. 创建一个新的Cocos2d-python项目 打开命令行窗口,进入到要创建项目的目录,然后执行以下命令: ``` cocos new mygame -p com.mycompany.mygame -l python ``` 其中,mygame是项目名称,com.mycompany.mygame是包名。 3. 编写helloworld程序 在mygame项目的src目录下创建一个新的Python文件helloworld.py,然后输入以下代码: ```python import cocos class HelloWorld(cocos.layer.Layer): def __init__(self): super(HelloWorld, self).__init__() label = cocos.text.Label( 'Hello, world!', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center' ) label.position = 320, 240 self.add(label) if __name__ == '__main__': cocos.director.director.init() HelloWorldScene = cocos.scene.Scene(HelloWorld()) cocos.director.director.run(HelloWorldScene) ``` 这个程序创建了一个名为HelloWorld的Layer,该Layer包含一个居中显示文本标签“Hello, world!”。最后,它创建了一个Scene,将HelloWorld添加到Scene中,并运行Scene。 4. 运行helloworld程序 在命令行窗口中进入mygame项目的根目录,然后执行以下命令: ``` python src/helloworld.py ``` 程序将会启动,并显示一个居中显示的“Hello, world!”文本标签。 这是一个简单的Cocos2d-python的helloworld程序,你可以在此基础上进一步学习Cocos2d-python的开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值