Cocos2dx v3.x c++ new ui button *solved*

Hi,

I am trying to use the new ui button in Cocos2dx v3.x (C++ visual studio 2013 on win7).

I have the code below, which works. When I click the button it closes the app,
but it does not trigger on touch end. As soon as the button is touched
it closes. If I hold the touch down it shouldn’t close until I let go.

How do I do this? Or how do I use this ui button properly ? Thanks

h

void touchEvent(cocos2d::Touch* touch, cocos2d::Event* event);

cpp

auto uButton = ui::Button::create();
        uButton->setTouchEnabled(true);
        //uButton->onTouchEnded(touch,event);
        uButton->loadTextures(“ButtonNormal.png”, “ButtonSelected1.png”, “”);
        uButton->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2) + Point(0, -50));
        uButton->addTouchEventListener(this, (ui::SEL_TouchEvent)&MenuScene::touchEvent);
        addChild(uButton);

cpp

void MenuScene::touchEvent(Touch *touch, Event *event)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

First of all, you are using a wrong signature of the callback function. I wonder why it even compiled. It shoud be void func(Ref*, Widget::TouchEventType).
In the callback you can check the type of the event. In your case you should close the app when the event has type TouchEventType::ENDED.

UPD. Ah, I see. You used a deprecated overload of addTouchEventListener method and that’s why your callback was accepted. So, you’re ok watching warnings during compilation? They are not accidental and you’d better pay attention to them.

Thanks dotsquid. Sorry, I am new to cocos and c++. Yes, I didn’t notice that warning. I think it is because I am getting alot of warnings that hide the important ones, so I will look at understanding and working through them.

Can you tell me what replaces the addTouchEventListener method since it is deprecated ?
I might have to download the latest cocos version as well (I think I am using 3.0).

Thanks so much for your replies.

void addTouchEventListener(ccWidgetTouchCallback callback);
typedef std::function<void(Ref*,Widget::TouchEventType)> ccWidgetTouchCallback;

Thanks dotsquid. I have figured it out now.

Solution was as follows:

  1. Download Cocos2dx v3.1.1 and setup Project to use updated SDK.

  2. Code changes:

     auto uButton = ui::Button::create();
    

    uButton->setTouchEnabled(true);
        uButton->loadTextures(“ButtonNormal.png”, “ButtonSelected1.png”, “”);
        uButton->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2) + Point(0, -50));
        uButton->addTouchEventListener(CC_CALLBACK_2(MenuScene::TouchEnd, this));
        addChild(uButton);

    void MenuScene::TouchEnd(Ref* pSender, ui::Widget::TouchEventType eEventType)
    {

    if (eEventType != ui::Widget::TouchEventType::ENDED) return;

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
        MessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”, “Alert”);
        return;
    #endif

    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        exit(0);
    #endif
    }