COCOS2DX 3.2 实现模态对话框

来源:互联网 发布:windows教育版 编辑:程序博客网 时间:2024/06/05 10:20

模态对话框(Modal Dialogue Box,又叫做模态对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。

在cocos2dx 3.x版本中,触摸机制有了很多改变,可以参考这篇文章http://www.tuicool.com/articles/Rjim6z。

我的实现方法是,重新创建一个layer当做对话框,使用setSwallowTouches(true)来屏蔽下面的layer的触摸。创建layer时使用LayerColor::initWithColor(Color4B(25,25,25,125));使背景变暗,auto action = ScaleTo::create(0.1, 1);实现对话框缩放动画。具体代码:

.CPP

#include "EndLayer.h"
USING_NS_CC;
Scene* EndLayer::createScene(){
auto s = Scene::create();
auto l = EndLayer::create();
s->addChild(l);
return s;
}
bool EndLayer::init()
{
LayerColor::initWithColor(Color4B(25,25,25,125));
auto visibleSize = Director::getInstance()->getVisibleSize();
auto sp = Sprite::create("closeButton.png");
sp->setPosition(visibleSize.width / 2, visibleSize.height/2);
sp->setScale(0.3);
auto action = ScaleTo::create(0.1, 1);
sp->runAction(action);
//auto menuItem1 = MenuItemFont::create("Yes", CC_CALLBACK_1(EndLayer::yesCallBack,this));
//auto menuItem2 = MenuItemFont::create("No", CC_CALLBACK_1(EndLayer::noCallBack,this));
auto menuItem1 = MenuItemImage::create("close_yes.png", "close_yes.png", CC_CALLBACK_1(EndLayer::yesCallBack, this));
auto menuItem2 = MenuItemImage::create("close_no.png", "close_no.png", CC_CALLBACK_1(EndLayer::noCallBack, this));
auto menu = Menu::create(menuItem1, menuItem2, NULL);
menu->alignItemsHorizontallyWithPadding(50);
//menu->setPosition(visibleSize.width / 2, visibleSize.height / 2 - 300);
//menu->setPosition(visibleSize.width / 2, 35);
menu->setPosition(sp->getContentSize().width/2, sp->getContentSize().height*0.2);
sp->addChild(menu);
addChild(sp);
//ÆÁ±Îϲ㴥Ãþ
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [=](Touch* t,Event* e){

return true;
};
listener->setSwallowTouches(true);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
void EndLayer::yesCallBack(Ref* r)
{
Director::getInstance()->end();
}
void EndLayer::noCallBack(Ref* r)
{
this->removeFromParent();
}

在游戏的其他回调函数中如下调用即可:

//退出游戏按钮回调函数
void GameStart::menuCloseCallback(Ref* pSender)
{
auto endLayer = EndLayer::createScene();
this->addChild(endLayer);
}

0 0
原创粉丝点击