cocos2dx 字体设置

    //设置字体
    CCLabelTTF * pLabel1 = CCLabelTTF ::create("hello world !", "Thonburi", 24);
//    CCLabelTTF * pLabel1 = CCLabelTTF ::create("hello world !", "STKaiti", 24);
   //  pLabel1->setPosition(ccp(100, 100));
   pLabel1->cocos2d::CCNode::setPosition(100, 100);
    addChild(pLabel1);
    
    
    CCLabelTTF *pLabel2 =CCLabelTTF ::create();
//    设置字体大小
    pLabel2->setFontSize(40);
//    设置字符串
    pLabel2->setString("你好");
    pLabel2->setPosition(ccp(200, 200));
    addChild(pLabel2);
    
    
//    加载图片数字
    CCLabelAtlas* label =CCLabelAtlas::create("Atlas-823", "testfont.png", 48, 64, ' ');
    label->setPosition(ccp(0, 190));
   
    addChild(label);
     label->setColor(ccc3(100, 0, 0));
    
    
    CCLabelAtlas * label2 = CCLabelAtlas::create("label2", "testfont.plist");
    label2->setPosition(ccp(120, 160));
    label2->setColor(ccc3(100,  0, 100));
    addChild(label2);
    
    
    CCLabelBMFont * labelIbm = CCLabelBMFont::create("kkkkk","testChinese.fnt");
//    参数1 :需要显示的文字
//    参数2:字体资源文件的名称
     labelIbm->setPosition(ccp(120, 50));
    addChild(labelIbm);

 在使用CCLabelTTF调用setString 改变显示字符串的时候 , 一个新的OPENGL纹理备创建。也就意味着调用setString函数和创建一个新文本一样的慢

所以建议多用CCLabelAtlas 和CCLabelIBMFont。

转载于:https://www.cnblogs.com/linux-ios/archive/2013/03/30/2990863.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个使用Cocos2d库创建的简单游戏的示例代码: ```c++ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); CREATE_FUNC(HelloWorld); }; Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if (!Scene::init()) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); label->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2, origin.y + closeItem->getContentSize().height / 2)); auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); auto sprite = Sprite::create("HelloWorld.png"); sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(sprite, 0); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } int main(int argc, char** argv) { // 创建应用程序实例 auto application = Application::getInstance(); // 设置设计分辨率和屏幕分辨率匹配 application->setApplicationName("HelloWorld"); application->setCommandLineArguments(argc, argv); // 创建并运行场景 auto scene = HelloWorld::createScene(); application->run(); return 0; } ``` 这是一个简单的 "Hello World" 游戏示例,使用了Cocos2d库来创建一个场景,并在场景中显示一段文字和一个关闭按钮。你可以根据你的需求进行扩展和修改。 请确保你已经安装了Cocos2d库,并配置好开发环境。在编译和运行之前,请确保已将相关资源文件(如字体文件、图片文件等)放置在正确的路径下,并在代码中正确引用它们。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值