Cocos2dx 中文標籤、中文菜單、用標籤創建菜單

text.xml文件

  • 新建一個txt文件
  • 按下面的格式輸入鍵值對
  • 另存爲UTF-8格式的.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>play</key>
<string>開始遊戲</string>
<key>title</key>
<string>標題</string>
</dict>

step1讀取xml文件

	//讀取xml文件
	auto dic = Dictionary::createWithContentsOfFile("fonts/text.xml");

step2通過xml讀取中文到字符串

	//通過xml讀取中文到字符串
	auto str = (String*)(dic->objectForKey("title"));

step3用中文字符串創建中文標籤

	//創建標籤
	auto title = Label::create();
	title->setString(str->getCString());
	title->setSystemFontSize(60);
	title->setColor(Color3B(174.0f, 111.0f, 23.0f));
	title->setPosition(Vec2(visibleSize.width / 2, 
		visibleSize.height - title->getContentSize().height));
	this->addChild(title);

中文菜單

用中文標籤創建

	//用中文標籤初始化菜單項
	auto start_menu = MenuItemLabel::create(title,CC_CALLBACK_1(StartGame::startPlay,this));
	//創建菜單
	auto menu = Menu::create(start_menu, NULL);
	menu->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 4));
	this->addChild(menu);

用中文字符串直接創建

	MenuItemFont::setFontName("Arial");
	MenuItemFont::setFontSize(40);
	MenuItemFont *item1 = MenuItemFont::create(str->getCstring(),
		CC_CALLBACK_1(StartGame::startPlay, this));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章