cocos2dx的CCControlButton事件

cocos2dx的CCControlButton事件
(基于cocos2dx2.2.2)cocos2dx的按钮类定义在extensions\GUI\CCControlExtension\CCControlButton.h
在需要用的时候,包括就可以了。
我们在相应的申明的地方,包含下面三个头文件

#include "GUI/CCControlExtension/CCControlButton.h"
#include "GUI/CCControlExtension/CCScale9Sprite.h"
#include "GUI/CCControlExtension/CCControl.h"
然按钮的事件有如下定义
enum 
{
    CCControlEventTouchDown           = 1 << 0,     //  A touch-down event in the control.
    CCControlEventTouchDragInside     = 1 << 1,     //  An event where a finger is dragged inside the bounds of the control.
    CCControlEventTouchDragOutside    = 1 << 2,     //  An event where a finger is dragged just outside the bounds of the control. 
    CCControlEventTouchDragEnter      = 1 << 3,     //  An event where a finger is dragged into the bounds of the control.
    CCControlEventTouchDragExit       = 1 << 4,     //  An event where a finger is dragged from within a control to outside its bounds.
    CCControlEventTouchUpInside       = 1 << 5,     //  A touch-up event in the control where the finger is inside the bounds of the control. 
    CCControlEventTouchUpOutside      = 1 << 6,     //  A touch-up event in the control where the finger is outside the bounds of the control.
    CCControlEventTouchCancel         = 1 << 7,     //  A system event canceling the current touches for the control.
    CCControlEventValueChanged        = 1 << 8       //  A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
};
事件原型定义:typedef void (CCObject::*SEL_CCControlHandler)(CCObject*, CCControlEvent); 并用宏cccontrol_selector定义了 #define cccontrol_selector(_SELECTOR) (SEL_CCControlHandler)(&_SELECTOR) 事件的两个参数分别的产生事件的对象,具体的事件,这是一个无符号32位整数,事件值就是CCControlEventTouchDown等定义的值。
在这里,定义了事件函数      void  OnTouchDown(CCObject *  pSender, extension::CCControlEvent  event );
     void OnTouchDragEnter(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchDragExit(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchDragInside(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchDragOutside(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchUpInside(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchUpOutside(CCObject* pSender, extension::CCControlEvent  event);
     void OnTouchCancel(CCObject* pSender, extension::CCControlEvent  event);
具体事件实现如下

void HelloWorld::OnTouchDown(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchDown:%u",  event);
}

void HelloWorld::OnTouchDragEnter(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchDragEnter:%u",  event);

}

void HelloWorld::OnTouchDragExit(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchDragExit:%u",  event);

}

void HelloWorld::OnTouchDragInside(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchDragInside:%u",  event);
}

void HelloWorld::OnTouchDragOutside(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchDragOutside:%u",  event);
}

void HelloWorld::OnTouchUpInside(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchUpInside:%u",  event);
}

void HelloWorld::OnTouchUpOutside(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchUpOutside:%u",  event);
}

void HelloWorld::OnTouchCancel(CCObject* pSender, extension::CCControlEvent  event)
{
    PrintLog("OnTouchCancel:%u",  event);
}
然后定义一个按钮,在init中实现 

            {
                extension::CCScale9Sprite * pBtnNormal = extension::CCScale9Sprite::create("up.png");
                extension::CCScale9Sprite * pBtnDown = extension::CCScale9Sprite::create("up.png");
                CCLabelTTF * pBtnCaption = CCLabelTTF::create("", "宋体", 30);
                extension::CCControlButton * pBtn = extension::CCControlButton::create(pBtnCaption, pBtnNormal);
                pBtn->setBackgroundSpriteForState(pBtnDown, extension::CCControlStateSelected);
                pBtn->setPosition(GetPosition(pPlayerLayer, "UpPoint"));
                pBtn->setAnchorPoint(CCPointZero);
                pBtn->setPreferredSize(CCSizeMake(32, 32));
                pBtn->setTag(100);
                 this->addChild(pBtn);

                pBtn->addTargetWithActionForControlEvents( this,    cccontrol_selector(HelloWorld::OnTouchDown), CCControlEventTouchDown);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchDragEnter), CCControlEventTouchDragEnter);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchDragExit), CCControlEventTouchDragExit);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchDragInside), CCControlEventTouchDragInside);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchDragOutside), CCControlEventTouchDragOutside);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchUpInside), CCControlEventTouchUpInside);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchUpOutside), CCControlEventTouchUpOutside);
                pBtn->addTargetWithActionForControlEvents( this, cccontrol_selector(HelloWorld::OnTouchCancel), CCControlEventTouchCancel);


                
            }
需要注意的事件,不要开启touch事件,否则会找不到。 也就是这句//CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
具体的按钮事件实验结果说明:
CCControlEventTouchDown:鼠标该处于按下并曾经点中按钮时,则触发一次 
CCControlEventTouchDragEnter:当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 ,也就是处于按下状态,回到该按钮区域
CCControlEventTouchDragExit:当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 ,也就是处于按下状态,离开该按钮区域
CCControlEventTouchDragInside:当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,每产生移动,就会触发 ,
CCControlEventTouchDragOutside: 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,每产生移动,就会触发 
CCControlEventTouchUpInside:当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次,也就是处于按下状态,并在该按钮区域放起
CCControlEventTouchUpOutside:当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 也就是处于按下状态,但是已经离开该按钮区域,再放起
CCControlEventTouchCancel:暂时没有发现能用鼠标触发这个事件的操作
了解后,就可以根据实际要求实现了

你可能感兴趣的:(cocos2dx的CCControlButton事件)