cocos2d-x中封装CCMenuItem等相关按钮,但是有些时候需要自己封装按钮,这样能够更加灵活的实现对应功能。

自定义Button,需要重写OnEnter()和onExit()函数,并在对应函数中添加注册和取消注册。

BaseButton.h内容

 #ifndef BaseButton_H_H
#define BaseButton_H_H #include "cocos2d.h"
using namespace cocos2d; typedef SEL_CallFunc v_callback; class BaseButton : public CCNodeRGBA, public CCTargetedTouchDelegate
{
public:
BaseButton() : sprite1(NULL), sprite2(NULL), curSprite(NULL), downCallback(NULL), moveCallback(NULL), upCallback(NULL) {}
static BaseButton* create(const char* pszFileName1, const char* pszFileName2, v_callback upCallback0);
virtual bool initWithFile(const char* pszFileName1, const char* pszFileName2, v_callback upCallback0); virtual void onEnter();
virtual void onExit(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
//virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); void downHand(); virtual void update(); private:
CCSprite *sprite1, *sprite2, *curSprite;
//vCallback downCallback, moveCallback, upCallback;
//void (*downCallback)(void);
void (*moveCallback)(void);
//void (*upCallback)(void);
v_callback downCallback;
v_callback upCallback;
}; #endif

BaseButton.cpp内容

 #include "BaseButton.h"

 BaseButton* BaseButton::create(const char* pszFileName1, const char* pszFileName2, v_callback upCallback0)
{
BaseButton *button = new BaseButton();
if(button && button->initWithFile(pszFileName1, pszFileName2, upCallback0)){
button->autorelease();
return button;
}
CC_SAFE_DELETE(button);
return NULL;
} bool BaseButton::initWithFile(const char* pszFileName1, const char* pszFileName2, v_callback upCallback0)
{
if( !CCNodeRGBA::init() ){
return false;
}
if(pszFileName1 == NULL){
return false;
} sprite1 = CCSprite::create( pszFileName1 );
CCSize size = sprite1->getContentSize();
if(pszFileName2 != NULL){
sprite2 = CCSprite::create( pszFileName2 );
}
else{
sprite2 = CCSprite::create( pszFileName1 );
sprite2->setColor( ccc3(, , ) );
sprite2->setOpacity( (GLubyte)(*0.7) );
}
sprite2->setVisible(false);
sprite2->setScaleX(size.width/sprite2->getContentSize().width);
sprite2->setScaleY(size.height/sprite2->getContentSize().height);
addChild(sprite1);
addChild(sprite2);
curSprite = sprite1; downCallback = (SEL_CallFunc)(&BaseButton::downHand);
upCallback = upCallback0; scheduleUpdate(); return true;
} void BaseButton::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, , true);
} void BaseButton::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
} bool BaseButton::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent)
{
//CCPoint point = pTouch->getLocation();
CCPoint point = this->convertTouchToNodeSpaceAR(pTouch);
if( sprite1->boundingBox().containsPoint(point) ){
if(downCallback != NULL){
(this->*downCallback)();
}
return true;
}
return false;
} void BaseButton::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
if(moveCallback != NULL){
moveCallback();
}
} void BaseButton::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
sprite2->setVisible(false);
sprite1->setVisible(true);
if(upCallback != NULL){
//upCallback();
(this->*upCallback)();
}
//CCMessageBox("Up", "Info");
} void BaseButton::downHand()
{
sprite1->setVisible(false);
sprite2->setVisible(true);
} void BaseButton::update()
{ }

MyScene.h内容

 #ifndef MyScene_H_H
#define MyScene_H_H #include "cocos2d.h"
using namespace cocos2d; class MyScene : public CCLayer
{
public:
static CCScene* createScene();
virtual bool init();
CREATE_FUNC( MyScene ); void upCallback(); private:
}; #endif

MyScene.cpp内容

 #include "MyScene.h"
#include "BaseButton.h" CCScene* MyScene::createScene()
{
CCScene *scene = CCScene::create();
MyScene *layer = MyScene::create();
scene->addChild(layer);
return scene;
}; bool MyScene::init()
{
if( !CCLayer::init() ){
return false;
} CCSize size = CCDirector::sharedDirector()->getWinSize(); BaseButton *button = BaseButton::create("pal_2.png", "pal_3.png", (v_callback)(&MyScene::upCallback1) );
button->setAnchorPoint(ccp(0.5, 0.5));
button->setPosition(, size.height/);
button->setScale(0.25f);
addChild(button); BaseButton *button2 = BaseButton::create("pal_2.png", "pal_3.png", (v_callback)(&MyScene::upCallback2) );
button2->setAnchorPoint(ccp(0.5, 0.5));
button2->setPosition(, size.height/);
button2->setScale(0.25f);
addChild(button2); return true;
} void MyScene::upCallback1()
{
CCMessageBox("Up1", "Info");
} void MyScene::upCallback2()
{
CCMessageBox("Up2", "Info");
}

运行结果:

cocos2d-x学习记录6——自定义Button-LMLPHP

这里的button响应函数使用的是成员函数指针,需要通过对象进行使用,如(this->*upCallback)()。

而普通的函数指针则需要是static函数,成员函数则会出现错误。

05-07 15:46