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

时间:2017-05-13 03:25:57

标签: android c++ cocos2d-x cocos2d-x-3.0

我有以下代码,我想在暂停菜单中单击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);

2 个答案:

答案 0 :(得分:1)

没有MenuItem

Menu不会制作场景。您正在创建更多按钮作为MenuItem,并且您将作为子图层添加到图层。

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

根据您的代码,每次单击暂停时都不会创建pauseLayer,因此每次触摸暂停时都不要创建moreButton,只需创建一次并添加到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);

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

答案 1 :(得分:0)

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