Button event listener not triggering

so I have setup a button such as:

        auto okbtn = ui::Button::create("btn.png", "btndown.png", "", ui::Widget::TextureResType::PLIST);
        okbtn->setTitleText("OK"); 
        okbtn->setTouchEnabled(true);
        okbtn->setScale9Enabled(true);
        okbtn->getTitleRenderer()->enableOutline(Color4B(0, 0, 0, 255), 1);
        okbtn->setCapInsetsNormalRenderer(Rect(7, 7, 33, 33));
        okbtn->setContentSize(Size(30, 15));
        okbtn->addTouchEventListener(CC_CALLBACK_2(MessageBox::caller, this));
       
        okbtn->setPosition(Vec2(width/2, 10));
        
        addChild(okbtn, 99);

This button shows up fine on my particular layer however the click event never seems to get called and the button does not animate during the ‘click’.

for reference my callback is this:

void MessageBox::caller(Ref *sender, ui::Widget::TouchEventType event)
{
       CCLOG("CLICKED!");
}

Any suggestions on what I am doing wrong would be great.

Try making it bigger, like 300x300. I had a similar problem and it was caused by the button just being too small to be touched.

ok, so i tried setting it to 300x300 to test. button is still unresponsive. going to try messing with it some more.

Are you adding the button to an actual Menu?

no. it’s being added to a layer. similar to how the GUItests do it.

see:

The only major difference is that my ‘layer’ is just a child of cocos2d::Layer

(adding setPressedActionEnabled(true) had no effect)

I don’t see an error in your code.

All three callbacks work fine:

button->addClickEventListener([this, data](Ref*)
{
    CCLOG("click works");
});

button->addTouchEventListener([this, data](Ref*, ui::Widget::TouchEventType)
{
    CCLOG("touch works");
});

button->addTouchEventListener(CC_CALLBACK_2(Board::caller, this));

inline void caller(Ref *sender, ui::Widget::TouchEventType event)
{
   CCLOG("selector works");
}

Try if all are failing.

Ya, so i created a new scene and tried adding this directly and it works fine. seems to be something directly related to the scene that is controlling that button. so digging into it more.

that particular scene was having issues with the event dispatcher and had to call Director::getInstance()->getEventDispatcher()->setEnabled(true); before using the asset manager, going to dig more there to see if that’s related.

Yeah, must be some code in there, which causes an issue with the dispatcher. Could also be a bug.

Is it possible to strip down your project and provide it?

Thanks everyone for the help.

I had overrode onEnter but did not call Layer::onEnter() in the new method. adding that fixed my problem.