{
m_DialogConfirm->addTouchEventListener(CC_CALLBACK_2(MomoScene::PressDialogConfirmBtn,this));
m_DialogConfirm->setPosition(Point(0,-100));
m_DialogConfirm->setVisible(true);
m_DialogCancel->setVisible(false);
}
//模态对话框需要做一个吞噬触摸层
if(isModalDialog == true)
{
auto listener1 = EventListenerTouchOneByOne::create();//创建一个触摸监听
listener1->setSwallowTouches(true);//设置不想向下传递触摸
listener1->onTouchBegan = [](Touch* touch, Event* event){
CCLOG("touch Swallow");
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,m_UI_Dialog);
}
}
void MomoScene::LoadDialogFinish(Armature *a, MovementEventType b, std::string c)
{
//动画完成
if(b == COMPLETE)
{
Dialog->getAnimation()->setMovementEventCallFunc(nullptr);
m_DialogMsg->setVisible(true);
}
}
//隐藏对话框
void MomoScene::HideDialog()
{
m_DialogMsg->setVisible(false);
m_DialogConfirm->setVisible(false);
m_DialogCancel->setVisible(false);
//移除触摸屏蔽
_eventDispatcher->removeEventListenersForTarget(m_UI_Dialog);
Dialog->getAnimation()->playWithIndex(1);
Dialog->getAnimation()
->setMovementEventCallFunc(CC_CALLBACK_3(MomoScene::HideDialogFinish,this));
}
//隐藏对话框完成
void MomoScene::HideDialogFinish(Armature *a, MovementEventType b, std::string c)
{
//动画完成
if(b == COMPLETE)
{
Dialog->getAnimation()->setMovementEventCallFunc(nullptr);
a->setVisible(false);
a->setOpacity(255);
}
}
//对话框取消按钮
void MomoScene::PressDialogCancelBtn(Ref *pSender, Widget::TouchEventType type)
{
switch(type)
{
case Widget::TouchEventType::ENDED:
HideDialog();
break;
}
}
//对话框确认按钮
void MomoScene::PressDialogConfirmBtn(Ref *pSender, Widget::TouchEventType type)
{
switch(type)
{
case Widget::TouchEventType::ENDED:
HideDialog();
if(m_Dialog_ConfirmCallback != nullptr)
m_Dialog_ConfirmCallback(NULL);
break;
}
}
值得一提的是 所有以m_开头的变量全部是类的成员变量,下面也给出这些变量的定义,其中有几个变量在类的构造函数中有初始化,也给出来
//标志变量
public:
bool m_IsNeedExit;
//UI变量
public:
Sprite* m_spr_ExitTip; //退出提示
Armature* Dialog; //对话框
Label* m_DialogMsg; //对话框消息
Button* m_DialogConfirm; //对话框确认按钮
Button* m_DialogCancel; //对话框取消按钮
//层变量
Layer* m_UI_Dialog; //对话框层 最高层
Layer* m_UI_Tool; //工具栏层 次高层
Layer* m_UI_Game; //游戏层 低层
Layer* m_UI_Background; //背景层 底层
//回调函数变量
public:
std::function
m_Dialog_ConfirmCallback; //对话框确认回调
//------------------------------函数----------------------------------
public:
MomoScene():m_IsNeedExit(false),m_Dialog_ConfirmCallback(nullptr){};
~MomoScene(){};
下面是对话框的使用方法:
auto fun = [=](Ref* e){
CCLog("OK You enter the Confirm");
};
LoadDialog("You Enter Continue",fun,true);
上述对话框的实现效果如下:(因为素材不太好,为了更好的展示效果,我在显示对话框的时候将菜单移除了)



表示不知道GIF图片要怎么展示出来 就先看着截图吧