在 cocos2dx 的暂停菜单中暂停时无法调用方法

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

我有以下代码,我想在暂停菜单中单击 moreButton 时调用一个函数。但是即使按下按钮我也无法调用该方法,可能是因为整个游戏都暂停了。我错过了什么吗?我很想听听你的消息!

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);

MenuItem没有Menu不要闹。您正在将 moreButton 创建为 MenuItem 并将其作为子项添加到层。

你为什么不试试 Button 来满足你的要求。

根据您的代码,您不会在每次单击暂停时都创建 pauseLayer,因此不要在每次触摸暂停时都创建更多按钮,创建一次并添加到 pauseLayer,这样每次您只需更改该图层的可见性。

// 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);

并在 lambda 函数中删除 moreButton 代码:

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);
             }
        });
    Problem is that you are using menuitem and adding it to Layer and also twice:-

       pauseLayer->addChild(moreButton, ZOrder::Level);
       pauseLayer->addChild(moreButton);

    This will throw exception of child already added.

    You just simply need to add moreButton on Menu.

    Following code should work:-

    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);

auto menu = Menu::create(moreButton, NULL);
pauseLayer->addChild(menu, ZOrder::Level);

                       } 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);