gpt4 book ai didi

android - cocos2dx无法实现暂停功能

转载 作者:行者123 更新时间:2023-11-27 23:54:09 24 4
gpt4 key购买 nike

我有一个处理和播放整个游戏的类。我尝试实现暂停和重启功能。但是,找不到合适的教程。我想实现一个在按下 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);

关于android - cocos2dx无法实现暂停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43898518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com