Search code examples
androidc++cocos2d-xcocos2d-x-3.0

Unable to implement pause functionality in cocos2dx


I have a class that handles and plays the entire game. I tried to implement the pause and restart functionality.However, cannot find the right tutorial. I want to implement a functionality to pause the game when the pauseButton is pressed. But I'm not sure where and how. How is a pause and restart or returning one scene back handled in cocos2dx? I would love to hear from you!

Part of my code. How to add a pause in the 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;
    }

Solution

  • You can create Layer or may be ColorLayer with some opacity and add to your current Scene/Layer. Add pause Dialog content like Pause Label, Resume Button on that layer and remove that layer when you resume.

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

    EDIT

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