CCControlButton and CCTouchesxxxxx

CCControlButton and CCTouchesxxxxx
0.0 0



i am trying to get a control-button working with a touches-method.
I am using:
cocos2dx 2.1.2
android target 4.4.2

In init-method of my testprogram i initialized a button this way:

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    cocos2d::extension::CCScale9Sprite* buttonUnsSprite = cocos2d
    cocos2d::extension::CCScale9Sprite* buttonSelSprite = cocos2d
    titleButton = CCLabelTTF::create("* Hello World!!!!!!!!! *", "Thonburi", 30);
    titleButton->setColor(ccc3(159, 168, 176));
    button = cocos2d::extension::CCControlButton::create(titleButton, buttonUnsSprite);
            , cocos2d::extension::CCControlStateHighlighted);
            , cocos2d::extension::CCControlStateHighlighted);
    button->setPosition(ccp (winSize.width / 2, 150));

Then in the implemented method ccTouchesEnded, i log all screen-touches:

void HelloWorld::ccTouchesEnded(cocos2d::CCSet* touches,
        cocos2d::CCEvent* event)
    for (CCSetIterator it = touches->begin()
            ; it != touches->end()
            ; ++it) {
        CCTouch* touch = (CCTouch*)*it;
        CCPoint touchPos = touch->getLocation();
        if (titleButton->boundingBox()
                .containsPoint(touchPos)) {
            CCLog("button touched!");
        } else {
            CCLog("touched pos. %f %f", touchPos.x, touchPos.y);

Sadly, if i touch inside the button, there is no corresponding message (“button touched”) appearing?!
I only notice the “touched pos…” issue if i click outside the button.
Is it actually possible to use ccTouchesXxxxx with a CCControlButton at all?

Thank you for any helping comments,


Have you checked if boundingBox() method returns values you expect? Such “misunderstandings” with cocos, gave me a couple headaches too.
Secondly : if you want custom touch behavior for your button only, it would be better to subclass it, and reimplement the corresponding methods.


I ran in to a similar issue (subclassing CCControlButton) and found that there are two separate sets of “touch” calls, plural and singular, and CCControlButton uses the singular version:

    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);

Try using those instead and see if that helps.


Thanks to Pawel and Justin - you are both right and led me to the solution:
Of course i have to overwrite one of the ccTouchxxxx_ methods in a derived button-class if i want to react to a touched
button ).
In my code, i looked for a touch of the button in the overwritten method of the
CCLayer_ class, which is wrong -
every item seems to receive touch-messages seperately and so must implement ccTouchxxx to capture such messages from the system.
But there is also another simple possibility to ask for a touched button without subclassing: button->isPushed_ which returns a boolean
and must be called within the
update_ method of the layer that the button belongs to.