我有一个处理和播放整个游戏的课程。我试图实现暂停和重启功能。但是,找不到合适的教程。 我想实现一个功能,当按下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;
}
答案 0 :(得分:3)
您可以使用不透明度创建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);