cocos2dx无法实现暂停功能

Unable to implement pause functionality in cocos2dx

我有一个 class 可以处理和玩整个游戏。我尝试实现暂停和重新启动 functionality.However,找不到合适的教程。 我想实现一个在按下 pauseButton 时暂停游戏的功能。但我不确定在哪里以及如何。在 cocos2dx 中如何处理暂停和重新启动或返回一个场景?我很想听听你的意见!

我的部分代码。如何在 pauseButton 中添加暂停?

  Scene* GameLayer::createScene(int level)
    {
        auto scene = Scene::create();
        auto layer = GameLayer::create(level);
        scene->addChild(layer);
        return scene;
    }

    GameLayer* GameLayer::create(int level)
    {
        GameLayer *pRet = new GameLayer();
        pRet->init(level);
        pRet->autorelease();

        return pRet;
    }

    bool GameLayer::init(int level)
    {
        if (!Layer::init())
            return false;

        auto touchListener = EventListenerTouchOneByOne::create();
        touchListener->setSwallowTouches(_swallowsTouches);
        touchListener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this);
        touchListener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this);
        touchListener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this);
        touchListener->onTouchCancelled = CC_CALLBACK_2(GameLayer::onTouchCancelled, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

            auto pauseButton = MenuItemImage::create("pause1.png","pause2.png",[](Ref*sender){
    //Do pause and show the pause menu

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

您可以创建 Layer 或者 ColorLayer 并添加一些不透明度并添加到您当前的 Scene/Layer。在该层添加暂停对话框内容,如暂停标签、恢复按钮,并在恢复时删除该层。

auto pauseButton = MenuItemImage::create("pauseNormal.png","pauseSelected.png",[](Ref*sender){

        if(!Director::getInstance()->isPaused())
            Director::getInstance()->pause();
        else
            Director::getInstance()->resume();
    });

auto buttons = Menu::create(pauseButton,NULL);
addChild(buttons);
buttons->setPosition(visibleSize.width / 2.0, visibleSize.height / 2.0);

编辑

auto pauseLayer=LayerColor::create(Color4B::BLACK, visibleSize.width,visibleSize.height);

auto pauseLabel=Label::createWithTTF("Paused", "fonts/Marker Felt.ttf", 24);
pauseLabel->setPosition(origin.x+visibleSize.width/2,origin.y+visibleSize.height-50);
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);

auto pauseButton = MenuItemImage::create("pauseNormal.png","pauseSelected.png",[pauseLayer](Ref*sender){

    if(!Director::getInstance()->isPaused()){
       Director::getInstance()->pause();
       pauseLayer->setVisible(true);
    }
    else {
        Director::getInstance()->resume();
        pauseLayer->setVisible(false);
    }

});

auto buttons = Menu::create(pauseButton,NULL);
addChild(buttons);
buttons->setPosition(visibleSize.width / 2.0, visibleSize.height / 2.0);