1. UI控件来自cocos2dx的扩展库。完好了UI方面的元素,使cocos2dx更加丰富多彩。使用扩展库需包括:

#include “cocos-ext.h”

USING_NS_CC_EXT;

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

  1. CCControlSlider

CCControlSlider * slider = CCControlSlider::create(“sliderTrack.png”,”sliderProgress.png”,”sliderThumb.png”);

第一个參数表示。slider滑动的轨道。即背景色。

第二个參数表示滑动的进度。

第三个參数表示拖动的button。

slider->setMaximumValue(2.0f);  
//设置滑动最大值

slider->setMinimumValue(0.0f);  
//设置滑动最小值

slider->setValue(0.5f);          
//设置默认值

slider->setMaximumAllowedValue(1.2f);  
//设置某一个范围内的最大值

slider->setMinimumAllowedValue(0.3f);  
//设置某一个范围内的最小值

slider->addTargetWithActionForControlEvents(this,

cccontrol_selector(T12UI::controlCallback),

CCControlEventValueChanged);

设置事件的响应函数

typedef unsigned int CCControlEvent;

typedef void (CCObject::*SEL_CCControlHandler)(CCObject*, CCControlEvent);

#define cccontrol_selector(_SELECTOR)(SEL_CCControlHandler)(&_SELECTOR);

关于CCControlEvent

/** Kinds of possible events for the control objects. */

enum

{

CCControlEventTouchDown          
= 1 << 0,    // A touch-down event in the control.

CCControlEventTouchDragInside    
= 1 << 1,    // An event where a finger is dragged inside the bounds of the control.

CCControlEventTouchDragOutside   
= 1 << 2,    // An event where a finger is dragged just outside the bounds of the control.

CCControlEventTouchDragEnter     
= 1 << 3,    // An event where a finger is dragged into the bounds of the control.

CCControlEventTouchDragExit      
= 1 << 4,    // An event where a finger is dragged from within a control to outside its bounds.

CCControlEventTouchUpInside      
= 1 << 5,    // A touch-up event in the control where the finger is inside the bounds of the control.

CCControlEventTouchUpOutside     
= 1 << 6,    // A touch-up event in the control where the finger is outside the bounds of the control.

CCControlEventTouchCancel        
= 1 << 7,    // A system event canceling the current touches for the control.

CCControlEventValueChanged       
= 1 << 8      // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.

};

typedef
unsigned
int
CCControlEvent;

  1. slider案例说明:

T12UI.h

#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()

{

CCScene *
scene =
CCScene::create();

T12UI *
layer =
T12UI::create();

scene->addChild(layer);

return
scene;

}

//UI控件来自cocos2dx的扩展库。完好了UI方面的元素,使cocos2dx更加丰富多彩。使用扩展库须要包括

bool
T12UI::init()

{

TBack::init();

//第一个參数表示slider滑动的轨道,即背景色。第二个參数表示滑动的进度。

//第三个參数表示拖动的button

CCControlSlider *slider
= CCControlSlider::create("sliderTrack.png","sliderProgress.png","sliderThumb.png");

//设置滑动最大值

slider->setMaximumValue(2.0f);

//设置滑动的最小值

slider->setMinimumValue(0.0f);

//设置默认值

slider->setValue(0.5f);

//设置某一范围内的最大值,当移动到了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(T12UI::sliderCallBack),

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,'.');

atlas->setAnchorPoint(ccp(0.5,0.5));

//设置字体的放大效果

atlas->setScale(2.0f);

atlas->setPosition(ccp(winSize.width
/ 2, winSize.height
/ 2 + 30));

addChild(atlas);

slider->setValue(1.3f);

addChild(slider);

return
true;

}

//设置slider的回调函数

//这里的sender表示发送的一者

void
T12UI::sliderCallBack(CCObject*
sender,
CCControlEvent
event)

{

CCControlSlider *
slider = (CCControlSlider
*)sender;

CCString *str
= CCString::createWithFormat("%.2g",
slider->getValue());

//由于成为了全局的了,所以可以訪问的到

atlas->setString(str->getCString());

}

