【问题标题】:Unable to call a method while paused in pause menu in cocos2dx在 cocos2dx 的暂停菜单中暂停时无法调用方法
【发布时间】:2017-05-13 03:25:56
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    MenuItem 没有Menu 不要闹。您正在将 moreButton 创建为 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);
    

    和里面的 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);
                 }
            });
    

    【讨论】:

      【解决方案2】:
          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);
      

      【讨论】:

      • 你应该把解释和代码示例分成两个不同的部分
      猜你喜欢
      • 1970-01-01
      • 2014-02-23
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多