cocos2dx CCControlButton 按钮事件

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    CCLabelTTF * label = CCLabelTTF::create("为选中文字", "MarkerFelt",25);

    CCControlButton * button = CCControlButton ::create(label,CCScale9Sprite::create("button.png"));

    button->setPosition(ccp(240, 170));

//    按钮选中后背景图响应的状态

    button->setTitleColorForState(ccc3(255, 0, 0), CCControlStateHighlighted);

//    按钮选中后文字响应的状态

    button->setTitleForState(CCString::create("选中文字"), CCControlStateHighlighted);

    

    addChild(button);

 

    //    按下按钮事件回调

    button->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);

    

    //    按钮在其内部拾起事件回调

    button->addTargetWithActionForControlEvents(this

                                                , cccontrol_selector( HelloWorld::touchUpInsideAction), CCControlEventTouchDragEnter);

   //    按钮在其外部拾起事件回调

    button->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchUpOutsideAction), CCControlEventTouchDragOutside);

    

//    用于显示按钮的状态

    CCLabelTTF * la = CCLabelTTF ::create(" ", "MarkerFelt",20);

    la->setColor(ccc3(255, 0, 0));

    la->setPosition(ccp(240, 220));

    addChild(la,0,923);



    return true;

}



//    按下按钮事件回调

void HelloWorld:: touchDownAction(CCObject * sender , CCControlEvent * controlEvent)

{

    CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);

    label->setString(CCString::createWithFormat("按下")->getCString());



}

//    按钮在其内部抬起事件回调

void HelloWorld::touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent)

{

    CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);

    label->setString(CCString::createWithFormat("内部抬起")->getCString());

}

//    按钮在其外部抬起事件回调

void HelloWorld::touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent)

{

    CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);

    label->setString(CCString::createWithFormat("外部抬起")->getCString());



}

。cpp要声明的

 

 

#include "cocos2d.h"

#include "cocos-ext.h"



using namespace cocos2d;

using namespace extension;



class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();



    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();

    

    // a selector callback

    void menuCloseCallback(CCObject* pSender);



    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

    

//    按下按钮事件回调

    void touchDownAction(CCObject * sender , CCControlEvent * controlEvent);

//    按钮在其内部拾起事件回调

    void touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent);

//    按钮在其外部拾起事件回调

        void touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent);

};

 

你可能感兴趣的:(cocos2dx)