执行结果:

最大值

最小范围:

最大范围:

执行结果在0.3和1.2之间

  1. CCControlSwitch

第一个參数,掩底背景图片,第二个參数为开的图片,第三个參数为关的图片。第四个參数为手指划到button,第五。六个參数分别为开和关显示的文字。

CCControlSwitch * sw = CCControlSwitch::create(

CCSprite::create("switch-mask.png"),

CCSprite::create("switch-on.png"),

CCSprite::create("switch-off.png"),

CCSprite::create("switch-thumb.png"),

CCLabelTTF::create("ON","Courier New",20),

CCLabelTTF::create("OFF","Courier New",20)

);

设置时间触发后的响应函数

sw->addTargetWithActionForControlEvents(this,cccontrol_selector(T12UI::switchCallback),

CCControlEventValueChanged)

怎样在响应函数中获取选项

void T12UI::switchCallback(CCObject * sender,CCControlEvent event)

{

CCControlSwitch * sw = (CCControlSwitch *)sender;

If(sw->isOn())

{

CCLog(“On”);

} else {

CCLog(“off”);

}

}


CCControlSwitch案例说明

T12UI.h

#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();

//开关的回调函数

void
switchCallBack(CCObject*
sender,
CCControlEvent
event);

};

#endif

T12UI.cpp

#include
"T12UI.h"

#include
"AppMacros.h"

#include
"SimpleAudioEngine.h"

using
namespace
CocosDenshion;

CCScene *T12UI::scene()

{

CCScene *
scene =
CCScene::create();

T12UI *
layer =
T12UI::create();

scene->addChild(layer);

return
scene;

}

//UI控件来自cocos2dx的扩展库,完好了UI方面的元素。使cocos2dx更加丰富多彩。

使用扩展库须要包括

bool
T12UI::init()

{

TBack::init();

//通过SimpleAudioEngine的方式实现载入音乐

SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("audio/start.wav");

//创建开关、

//第一个參数为:掩底背景CCSprite

//第二个參数为开的CCSprite

//第三个參数为关的CCSprite

//第四个參数为手指滑到CCSprite

//第五个參数on的label

//第六个參数为off的label

CCControlSwitch *sw
= CCControlSwitch::create(

CCSprite::create("switch-mask.png"),

CCSprite::create("switch-on.png"),

CCSprite::create("switch-off.png"),

CCSprite::create("switch-thumb.png"),

CCLabelTTF::create("ON",
"Courier New", 20),

CCLabelTTF::create("OFF",
"Courier New", 20)

);

//设置开关的位置

sw->setPosition(ccp(winSize.width
/ 2,winSize.height
/ 2));

sw->addTargetWithActionForControlEvents(this,

cccontrol_selector(T12UI::switchCallBack),

CCControlEventValueChanged);

//设置开关默认是关闭的

sw->setOn(false);

//将开关加入到Layer中去

addChild(sw);

return
true;

}

//开关的回调函数

void
T12UI::switchCallBack(CCObject*
sender,
CCControlEvent
event)

