【问题标题】:Unable to implement pause functionality in cocos2dxcocos2dx 无法实现暂停功能
【发布时间】:2017-05-10 16:51:49
【问题描述】:

我有一个类来处理和玩整个游戏。我尝试实现暂停和重新启动功能。但是,找不到正确的教程。 我想实现一个功能来在按下 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;
    }

【问题讨论】:

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


    【解决方案1】:

    您可以创建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);
    

    【讨论】:

    • 谢谢,您知道连接Layer和Directors isPaused的链接或示例吗?
    • 我不知道这样的链接,但您可以轻松创建自己的图层并可以使用暂停/恢复按钮进行模拟。
    • 当我在 if 条件中为 if paused 添加 ColorLayer 时,出现以下错误。 “错误:没有为这个 lambda 函数捕获‘this’”。错误指向 addChild(colorLayer);我已经添加了。
    • by reference [&] 你需要在 lambda 函数的捕获列表中使用引用,之后你可以在 lambda 函数中添加子对象,每次创建新对象(pauseLayer)和删除都不是很好的解决方案所以我在我的 pauseLayer 上使用 setVisible 方法,检查更新的答案
    • 谢谢你,完美的解决方案!
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多