Cocos2d-x新UI解决方案的使用
2015-05-24 23:14
260 查看
参考了几篇文章:
http://io.diveinedu.com/2015/01/13/Cocos2d-3.x%E4%B8%ADButton%E7%9A%84%E4%BD%BF%E7%94%A8.html
http://www.cocos2d-x.org/wiki/UI#Label
说实话,官方文档不是很好,很多库的使用方法都没有提到,从官方示例项目中找的代码也是添加了很多依赖的,稍微不注意一点就踩坑。
使用Button正如第一篇文章所说的那样:
然后再cpp中就可以:
使用CheckBox的话监听回调函数就需要不一样的参数了:
Slider:
效果:
LoadingBar:
使用cocos2d-x定时更新机制制造动态效果:
效果:
ImageView的创建和一般的图片精灵一样:
据传说ImageView是可以使用PLISH图像的,但是没测试。
Text类似Label,但是只是TTF的Label:
TextBMFont和TextAtlas没试,但那时看示例和一般的BMFLabel等相同。
RichText和TextField暂时用不上,也没看。
http://io.diveinedu.com/2015/01/13/Cocos2d-3.x%E4%B8%ADButton%E7%9A%84%E4%BD%BF%E7%94%A8.html
http://www.cocos2d-x.org/wiki/UI#Label
说实话,官方文档不是很好,很多库的使用方法都没有提到,从官方示例项目中找的代码也是添加了很多依赖的,稍微不注意一点就踩坑。
使用Button正如第一篇文章所说的那样:
#ifndef __THIRD_SCENE_H__ #define __THIRD_SCENE_H__ #include "cocos2d.h" #include "ui/CocosGUI.h" class UIScene : public cocos2d::Layer{ public: static cocos2d::Scene* createScene(); virtual bool init(); void touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type); CREATE_FUNC(UIScene); }; #endif //__THIRD_SCENE_H__
然后再cpp中就可以:
#include "UIScene.h" #include "HelloWorldScene.h" USING_NS_CC; Scene* UIScene::createScene(){ auto scene = Scene::create(); auto layer = UIScene::create(); scene->addChild(layer); return scene; } bool UIScene::init(){ if (!Layer::init()){ return false; } auto btn = ui::Button::create("CloseNormal.png", "CloseSelected.png"); btn->setTitleText("TextButton"); btn->setPosition(Vec2(150,100)); btn->addTouchEventListener(CC_CALLBACK_2(UIScene::touchEvent,this)); this->addChild(btn); return true; } void UIScene::touchEvent(Ref *pSender, ui::Widget::TouchEventType type){ Director::getInstance()->end(); }
使用CheckBox的话监听回调函数就需要不一样的参数了:
#include "UIScene.h" USING_NS_CC; Scene* UIScene::createScene(){ auto scene = Scene::create(); auto layer = UIScene::create(); scene->addChild(layer); return scene; } bool UIScene::init(){ if (!Layer::init()){ return false; } auto check = ui::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"); check->setPosition(Vec2(240, 160)); check->addEventListener(CC_CALLBACK_2(UIScene::selectedEvent, this)); this->addChild(check); return true; } void UIScene::selectedEvent(Ref* pSender, ui::CheckBox::EventType type){ // Director::getInstance()->end(); }
Slider:
bool UIScene::init(){ if (!Layer::init()){ return false; } auto mySlider = Slider::create(); mySlider->loadBarTexture("background.png"); mySlider->loadSlidBallTextures("sliderThumb.png","sliderThumb.png","sliderThumb.png"); mySlider->loadProgressBarTexture("sliderProgress.png"); mySlider->setPosition(Vec2(240, 160)); this->addChild(mySlider,1); return true; }
效果:
LoadingBar:
bool UIScene::init(){ if (!Layer::init()){ return false; } auto myload = LoadingBar::create(); myload->setName("LoadingBar"); myload->loadTexture("sliderProgress.png"); myload->setPercent(50); myload->setDirection(LoadingBar::Direction::LEFT); myload->setPosition(Vec2(240,160)); this->addChild(myload); return true; }
使用cocos2d-x定时更新机制制造动态效果:
bool UIScene::init(){ if (!Layer::init()){ return false; } auto myload = LoadingBar::create(); myload->setName("LoadingBar"); myload->loadTexture("sliderProgress.png"); myload->setPercent(50); myload->setDirection(LoadingBar::Direction::LEFT); myload->setPosition(Vec2(240,160)); myload->setTag(0); this->addChild(myload); scheduleUpdate(); return true; } void UIScene::update(float delta){ if (count > 100){ count = 0; } ++count; auto myload = static_cast<LoadingBar*>(this->getChildByTag(0)); myload->setPercent(count); }
效果:
ImageView的创建和一般的图片精灵一样:
bool UIScene::init(){ if (!Layer::init()){ return false; } auto imageView = ImageView::create("HelloWorld.png");; imageView->setPosition(Vec2(240, 160)); this->addChild(imageView); return true; }
据传说ImageView是可以使用PLISH图像的,但是没测试。
Text类似Label,但是只是TTF的Label:
bool UIScene::init(){ if (!Layer::init()){ return false; } auto text = Text::create("Text UI","fonts/arial.ttf",40); text->setPosition(Vec2(240, 160)); this->addChild(text); return true; }
TextBMFont和TextAtlas没试,但那时看示例和一般的BMFLabel等相同。
RichText和TextField暂时用不上,也没看。
相关文章推荐
- 枫叶天空Cocos2d-x3.0系列教程三 ui教程3 自定义组件和使用CocoStudio UI编辑器
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- cocos2d-x-2.0 新增UI的使用方法
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- Cocos2d-x 3.0 开发(四)使用CocoStudio创建UI并载入到程序中
- 在cocos2d-js-v3.0 中使用cocostudio UI的正确方式
- HMI全液晶仪表方案-使用QNX和Kanzi的UI解决方案
- Android中使用Handler机制更新UI的三种解决方案
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- 使用cocos2d-x显示瓦片地图,发现两个瓦片衔接的地方有黑线,找了一下解决方案:
- UIKit和Cocos2d-x的整合使用;设置CCEAGLView背景为透明,来显示背后的UIView; 以及整合中遇到的问题的解决方案
- cocos2d-x里的TiledMap出现黑线和抖动的解决方案(不影响其他类使用)
- cocos2d-x 3.2 中使用cocostudioUI动画 二次加载播放时的bug解决
- Android中使用Handler机制更新UI的三种解决方案
- Cocos2d-x 3.9教程:10.使用CocosStudio的UI编辑器从UI文件中加载布局和控件
- Android开发笔记-使用Handler机制更新UI的三种解决方案
- cocos2d-x cocoStudioUI编辑器导出文件的使用
- cocos2d-x cocoStudioUI编辑器导出文件的使用
- cocos2d-x3.3 + cocostudio1.5 scene+ui使用
- 在cocos2d-js-v3.0 中使用cocostudio UI的正确方式