{

CCControlSwitch *
sw = (CCControlSwitch
*)sender;

if (sw->isOn())

{

CCLog("click
On");

//通过playBackgroundMusic打开音乐

SimpleAudioEngine::sharedEngine()->playBackgroundMusic("audio/start.wav");

}

else

{

//通过stopBackgroundMusic()关闭音乐

SimpleAudioEngine::sharedEngine()->stopBackgroundMusic("audio/start.wav");

CCLog("click
off");

}

}

执行结果:

  1. CCScale9Sprite九妹图

CCScale9Sprite对象,是一种CCSprite对象的变形,它的使用方法和CCSprite类似,不同点是:CCScale9Sprite对象有个特性就是缩放贴图时能够尽量不失帧。比方QQ聊天内边框

原理:

CCScale9Sprite的实现非常巧妙。是通过1个CCSpriteBatchNode和9个CCSprite来实现的。原理非常easy,通过将原纹理资源分割成9部分(PS:这也是叫九宫图的原因)。

依据想要的尺寸,完毕下面三个步骤:

  1. 保持4个角部分不变形

  2. 单向拉伸4条边(即在4个角两两之间的边,比方上边,仅仅做横向拉伸)

  3. 双向拉伸中间部分(即九宫图的中间部分。横向,纵向同一时候拉伸,PS:拉伸比例不一定同样)

CCSpriteBatchNode的资源为整个的纹理。9
个CCSprite
相应于纹理的9

个部分(依据纹理不同,9
部分所占比例会有所不同)。依据想要的尺寸,

将9
部分拼装在一起!

  1. 须要包括的头文件

#include “cocos-ext.h”                   
//包括cocos-ext.h头文件

using namespace cocos2d::extension;      
//引用cocos2d::extension
命名空间

使用说明:

CCScale9Sprite::create(const char* file,CCRect rect, CCRect, capInsets);

第一个參数为文件。第二个參数使用文件的大小,第三个參数例如以下。若未设置,或设置图分别例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

我们知道CCSprite的拉伸方式是通过setScale();来实现的。而对于CCScale9Sprite则不同。它是通过setContentSize(constCCSize
& size);来实现图片的拉伸。

測试代码:

CCScale9Sprite * spr = CCScale9Sprite::create("scale9.png",CCRectMake(0, 0, 116, 102), CCRectMake(40, 30, 30, 40));

spr->setPosition(ccp(winSize.width/2,winSize.height/2));

addChild(spr);

//spr->setScale(4.0f);

spr->setPreferredSize(CCSizeMake(400,200));

关于CCScale9Sprite::create()

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

T12UI.h

#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();

};

#endif

T12UI.cpp

#include
"T12UI.h"

#include
"AppMacros.h"

#include
"SimpleAudioEngine.h"

using
namespace
CocosDenshion;

CCScene *T12UI::scene()

{

CCScene *
scene =
CCScene::create();

T12UI *
layer =
T12UI::create();

scene->addChild(layer);

return
scene;

}

//UI控件来自cocos2dx的扩展库,完好了UI方面的元素,使cocos2dx更加丰富多彩。使用扩展库须要包括

bool
T12UI::init()

{

TBack::init();

CCScale9Sprite *
s9spr =
CCScale9Sprite::create(

"scale9.png",

CCRectMake(0, 0, 116, 102),

CCRectMake(30, 40, 56, 20));

s9spr->setPosition(ccp(winSize.width
/ 2, winSize.height
/ 2));

addChild(s9spr);

s9spr->setPreferredSize(CCSize(500,100));

return
true;

}

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

  1. CControlButton

CCScale9Sprite * bgbutton = CCScale9Sprite::create("button.png");

//背景色图片

CCScale9Sprite * bgbuttonlighted =

CCScale9Sprite::create("buttonHighlighted.png");

//背景色高亮图片

CCLabelTTF * titlebutton = CCLabelTTF::create("Touch Me", "Courier

New", 30);

//button的文本

CCControlButton * button =

CCControlButton::create(titlebutton,bgbutton);

//创建button

button->setColor(ccc3(159, 168, 176));

//调色

button->setBackgroundSpriteForState(bgbuttonlighted,

CCControlStateHighlighted);

//按下后背景高亮

button->setTitleColorForState(ccWHITE,

CCControlStateHighlighted);

//按下后文本高亮

button->addTargetWithActionForControlEvents(this,cccontrol_selector(T12UI::buttonTouchDown));

button->addTargetWithActionForControlEvents(this,cccontrol_selector(T12UI::buttonTouchDown),CCControlEventTouchDown);

button->addTargetWithActionForControlEvents(this,cccontrol_selector(T12UI::buttonTouchDragInside),CCControlEventTouchDragInside);

响应的事件类型例如以下:

/** Kinds of possible events for the control objects. */

enum

{

CCControlEventTouchDown          
= 1 << 0,    // A touch-down event in the control.

CCControlEventTouchDragInside    
= 1 << 1,    // An event where a finger is dragged inside the bounds of the control.

CCControlEventTouchDragOutside   
= 1 << 2,    // An event where a finger is dragged just outside the bounds of the control.

CCControlEventTouchDragEnter     
= 1 << 3,    // An event where a finger is dragged into the bounds of the control.

CCControlEventTouchDragExit      
= 1 << 4,    // An event where a finger is dragged from within a control to outside its bounds.

CCControlEventTouchUpInside      
= 1 << 5,    // A touch-up event in the control where the finger is inside the bounds of the control.

CCControlEventTouchUpOutside     
= 1 << 6,    // A touch-up event in the control where the finger is outside the bounds of the control.

CCControlEventTouchCancel        
= 1 << 7,    // A system event canceling the current touches for the control.

CCControlEventValueChanged       
= 1 << 8      // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.

};

typedef
unsigned
int
CCControlEvent;

T12UI.h

#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();

void
touchDownCallBack(CCObject*
sender,
CCControlEvent
event);

void
touchDragInsideCallBack(CCObject*
sender,
CCControlEvent
event);

};

#endif

T12UI.cpp

#include
"T12UI.h"

#include
"AppMacros.h"

#include
"SimpleAudioEngine.h"

using
namespace
CocosDenshion;

CCScene *T12UI::scene()

{

CCScene *
scene =
CCScene::create();

T12UI *
layer =
T12UI::create();

scene->addChild(layer);

return
scene;

}

bool
T12UI::init()

{

TBack::init();

CCScale9Sprite *bgButton
= CCScale9Sprite::create("button.png");

CCScale9Sprite *bgButtonLighted
= CCScale9Sprite::create("buttonHighlighted.png");

CCLabelTTF *
text =
CCLabelTTF::create("Touch
Me",
"Couier New", 50);

CCControlButton *
button =
CCControlButton::create(text,
bgButton);

//为button加入位置

button->setPosition(ccp(winSize.width
/ 2, winSize.height
/ 2));

button->setBackgroundSpriteForState(bgButtonLighted,
CCControlStateHighlighted);

button->setTitleColorForState(ccRED,
CCControlStateHighlighted);

addChild(button);

//为button加入监听事件,加入的是button被点击的事件

button->addTargetWithActionForControlEvents(this,

cccontrol_selector(T12UI::touchDownCallBack),

CCControlEventTouchDown);

//为button加入监听事件,加入的是buttonDrag的事件

button->addTargetWithActionForControlEvents(this,

cccontrol_selector(T12UI::touchDragInsideCallBack),

CCControlEventTouchDragInside);

return
true;

}

void
T12UI::touchDownCallBack(CCObject*
sender,
CCControlEvent
event)

{

CCLog("touchDownCallBack");

}

void
T12UI::touchDragInsideCallBack(CCObject*
sender,
CCControlEvent
event)

{

CCLog("touchDragInsideCallBack");

}

执行结果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

版权声明:本文博客原创文章,博客,未经同意,不得转载。

1cocos2dx扩展UI控制,CCControlSlider,CCScale9Sprite(九妹图。),CCControlSwitch,CCControlButton的更多相关文章

  1. Android基础新手教程——1.6 .9(九妹)图片怎么玩

    Android基础新手教程--1.6 .9(九妹)图片怎么玩 标签(空格分隔): Android基础新手教程 1.本节引言: 可能有的一些疑问: 1.什么是.9图片? 答:图片后缀名前有.9的图片,如 ...

  2. UML总结4---UML九种图关系说明

    转自:http://blog.csdn.NET/chenyujing1234/article/details/8173519 UML中包括九种图:用例图.类图.对象图.状态图.时序图.协作图.活动图. ...

  3. UML 小结(6)- UML九种图的比较与学习

    UML中的九种图: 用例图.类图.对象图.状态图.时序图.协作图.活动图.部署图.构件图. 1)用例图(Use Case Diagram) 它是UML中最简单也是最复杂的一种图.说它简单是因为它采用了 ...

  4. UML总结---UML九种图关系说明

    UML中包括九种图:用例图.类图.对象图.状态图.时序图.协作图.活动图.组件图.配置图. 1)用例图(Use Case Diagram) 它是UML中最简单也是最复杂的一种图.说它简单是因为它采用了 ...

  5. UML九种图作用简介

    UML(统一建模语言):是面向对象的可视化建模语言. UML中有3种构造块:事物.关系和图,事物是对模型中最具有代表性的成分的抽象,关系是把事物结合在一起,图聚集了相关的事物 UML中有九种图如下: ...

  6. 转发-UI基础教程 – 原生App切图的那些事儿

    UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切 ...

  7. [UML]转:浅谈UML的概念和模型之UML九种图

    转自:http://blog.csdn.net/jiuqiyuliang/article/details/8552956 目录: UML的视图 UML的九种图 UML中类间的关系 上文我们介绍了,UM ...

  8. QT模态对话框用法(在UI文件中设置Widget背景图,这个图是一个带阴影边框的图片——酷)

    QT弹出模态对话框做法: 1.新建UI文件时,一定要选择基类是QDialog的,我的选择是:Dialog without Buttons(),如下图: 2.然后在使用的时候: MyDialog dlg ...

  9. 讨论UML概念和模型UML九种图。

    文件夹: UML的视图 UML的九种图 UML中类间的关系 上文我们介绍了,UML的视图.在每一种视图中都包括一个或多种图. 本文我们重点解说UML每种图的细节问题: 1.用例图(use case d ...

随机推荐

  1. Akka 简介与入门

    Akka 简介与入门 http://www.thinksaas.cn/group/topic/344095/ 参考官网  http://akka.io/ 开源代码  https://github.co ...

  2. Cocos2dx 3.0 过渡篇(二十五)死不了的贪食蛇(触摸版)

    上一篇写的贪食蛇的重力感应控制版,这一篇就讲下触摸控制版吧.额,也不知道写了那个贪食蛇教程究竟有没有获得沈老师的书,假设没有的话,看我不拿西瓜刀砍掉 偶尔E往事 的那啥! 重力版链接:http://b ...

  3. hive建表没使用LZO存储格式,可是数据是LZO格式时遇到的问题

    今天微博大数据平台发邮件来说.他们有一个hql执行失败.可是从gateway上面的日志看不出来是什么原因导致的,我帮忙看了一下.最后找到了问题的解决办法,下面是分析过程: 1.执行失败的hql: IN ...

  4. jQuery性能优化篇

    jQuery高级技巧——性能优化篇 阅读目录 通过CDN(Content Delivery Network)引入jQuery库 减少DOM操作 适当使用原生JS 选择器优化 缓存jQuery对象 定义 ...

  5. jsp 行动标签

    jsp行动标签 签.它影响JSP执行时的功能. 1.include动作标签 <jsp:include page="文件名称字"/> 或 <jsp:include  ...

  6. AspNet.WebAPI.OData.ODataPQ

    AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务 AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔) AspNet. ...

  7. FloatyFish下载量

    老师之前没有统一好一个平台,为了公平起见,我们选择了知名度比较高的CSDN,上次课上给老师说的下载量已成为过去,我们目前的下载量是: 这里还有我们最真实的用户体验,来自CSDN的用户,而非我们的朋友: ...

  8. Python判断内网IP

    def ip_into_int(ip): # 先把 192.168.1.13 变成16进制的 c0.a8.01.0d ,再去了"."后转成10进制的 3232235789 即可. ...

  9. mac os x10.11.2系统eclipse无法读取环境变量的问题

    eclipse调试Android自动化脚本的时候一直无法找到adb,遇到这么坑的问题,折腾死了,记录一下. mac os x10.11.2系统GUI程序(eclipse)无法读取~/.bash_pro ...

  10. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...