androidc++cocos2d-xcocos2d-x-3.0

Unable to call a method while paused in pause menu in cocos2dx


I have the following code and I want to call a function when the moreButton is clicked in the pause menu. But I'm unable to call the method even though the button is pressed, maybe since the entire game is paused. Am I missing something? I would love to hear from you!

auto pauseLayer=LayerColor::create(Color4B::BLACK, WINSIZE.width, WINSIZE.height);
auto pauseLabel=Label::createWithTTF("Paused", "fonts/Marker Felt.ttf", 24);
pauseLabel->setPosition(WINSIZE.width / 2.0, WINSIZE.height/ 2.0);
pauseLayer->addChild(pauseLabel);
// Add your required content to pauseLayer like pauseLabel

pauseLayer->setVisible(false);
pauseLayer->setOpacity(220);  // so that gameplay is slightly visible
addChild(pauseLayer, ZOrder::Level);

auto pauseButton = MenuItemImage::create("CloseNormal.png","CloseSelected.png",[pauseLayer](Ref*sender){
            if(!Director::getInstance()->isPaused()) {
                        Director::getInstance()->pause();
                        pauseLayer->setVisible(true);
                        auto moreButton = MenuItemImage::create("more.png","more.png","more.png",[](Ref*sender){
                                    std::string url = "https://play.google.com/store/xxx";
                                    cocos2d::Application::getInstance()->openURL(url);
                                    });
                                    moreButton->setPosition(WINSIZE.width / 2.0, WINSIZE.height/ 2.0+50);
                                    pauseLayer->addChild(moreButton, ZOrder::Level);
                            pauseLayer->addChild(moreButton);
                   } else {
                        Director::getInstance()->resume();

                        pauseLayer->setVisible(false);
                   }
        });

auto menu = Menu::create(pauseButton, NULL);
menu->setPosition(WINSIZE.width / 2.0, WINSIZE.height - 50);
addChild(menu, ZOrder::Level);

Solution

  • MenuItem without Menu don't make scene. You're creating moreButton as MenuItem and you're adding as child to layer.

    Why don't you try Button for you're requirement.

    According to your code you're not creating pauseLayer each time when you click pause so don't make moreButton each time when you touch pause, Make it once and add to pauseLayer so that each time you only have to change the visibility of that layer.

    // Add your required content to pauseLayer like pauseLabel
    
    auto moreButton = cocos2d::ui::Button::create("more.png");
    moreButton->setPosition(Vec2(100,100));
    moreButton->addClickEventListener([](Ref*){
            std::string url = "https://play.google.com/store/xxx";
            cocos2d::Application::getInstance()->openURL(url);
        });
    
    pauseLayer->addChild(moreButton);
    

    And Inside lambda function remove moreButton code :

    auto pauseButton = MenuItemImage::create("CloseNormal.png","CloseSelected.png",[pauseLayer](Ref*sender){
                if(!Director::getInstance()->isPaused()) {
                       Director::getInstance()->pause();
                       pauseLayer->setVisible(true);
    
                 } else {
                       Director::getInstance()->resume();
                       pauseLayer->setVisible(false);
                 }
            });