cocos2d-x自定义按钮类

前端之家收集整理的这篇文章主要介绍了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,png_width,png_height); //图片的大小 CCRect rectInsets = CCRectMake(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);

猜你在找的Cocos2d-x相关文章