//下面这段代码添加到init里面 CCMenuItemFont::setFontSize(32); //设置字号 CCMenuItemFont::setFontName("Marker Felt"); //设置字体 CCMenuItemFont *font1=CCMenuItemFont::create("hah",this,menu_selector(MainUI::menuChangeStart));//添加文字按钮,点击实现缩放 font1->setColor(ccc3(255,0)); //设置颜色 CCMenuItemToggle * font2=CCMenuItemToggle::createWithTarget(this,menu_selector(MainUI::menuChangeSet),CCMenuItemFont::create("on"),CCMenuItemFont::create("off"),NULL); //添加选择按钮,点击实现on与off之间切换 font2->setColor(ccc3(0,255,0)); font2->setSelectedIndex(1); //设置默认显示,从零算起(此为off) CCMenu *newmenu=CCMenu::create(font1,font2,NULL); // 此步很关键,否则只是菜单项,但不能点击 newmenu->setPosition(ccp(visibleSize.width/2,visibleSize.height/2)); newmenu->alignItemsVertically(); //设置为竖排排列 this->addChild(newmenu,2);
label = LabelTTF::create("Last button: None","Marker Felt",32); label->setPosition(Point(winSize.width / 2,winSize.height - 80)); label->setHorizontalAlignment(TextHAlignment::CENTER); this->addChild(label);先定义了文字,在其他地方修改。label定义成全局变量
label->setString("Last button: * ");
ok了,很简单
引用类库
1,在你的工程中添加对头文件cocos2dx\platform\third_party\win32\iconv\iconv.h的引用。
2,相应的库引用:cocos2dx\platform\third_party\win32\libraries\libiconv.lib。
添加汉字
inline void HelloWorld::WStrToUTF8(std::string& dest,const std::wstring& src){ dest.clear(); for (size_t i = 0; i < src.size(); i++){ wchar_t w = src[i]; if (w <= 0x7f) dest.push_back((char)w); else if (w <= 0x7ff){ dest.push_back(0xc0 | ((w >> 6)& 0x1f)); dest.push_back(0x80| (w & 0x3f)); } else if (w <= 0xffff){ dest.push_back(0xe0 | ((w >> 12)& 0x0f)); dest.push_back(0x80| ((w >> 6) & 0x3f)); dest.push_back(0x80| (w & 0x3f)); } else if (sizeof(wchar_t) > 2 && w <= 0x10ffff){ dest.push_back(0xf0 | ((w >> 18)& 0x07)); // wchar_t 4-bytes situation dest.push_back(0x80| ((w >> 12) & 0x3f)); dest.push_back(0x80| ((w >> 6) & 0x3f)); dest.push_back(0x80| (w & 0x3f)); } else dest.push_back('?'); } } //! simple warpper inline std::string HelloWorld::WStrToUTF8(const std::wstring& str){ std::string result; WStrToUTF8(result,str); return result; }在要引用的地方添加这段就ok了
std::string text = WStrToUTF8(L"你好世界"); auto label = LabelTTF::create(text,"Arial",24); label->setPosition(Point(200,200)); this->addChild(label);我也是看网上的,原理不是很懂,不过可以实现,要是有懂原理的,希望留个言