
auto myLabel = Label::createWithBMFont("bitmapRed.fnt","Your Text"); @H_502_3@所有标签中出出的字符都应该能提供.fnt文件,如果找不到,字符就不会被渲染。如果你渲染了一个Label,同时它有丢失字符,记得去查看一个.fnt文件是否完备。 TTF @H_502_3@TrueType字体不需要为每种尺寸的和颜色单独使用字体文件,在绽放时不会失真。要创建这种标签,需要指定.ttf字体文件名,文本字符串和字体大小。 auto myLabel = Label::createWithTTF("Your Text","Marker Felt.ttf",24); @H_502_3@虽然使用TrueType字体比使用位图字体更灵活,但是它渲染速度较慢,并且更改标签的属性(字体,大小)是一项非常消耗性能的 *** 作。 @H_502_3@如果你需要具有相同属性的多个Label对象,那可以创建一个TTFConfig对象来统一配置,TTFConfig对象允许你设置所有标签的共同属性。 // create a TTFConfig files for labels to shareTTFConfig labelConfig;labelConfig.FontfilePath = "myFont.ttf";labelConfig.FontSize = 16;labelConfig.glyphs = GlyphCollection::DYNAMIC;labelConfig.outlinesize = 0;labelConfig.customGlyphs = nullptr;labelConfig.distanceFIEldEnabled = false;// create a TTF Label from the TTFConfig file.auto myLabel = Label::createWithTTF(labelConfig,"My Label Text");@H_502_3@TTFConfig也能用于展示中文、日文、韩文的字符。 SystemFont @H_502_3@这样的标签不要改他的属性,它会使用系统的规则。 标签效果 @H_502_3@在屏幕上有标签后,它们可能看起来很普通,这时你希望让他们变漂亮,Label对象可以对标签应用效果,包括阴影、描边、发光。
auto myLabel = Label::createWithTTF("myFont.ttf","My Label Text",16);// shadow effect is supported by all Label typesmyLabel->enableShadow(); auto myLabel = Label::createWithTTF("myFont.ttf",16);// outline effect is TTF only,specify the outline color desiredmyLabel->enableOutline(color4B::WHITE,1)); auto myLabel = Label::createWithTTF("myFont.ttf",16);// glow effect is TTF only,specify the glow color desired.myLabel->enableGlow(color4B::YELLOW); 菜单 @H_502_3@Menu对象是一种特殊的Node对象。 auto myMenu = Menu::create();@H_502_3@菜单项一般有正常状态和选择状态,菜单项显示时是正常状态,当你点击它时变为选择状态,同时点击菜单还会触发一个回调函数。
// creating a menu with a single item// create a menu item by specifying imagesauto closeItem = MenuItemImage::create("Closenormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));auto menu = Menu::create(closeItem,NulL);this->addChild(menu,1); // creating a Menu from a Vector of itemsVector<MenuItem*> MenuItems;auto closeItem = MenuItemImage::create("Closenormal.png",this));MenuItems.pushBack(closeItem);/* repeat for as many menu items as needed */auto menu = Menu::createWithArray(MenuItems);this->addChild(menu,1); 使用Lambda表达式 @H_502_3@C++11支持Lambda表达式,lambda表达式是匿名函数,使用lambda表达式,能让代码看起来更简洁,同时不会有额外的性能开销。 // create a simple Hello World lambdaauto func = [] () { cout << "Hello World"; };// Now call it someplace in codefunc(); auto closeItem = MenuItemImage::create("Closenormal.png",[&](Ref* sender){ // your code here}); 按钮(button) @H_502_3@按钮有一个正常状态,一个选择状态,还有一个不可点击状态,按钮外观可以根据这三个状态而改变。创建一个按钮并定义一个回调函数很简单,记得在 *** 作的时候要有头文件包含:#include "ui/CocosGUI.hs"。
auto button = button::create("normal_image.png","selected_image.png","@R_419_4610@_image.png");button->setTitleText("button Text");button->addtouchEventListener([&](Ref* sender,Widget::touchEventType type){ switch (type) { case ui::Widget::touchEventType::BEGAN: break; case ui::Widget::touchEventType::ENDED: std::cout << "button 1 clicked" << std::endl; break; default: break; }});this->addChild(button); 复选框(CheckBox)#include "ui/CocosGUI.h"auto checkBox = CheckBox::create("check_Box_normal.png","check_Box_normal_press.png","check_Box_active.png","check_Box_normal_disable.png","check_Box_active_disable.png");checkBox->addtouchEventListener([&](Ref* sender,Widget::touchEventType type){ switch (type) { case ui::Widget::touchEventType::BEGAN: break; case ui::Widget::touchEventType::ENDED: std::cout << "checkBox 1 clicked" << std::endl; break; default: break; }});this->addChild(checkBox); 进度条(Loadingbar) #include "ui/CocosGUI.h"auto loadingbar = Loadingbar::create("Loadingbarfile.png");// set the direction of the loading bars progressloadingbar->setDirection(Loadingbar::Direction::RIGHT);this->addChild(loadingbar); #include "ui/CocosGUI.h"auto loadingbar = Loadingbar::create("Loadingbarfile.png");loadingbar->setDirection(Loadingbar::Direction::RIGHT);// something happened,change the percentage of the loading barloadingbar->setPercent(25);// more things happened,change the percentage again.loadingbar->setPercent(35);this->addChild(loadingbar); 滑动条(SlIDer) #include "ui/CocosGUI.h"auto slIDer = SlIDer::create();slIDer->loadbarTexture("SlIDer_Back.png"); // what the slIDer looks likeslIDer->loadSlIDBallTextures("SlIDerNode_normal.png","SlIDerNode_Press.png","SlIDerNode_disable.png");slIDer->loadProgressbarTexture("SlIDer_Pressbar.png");slIDer->addtouchEventListener([&](Ref* sender,Widget::touchEventType type){ switch (type) { case ui::Widget::touchEventType::BEGAN: break; case ui::Widget::touchEventType::ENDED: std::cout << "slIDer moved" << std::endl; break; default: break; }});this->addChild(slIDer); @H_502_3@从上例可以看出,实现一个滑动条需要提供5张图像,对应滑动条的不同部分不同状态,分别为:滑动条背景、上层进度条、正常显示的滑动端点、滑动时的滑动端点、不可用时的滑动端点。 文本框(TextFIEld) #include "ui/CocosGUI.h"auto textFIEld = TextFIEld::create("","Arial",30);textFIEld->addtouchEventListener([&](Ref* sender,Widget::touchEventType type){ std::cout << "editing a TextFIEld" << std::endl;});this->addChild(textFIEld); @H_502_3@提供的文本框架对象,是多功能的,能满足所有的输入需求,比如用户密码的输入,限制用户可以输入的字符数等等。 #include "ui/CocosGUI.h"auto textFIEld = TextFIEld::create("",30);// make this TextFIEld password enabledtextFIEld->setPasswordEnabled(true);// set the maximum number of characters the user can enter for this TextFIEldtextFIEld->setMaxLength(10);textFIEld->addtouchEventListener([&](Ref* sender,Widget::touchEventType type){ std::cout << "editing a TextFIEld" << std::endl;});this->addChild(textFIEld); 总结 以上是内存溢出为你收集整理的cocos2d-x UI组件全部内容,希望文章能够帮你解决cocos2d-x UI组件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)