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);};
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/”也加到里面去,不然会报错!