thuli
January 19, 2016, 11:51pm
1
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.
cei
January 20, 2016, 12:14am
2
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.
thuli
January 20, 2016, 1:07am
3
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?
thuli
January 20, 2016, 1:26am
5
no. it’s being added to a layer. similar to how the GUItests do it.
see:
Button* button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
button->getContentSize().height);
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this));
button->setZoomScale(0.4f);
button->setPressedActionEnabled(true);
_uiLayer->addChild(button);
The only major difference is that my ‘layer’ is just a child of cocos2d::Layer
(adding setPressedActionEnabled(true) had no effect)
iQD
January 20, 2016, 1:36am
6
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.
thuli
January 20, 2016, 2:06am
7
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.
iQD
January 20, 2016, 2:32am
8
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?
thuli
January 20, 2016, 5:14am
9
Thanks everyone for the help.
I had overrode onEnter but did not call Layer::onEnter() in the new method. adding that fixed my problem.