UI Buttons does not response to touches

Hi.

I’m getting pretty tired of UI Editor, but as I have windows and AMD, I can’t use CocosBuilder. Looks like nothing work as expected, and now, UIButtons are not an exception.

I have created a UIButton inside a Layout. Both have user interaction set to true. I added the widget to my current scene and now I can see the button, but when I try to press it, it does nothing. I have set a breakpoint to its press methods, and it is never reached, also added a SEL_TouchEvent, and the same.

What is happening now? What do I have to do to have it working?

Thanks…

Have you added addTouchEventListener and are you listening to the touch events?

touchEvent(Ref *pSender, Widget::TouchEventType type)

When using the UI Editor, touches are generated through events.

Please post your cocos2d-x version and your code.

I’m using cocos2dx 2.2.2

I don’t have any touchEvent function. I do have a HUD class, which is a CCLayer. In it, I added the buttons I read from the json of the UI Editor. After that, I added the callback of the button. And it is not working. The CCLayer receives touches and so on, but the button is never triggered.

In Player:
    hud->setJumpCallback((SEL_TouchEvent)&Player::jump);
In HUD:
_jumpButton = static_cast<UIButton*>(buttons->getChildByName("jumpButton"));
...
void HUD::setJumpCallback(SEL_TouchEvent callback)
{
	_jumpButton->addTouchEventListener(this, callback);
}

Even if the callback is not working, I put breakpoints everywhere inside the widget, and none of them are triggered. It is not receiving any touches.

Not related but, in UI Editor, we can’t specify a color to apply to the button image when it is pressed, right? We have to specify a different image for that state…

If you use UI Editor, you must mark up the “Touchable” checkbox.
After loading it into your scene, you should add extra callback to the button with addTouchEventListener.

I did both things and is not working.

I even put a breakpoint in the onTouchBegan of the UIWidget, which is the function that calls the callback, and it is never triggered. It’s not receiving any touches.

I don’t know if it’s because the widget is added to a CCLayer or what… i don’t know what to do.

EDIT:

This is how I’m reading the json in my cocos2dx 2.2.2:

Widget* widget = extension::GUIReader::shareReader()->widgetFromJsonFile("ui/hud/hud.json");

Do I have to call manually the onTouchBegan of the Widget? Because I can’t find where it is being called

It might be a case when the transparent widget is covering your button,For example If the button is “under” layout and the layout has touchable option enabled in cocoseditor then the event will be recieved by layout not button! Check the “Render Layer” values and touchable option in each node especially transparent ones since you dont see them “covering” your button and disable touchable where you dont need it

All the widgets have touchable set to true, and this contains the layouts. Also, I put a breakpoint inside the onTouchBegan of the UIWidget class, and it is never triggered. I am not even waiting for the UIButton to trigger, but any widget. They are not receiving any touches, but they are added to my CCLayer, so they are at least being shown. I think there has to be something related with the touches of the CCLayer. I never registered the Widget to receive touches or something like that.

I just solved a similar issue where I had a custom Widget with a Sprite in it, and no matter where I’d click the Sprite, the Widget’s onTouchEnded method override wouldn’t get called.

The issue was that I needed to set the content size (setContentSize) of the Widget to encompass the Sprite, otherwise it was a default 0,0 size meaning you couldn’t physically click on it. setTouchEnabled also needs to be true.

I don’t know if its exactly the same issue you ran into, but hopefully it helps you.