Button not responding to touches!

Hello!

I have a problem with a start button in my game. I’m using cocos studio 2, created a scene there, put the button and everything looks fine however the button is not responding to any touches.

My code looks like this:

bool MainScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto rootNode = CSLoader::createNode( "MainScene.csb" );
    rootNode->setName("root");
    rootNode->setContentSize(size);

    addChild(rootNode);
    
    setStartButton();

    return true;
}

setting up the button:

void MainScene::setStartButton()
{
    startButton = (Button*)this->getChildByName("root")->getChildByName( "startButton" );
    startButton->addTouchEventListener( CC_CALLBACK_2( MainScene::touchEventStartButton, this ) );
}

and the touch event method:

void MainScene::touchEventStartButton(Ref *pSender, Widget::TouchEventType type)
{
    switch (type)
    {
        case Widget::TouchEventType::BEGAN:
            // TODO
            break;
        case Widget::TouchEventType::ENDED:
            // TODO
            break;
        default:
            // TODO
            break;
    }
}

What am I missing guys? Do I need to initialize something in the init method? Or set something in cocos studio? Help! :open_mouth:

Have you checked the “Touchable” checkbox for the button in Cocos Studio?

And do you really want to recognize touches on the button, or do you actually want clicks? In the latter case you can use “addClickEventListener” instead of “addTouchEventListener”.

1 Like

@Ghaleon
I already fixed the problem… It was really stupid from my side… I forgot about the

Layer::onEnter(); 

In onEnter() method :frowning:

Btw can You tell me whats the difference between the Click and Touch event? Whats better for a “Start Button” in the menu?