cocos2dx-CCControlButton
2014-07-03 12:01
399 查看
声 明 本教程仅用于初学cocos2dx同学使用,内容由本人(孤狼)学习过程中笔记编写,本教程使用cocos2dx版本为2.1.4。本教程内容可以自由转载,但必须同时附带本声明,或注明出处。 gl.paea.cn版权所有。 |
![](http://img.baidu.com/hi/jx2/j_0058.gif)
我们新建一个项目名字叫Controlbutton。
这里我要说一下,有不少同学是直接把我的代码里的.h和.cpp文件拷贝进他的项目,结果运行报错,这里要和大家说一个下,每个类的最上面
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
![](http://gl.paea.cn/upload/images/8131373540568.png)
这里是定义了一个常量。目的就是为了区分每个不同的类。如果发现常量已经被定义过就跳出,我的代码中是没有helloworld.h这个文件的,如果你直接把我的代码加入项目,记得修改这里。或者和我一样删除自带的helloworld项目。
![](http://img.baidu.com/hi/jx2/j_0063.gif)
【一】:创建:
1.方法:
(1):CCControlButton::create("CCScale9Sprite背景对象");
(2):CCControlButton::create("CCLabelTTF对象","CCScale9Sprite背景对象");
【二】:函数:
//当按钮响应对应事件后的CCScale9Sprite对象
setBackgroundSpriteForState("新的CCScale9Sprite对象","CCControlState状态变量");
//当按钮响应对应事件后的文字颜色
setTitleColorForState("新的ccColor3B颜色","CCControlState状态变量");
//当按钮响应对应事件后的文本
setTitleForState("新的CCString对象","CCControlState状态变量");
CCControlState状态变量如下(就是说处于这个状态才调用):
CCControlStateDisabled //禁用
CCControlStateHighlighted //高亮
CCControlStateNormal //正常
CCControlStateSelected //被XX过后 (感谢 北京|殁如雪 同学的解释)
CCControlState事件如下:
CCControlEvenTouchDown //按下
CCControlEvenTouchDragInside //在其内部拖动
CCControlEvenTouchDragOutside //在其外部拖动
CCControlEvenTouchDragEnter //拖动进入其内部
CCControlEvenTouchDragExit //拖动离开其内部
CCControlEvenTouchUpinside //在其内部抬起
CCControlEvenTouchUpOutside //在其外部抬起
CCControlEvenTouchCancel //取消所有触点
【三】:示例:
首先呢,为了做出效果,我们要准备2张图片,已经给大家准备好了。
![](http://gl.paea.cn/upload/images/9551373540568.png)
![](http://gl.paea.cn/upload/images/66421373540568.png)
Controlbutton.h文件
1.包含“cocos-ext.h”文件。#include "cocos-ext.h"
2.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;
3.设置4个回调函数,一会儿给两个按钮实现不同效果
void downaction1(CCObject * sender,CCControlEvent);
void upinsideaction1(CCObject * sender,CCControlEvent);
void downaction2(CCObject * sender,CCControlEvent);
void upinsideaction2(CCObject * sender,CCControlEvent);
![](http://gl.paea.cn/upload/images/53641373540568.png)
Controlbutton.cpp文件
1.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;
2.引用一下CocosDenshion命名空间 using namespace CocosDenshion;
3.加载我们的2张图片。
4.开始写码
![](http://img.baidu.com/hi/tsj/t_0039.gif)
//--new--//
CCSize mysize=CCDirector::sharedDirector()->getWinSize();
//新建三个c9对象
//这里要说一下为啥创建两个一样的c9对象c91,c93因为后面要用
//他们分别创建2个对象,不能同指针不然会报错哦。
CCScale9Sprite * c91=CCScale9Sprite::create("button1.png");
CCScale9Sprite * c92=CCScale9Sprite::create("button2.png");
CCScale9Sprite * c93=CCScale9Sprite::create("button1.png");
//新建2个ttf对象
CCLabelTTF * ttf=CCLabelTTF::create("hello","Arial",20);
CCLabelTTF * statettf=CCLabelTTF::create("no object","Arial",20);
//用2种方法新建两个CCControlButton对象
CCControlButton * mybutton1=CCControlButton::create(c91);
CCControlButton * mybutton2=CCControlButton::create(ttf,c93);
//设置位置
mybutton1->setPosition(ccp(mysize.width/2-100,mysize.height/2));
mybutton2->setPosition(ccp(mysize.width/2+100,mysize.height/2));
//设置大小
mybutton1->setPreferredSize(CCSizeMake(80,30));
mybutton2->setPreferredSize(CCSizeMake(80,30));
//设置按下事件
//这里的意思是mybutton1处于CCControlStateHighlighted高亮状态调用c92。
mybutton1->setBackgroundSpriteForState(c92,CCControlStateHighlighted);
mybutton2->setTitleColorForState(ccc3(120,120,120),CCControlStateHighlighted);
mybutton2->setTitleForState(CCString::create("hi"),CCControlStateHighlighted);
//设置按下回调事件
mybutton1->addTargetWithActionForControlEvents(
this,
cccontrol_selector(Controlbutton::downaction1),
CCControlEventTouchDown
);
mybutton1->addTargetWithActionForControlEvents(
this,
cccontrol_selector(Controlbutton::upinsideaction1),
CCControlEventTouchUpInside
);
mybutton2->addTargetWithActionForControlEvents(
this,
cccontrol_selector(Controlbutton::downaction2),
CCControlEventTouchDown
);
mybutton2->addTargetWithActionForControlEvents(
this,
cccontrol_selector(Controlbutton::upinsideaction2),
CCControlEventTouchUpInside
);
//设置状态显示
statettf->setPosition(ccp(mysize.width/2,mysize.height/2-100));
//加载
this->addChild(statettf,0,521);
this->addChild(mybutton1);
this->addChild(mybutton2);
//--new--//
![](http://gl.paea.cn/upload/images/20130711/13735406755590.jpg)
然后我们把4个回调函数写好
void Controlbutton::downaction1(CCObject * sender,CCControlEvent){
CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);
statettf->setString(CCString::createWithFormat("button1 down")->getCString());
}
void Controlbutton::downaction2(CCObject * sender,CCControlEvent){
CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);
statettf->setString(CCString::createWithFormat("button2 down")->getCString());
}
void Controlbutton::upinsideaction1(CCObject * sender,CCControlEvent){
CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);
statettf->setString(CCString::createWithFormat("button1 up")->getCString());
}
void Controlbutton::upinsideaction2(CCObject * sender,CCControlEvent){
CCLabelTTF * statettf=(CCLabelTTF*)this->getChildByTag(521);
statettf->setString(CCString::createWithFormat("button2 up")->getCString());
}
![](http://gl.paea.cn/upload/images/20130711/13735406886190.png)
好了,我们运行一下看看。
![](http://gl.paea.cn/upload/images/20130711/13735406414511.png)
OK,成功了
![](http://img.baidu.com/hi/tsj/t_0037.gif)
本节DEMO下载
上一篇:和屌丝一起学cocos2dx-CCControlSlider
下一篇:和屌丝一起学cocos2dx-获取设备语言
相关文章推荐
- vs2012 cocos2dx中CCControlButton绑定点击事件无回调
- cocos2dx基础篇(13)——按钮控件CCControlButton
- 【Cocos2dx】使用CCControlButton创建按钮、按钮点击事件,点击事件中的组件获取,setPosition的坐标问题
- cocos2dx----按钮控件CCControlButton
- 和屌丝一起学cocos2dx-CCControlButton
- 1cocos2dx扩展库UI控件,CCControlSlider,CCScale9Sprite(九妹图),CCControlSwitch,CCControlButton
- cocos2dX UI控件之CCControlButton
- cocos2dx CCControlButton 按钮事件
- cocos2dx CCControlButton 按钮事件
- 12.解决CCScale9Sprite或者CCControlButton无法使用的问题。
- Cocos2d-x_CCControlButton(按钮类)介绍
- CCControlButton 按钮
- Cocos2d-x学习笔记(12)(CCControlSwitch开关、CCControlSlider滑动条、CCControlButtonbutton)
- cocos2dx CCContrulButton不能适应图片大小的问题
- cocos2d-x按钮CCControlButton的用法
- cocos2dX UI控件之CCControlSwitch
- cocos2dX UI控件之CCControlSlider
- cocos2dx基础篇(10)——开关按钮CCControlSwitch
- CCControlButton的用法
- cocos2d-x 3.3 按钮添加和事件(CCControlButton)