大佬教程收集整理的这篇文章主要介绍了1cocos2dx扩展库UI控件,CCControlSlider,CCScale9Sprite(九妹图),CCControlSwitch,CCControlButton,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
UI控件来自cocos2dx的扩展库,完善了UI方面的元素,使cocos2dx更加丰富多彩。使用扩展库需包含:
第一个参数表示,slider滑动的轨道,即背景色。第二个参数表示滑动的进度。第三个参数表示拖动的按钮。
设置事件的响应函数
关于CCControlEvent
#ifndef __T12UI_H__ #define __T12UI_H__
#include "cocos2d.h" #include "TBACk.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT;
class T12UI :public TBACk public: static CCScene * scene(); CREATE_FUNC(T12UI); bool init();
CCLabelAtlas * atlas;
//slider的回调函数 void sliderCallBACk(CCObject* sender,CCControlEvent event); };
#endif |
T12UI.cpp |
#include "T12UI.h" #include "AppMacros.h" #include "SimpleAudioENGIne.h" using namespace CocosDenshion;
CCScene *T12UI::scene() scene = CCScene::create(); T12UI * layer = create(); scene->addChild(layer); return scene; }
//UI控件来自cocos2dx的扩展库,完善了UI方面的元素,使cocos2dx更加丰富多彩。使用扩展库需要包含 bool init()
//第一个参数表示slider滑动的轨道,即背景色。第二个参数表示滑动的进度。 //第三个参数表示拖动的按钮 CCControlSlider *slider = CCControlSlider::create("sliderTrack.png","sliderProgress.png",21); font-family:新宋体; font-size:9.5pt">"sliderThumb.png");
//设置滑动最大值 slider->setMaximumValue(2.0f); //设置滑动的最小值 slider->setMinimumValue(0.0f);
//设置默认值 //设置某一范围内的最大值,当移动到了1.2之后移动不了了 slider->setMaximumAllowedValue(1.2f); //设置某一范围内的最小值,向左移动到0.3之后移动不了了 slider->setMinimumAllowedValue(0.3f); //设置slider的所在位置 slider->setPosition(ccp(winSize.width / 2,winSize.height/2 - 30));
slider->addTargetWithActionForControlEvents( this, cccontrol_SELEctor(sliderCallBACk),138); font-family:新宋体; font-size:9.5pt">CCControlEventValueChanged);
CCString *str = CCString::createWithFormat("%.2g",slider->getValue()); //第一个参数表示要显示的字符串 //第二个参数表示从哪张图片中取值 //第三个参数表示的是每个字的宽度width //第四个参数表示的是每个字的高度 //第五个参数表示的是起始的字符 /* creates the CCLabelAtlas with a String,a char map file(the atlas), the width and height of each element and the starTing char of the atlas */ atlas = CCLabelAtlas::create( str->getCString(), "fonts/fps_images.png", 12,32,21); font-family:新宋体; font-size:9.5pt">'.'); atlas->setAnchorPoint(ccp(0.5,0.5)); //设置字体的放大效果 atlas->setScale(2.0f); atlas->@H_401_1673@winSize.height / 2 + 30)); addChild(atlas);
addChild(slider);
return true; }
//设置slider的回调函数 //这里的sender表示发送的一者 void CCControlEvent event) CCControlSlider * slider = (CCControlSlider *)sender;
getValue()); //因为成为了全局的了,所以能够访问的到 atlas->setString(str->getCString()); } |
运行结果: 最大值
最小范围:
最大范围: 运行结果在0.3和1.2之间 |
第一个参数,掩底背景图片,第二个参数为开的图片,第三个参数为关的图片,第四个参数为手指划到按钮,第五,六个参数分别为开和关显示的文字。
设置时间触发后的响应函数
如何在响应函数中获取选项
5 CCControlSwitch案例说明
init();
//开关的回调函数
//通过SimpleAudioENGIne的方式实现加载音乐 SimpleAudioENGIne::sharedENGIne()->preloadBACkgroundMusic("audio/start.wav"); //创建开关、 //第一个参数为:掩底背景CCSprite //第二个参数为开的CCSprite //第三个参数为关的CCSprite //第四个参数为手指滑到CCSprite //第五个参数on的label //第六个参数为off的label CCControlSwitch *sw = CCControlSwitch::create( CCSprite::"switch-mask.png"), "switch-on.png"),21); font-family:新宋体; font-size:9.5pt">"switch-off.png"),21); font-family:新宋体; font-size:9.5pt">"switch-thumb.png"),133); font-family:新宋体; font-size:9.5pt">CCLabelTTF::"ON",21); font-family:新宋体; font-size:9.5pt">"Courier New",21); font-family:新宋体; font-size:9.5pt">"OFF",20) //设置开关的位置 sw->@H_401_1673@winSize.height / 2)); sw->addTargetWithActionForControlEvents(this,0); font-family:新宋体; font-size:9.5pt">switchCallBACk),138); font-family:新宋体; font-size:9.5pt">CCControlEventValueChanged);
//设置开关默认是关闭的 //将开关添加到Layer中去 addChild(sw);
return true; }
//开关的回调函数 void CCControlSwitch * sw = (CCControlSwitch *)sender; if (sw->isOn()) CCLog("click On"); //通过playBACkgroundMusic打开音乐 playBACkgroundMusic("audio/start.wav"); } else //通过stopBACkgroundMusic()关闭音乐 stopBACkgroundMusic("audio/start.wav"); "click off"); } } |
运行结果:
|
CCScale9Sprite对象,是一种CCSprite对象的变形,它的用法和CCSprite类似,不同点是:CCScale9Sprite对象有个特性就是缩放贴图时可以尽量不失帧。比如QQ聊天内边框
原理:
CCScale9Sprite的实现非常巧妙,是通过1个CCSpriteBatchNode和9个CCSprite来实现的,原理很简单,通过将原纹理资源切割成9部分(PS:这也是叫九宫图的原因)。根据想要的尺寸,完成以下三个步骤:
单向拉伸4条边(即在4个角两两之间的边,比如上边,只做横向拉伸)
CCSpriteBatchNode的资源为整个的纹理,9 个CCSprite 对应于纹理的9
个部分(根据纹理不同,9 部分所占比例会有所不同),根据想要的尺寸,
将9 部分拼装在一起!
#include “cocos-ext.h” //包含cocos-ext.h头文件
using namespace cocos2d::extension; //引用cocos2d::extension 命名空间
使用说明:
CCScale9Sprite::create(const char* file,CCRect rect,CCRect,capInsets);
第一个参数为文件,第二个参数使用文件的大小,第三个参数如下,若未设置,或设置图分别如下:
我们知道CCSprite的拉伸方式是通过setScale();来实现的,而对于CCScale9Sprite则不同。它是通过setContentSize(constCCSize & sizE);来实现图片的拉伸。
测试代码:
关于CCScale9Sprite::create()
init(); };
#endif |
CCScale9Sprite * s9spr = CCScale9Sprite::create( "scale9.png",138); font-family:新宋体; font-size:9.5pt">CCRectMake(0,138); font-family:新宋体; font-size:9.5pt">CCRectMake(30,40,56,20)); s9spr->@H_401_1673@winSize.height / 2)); addChild(s9spr); s9spr->setPreferredSize(CCSize(500,100)); return true; } |
运行结果:
|
响应的事件类型如下:
init();
void touchDownCallBACk(CCControlEvent event); void touchDragInsideCallBACk(scene; }
bool init(); CCScale9Sprite *bgButton = "button.png"); CCScale9Sprite *bgButtonLighted = "buttonHighlighted.png"); CCLabelTTF * text = "Touch Me",21); font-family:新宋体; font-size:9.5pt">"Couier New",50);
CCControlButton * button = CCControlButton::create(text,bgButton); //为按钮添加位置 button->@H_401_1673@winSize.height / 2)); button->setBACkgroundSpriteForState(bgButtonLighted,138); font-family:新宋体; font-size:9.5pt">CCControlStateHighlighted); button->settitleColorForState(ccRED,138); font-family:新宋体; font-size:9.5pt">CCControlStateHighlighted); addChild(button);
//为按钮添加监听事件,添加的是按钮被点击的事件 button->touchDownCallBACk),138); font-family:新宋体; font-size:9.5pt">CCControlEventTouchDown); //为按钮添加监听事件,添加的是按钮Drag的事件 button->touchDragInsideCallBACk),138); font-family:新宋体; font-size:9.5pt">CCControlEventTouchDragInside);
return true; }
void CCControlEvent event) }
void "touchDragInsideCallBACk"); } |
运行结果:
|
以上是大佬教程为你收集整理的1cocos2dx扩展库UI控件,CCControlSlider,CCScale9Sprite(九妹图),CCControlSwitch,CCControlButton全部内容,希望文章能够帮你解决1cocos2dx扩展库UI控件,CCControlSlider,CCScale9Sprite(九妹图),CCControlSwitch,CCControlButton所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。