Cocos2d-x中按钮类CCControlButton的使用

//【一】:创建:


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张图片,已经给大家准备好了。


[gl.paea.cn-CCControlButton-button1.png]


[gl.paea.cn-CCControlButton-button2.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);


[gl.paea.cn-CCControlButton5.png]




Controlbutton.cpp文件


1.引用命名空间“cocos2d::extension”。using namespace cocos2d::extension;

2.引用一下CocosDenshion命名空间 using namespace CocosDenshion;

3.加载我们的2张图片。

4.开始写码


//--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--//


[gl.paea.cn-CCControlButton4.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());

}


[gl.paea.cn-CCControlButton3.png]


       好了,我们运行一下看看。

[gl.paea.cn-CCControlButton2.png]

       OK,成功了

【转载】


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值