Cocos2dx下,自己封装的一个按钮
typedef void (CCObject::*ButtonClick_Func)(); typedef void (CCObject::*ButtonClick_FuncN)(CCObject* obj); typedef void (CCObject::*ButtonClick_FuncND)(CCObject* obj,void* param); #define click_selector(_SELECTOR) (ButtonClick_Func)(&_SELECTOR) #define click_selectorN(_SELECTOR) (ButtonClick_FuncN)(&_SELECTOR) #define click_selectorND(_SELECTOR) (ButtonClick_FuncND)(&_SELECTOR)
// // ButtonSprite.h // Zuma_V4 // // Created by user on 13-1-28. // // #ifndef __Zuma_V4__ButtonSprite__ #define __Zuma_V4__ButtonSprite__ #include <iostream> #include "CallBack.h" #include "cocos2d.h" using namespace cocos2d; class ButtonSprite : public CCSprite,public CCTouchDelegate { // touch 事件优先级 int m_touchPriority; bool m_isDown; // update 2013-02-17 // click handle ButtonClick_Func m_BtnClick_Func; // int param; public: ButtonSprite(); virtual ~ButtonSprite(); // // update 2013-02-17 // 调整按钮的默认优先级,设置为最高优先级 static ButtonSprite* createButtonSprite(const char* fileName,int tPriority = kCCMenuHandlerPriority); static ButtonSprite* createButtonSpriteWithSpriteFrame(CCSpriteFrame *pSpriteFrame,int tPriority = kCCMenuHandlerPriority); /* 触摸相关 */ virtual void onEnter(); virtual void onExit(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); // optional virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); //update 2013-02-17 void clicked(); void setOnClickListener(ButtonClick_Func tBtnClickFunc); }; #endif /* defined(__Zuma_V4__ButtonSprite__) */
// // ButtonSprite.cpp // Zuma_V4 // // Created by user on 13-1-28. // // #include "ButtonSprite.h" /********************************************** 构造函数和析构函数 **********************************************/ ButtonSprite::ButtonSprite():m_isDown(false) { m_touchPriority = 0; m_BtnClick_Func = 0; } ButtonSprite::~ButtonSprite(){}; void ButtonSprite::onEnter(){ CCSprite::onEnter(); CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,m_touchPriority, true); }; void ButtonSprite::onExit(){ CCSprite::onExit(); CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); }; bool ButtonSprite::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){ CCPoint touchPoint = getParent()->convertTouchToNodeSpace(pTouch); if (boundingBox().containsPoint(touchPoint) && !m_isDown) { m_isDown = true; return true; } return false; }; // optional void ButtonSprite::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){ CCPoint touchPoint = getParent()->convertTouchToNodeSpace(pTouch); float deltaX = pTouch->getDelta().x; deltaX = deltaX > 0 ? deltaX : -deltaX; if (!boundingBox().containsPoint(touchPoint) || !m_isDown || deltaX > 15) { m_isDown = false; return; } }; void ButtonSprite::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){ CCPoint touchPoint = getParent()->convertTouchToNodeSpace(pTouch); if (boundingBox().containsPoint(touchPoint) && m_isDown) { /* 相当于产生了click事件 */ if (m_BtnClick_Func) { //来个点击效果 CCActionInterval* sAction = CCScaleTo::create(0.1, 1.2); CCActionInterval* sAction2 = CCScaleTo::create(0.1, 1); CCCallFunc* callFun = CCCallFunc::create(this,callfunc_selector(ButtonSprite::clicked)); runAction(CCSequence::create(sAction,sAction2,callFun,NULL)); } m_isDown = false; } }; void ButtonSprite::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent){ m_isDown = false; }; ButtonSprite* ButtonSprite::createButtonSprite(const char* fileName,int tPriority) { ButtonSprite* button = new ButtonSprite(); button->m_touchPriority = tPriority; if (button && button->initWithFile(fileName)) { button->autorelease(); return button; } CC_SAFE_DELETE(button); return button; } ButtonSprite* ButtonSprite::createButtonSpriteWithSpriteFrame(CCSpriteFrame *pSpriteFrame,int tPriority) { ButtonSprite *pobSprite = new ButtonSprite(); pobSprite->m_touchPriority = tPriority; if (pSpriteFrame && pobSprite && pobSprite->initWithSpriteFrame(pSpriteFrame)) { pobSprite->autorelease(); return pobSprite; } CC_SAFE_DELETE(pobSprite); return NULL; } // // update void ButtonSprite::setOnClickListener(ButtonClick_Func tBtnClickFunc) { m_BtnClick_Func = tBtnClickFunc; } void ButtonSprite::clicked() { (this->*m_BtnClick_Func)(); }
void MainMenu::onEnter() { CCLayer::onEnter(); ButtonSprite* playBtn = ButtonSprite::createButtonSprite("play.png"); playBtn->setPosition(ccp(240, 400)); playBtn->setOnClickListener(click_selector(MainMenu::btnCall)); addChild(playBtn); } void MainMenu::btnCall() { CCLog("[MainMenu::btnCall()]"); }