cocos2d-x自定义按钮类

#ifndef __ControlButton_H__
#define __ControlButton_H__
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
//用于标识当前按钮的状态
typedef enum{
touch_begin,
touch_down,
touch_up,
}tagForTouch;
class ControlButton :public CCNode
{
public:
ControlButton();
~ControlButton();
CREATE_FUNC(ControlButton);
//创建按钮,其中name_png为按钮的背景图片,button_title为按钮图片上要显示的文字,num为文字的透明度0-100,0为透明
void CreateButton(const char* name_png,const char* button_title="0",unsigned int num=0);
//绑写按钮事件
void BinonEven();

void touchDownAction(CCObject* pSender, CCControlEvent event);

void touchDragEnter(CCObject* pSender, CCControlEvent event);

void touchDragExit(CCObject* pSender, CCControlEvent event);

void touchDragInside(CCObject* pSender, CCControlEvent event);

void touchDragOutside(CCObject* pSender, CCControlEvent event);
void touchUpInside(CCObject* pSender, CCControlEvent event);

void touchUpOutside(CCObject* pSender, CCControlEvent event);

void touchCancel(CCObject* pSender, CCControlEvent event);
//是否按下按钮
bool isTouch;
private:
//按钮控件变量
CCControlButton* controlBtn;
};
#endif

/ControlButton.cpp文件
#include "ControlButton.h"
ControlButton::ControlButton():controlBtn(NULL),isTouch(false)
{
}
ControlButton::~ControlButton()
{

}
void ControlButton::CreateButton(const char* name_png,const char* button_title,unsigned int num)
{
//得到按钮图片的大小
CCScale9Sprite* btn = CCScale9Sprite::create(name_png);
CCLOG("%f",btn->getContentSize().width);
CCLOG("%f",btn->getContentSize().height);
int png_height=static_cast(btn->getContentSize().height);
int png_width=static_cast( btn->getContentSize().width);
btn->release();

//要显示的图片大小
CCRect rect = CCRectMake(0,0,png_width,png_height); //图片的大小
CCRect rectInsets = CCRectMake(1,1,1,1); //left,right,width,height

//按钮标题,Marker Felt为字体类型,png_height为字体高度
CCLabelTTF *title = CCLabelTTF::create(button_title, "Marker Felt",png_height-10);
title->setOpacity(num);//设置可见度

//正常状态下的按钮图片
CCScale9Sprite *btnNormal = CCScale9Sprite::create(name_png,rect,rectInsets);
//创建按钮
controlBtn = CCControlButton::create(title,btnNormal);
this->addChild(controlBtn);
//绑定事件
BinonEven();
}
void ControlButton::BinonEven()
{
if(!controlBtn)
return;
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDownAction),CCControlEventTouchDown);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragEnter),CCControlEventTouchDragEnter);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragExit),CCControlEventTouchDragExit);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragInside),CCControlEventTouchDragInside);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchDragOutside),CCControlEventTouchDragOutside);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchUpInside),CCControlEventTouchUpInside);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchUpOutside),CCControlEventTouchUpOutside);
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlButton::touchCancel),CCControlEventTouchCancel);
}

void ControlButton::touchDownAction(CCObject* pSender, CCControlEvent event)
{
isTouch=true;

}

void ControlButton::touchDragEnter(CCObject* pSender, CCControlEvent event)
{
}

void ControlButton::touchDragExit(CCObject* pSender, CCControlEvent event)
{
}

void ControlButton::touchDragInside(CCObject* pSender, CCControlEvent event)
{

}

void ControlButton::touchDragOutside(CCObject* pSender, CCControlEvent event)
{

}

void ControlButton::touchUpInside(CCObject* pSender, CCControlEvent event)
{
isTouch=false;

}


void ControlButton::touchUpOutside(CCObject* pSender, CCControlEvent event)
{

}

void ControlButton::touchCancel(CCObject* pSender, CCControlEvent event)
{

}

.cpp中保留了一些按钮的事件,如果后头有需要或者你有什么需要可以直接拿去改,十分方便。
使用方法:

在要用到的地方加头文件#include "ControlButton.h"
定义成员变量: ControlButton* btn;//按钮控件变量

//添加按钮
btn=ControlButton::create();
btn->CreateButton("bt.png");
btn->setPosition(ccp(visibleSize.width-50,50));

this->addChild(btn,2);

转载于:https://my.oschina.net/u/1585312/blog/997355

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值