游戏开发引擎—Cocos2d-x 3.2开发《大富翁》退出对话框
游戏开发引擎—Cocos2d-x 3.2开发《大富翁》04_ 退出对话框
本文检索关键词:游戏引擎,游戏开发引擎,cocos引擎html5游戏开发 上一节中介绍了如何制作菜单场景,本节主要讲如何制作游戏中的退出对话框。
效果图,
首先看看在MenuScene.cpp如是如何调用对话框的,
1 void MenuScene::popupLayer(){
2 // 定义一个弹出层,传入一张背景图
3 PopupLayer* popDialog = PopupLayer::create(DIALOG_BG);
4 // ContentSize 是可选的设置,可以不设置,如果设置把它当作 9 图缩放 5 popDialog->setContentSize(CCSizeMake(Quit_Dialog_Size_Width, Quit_Dialog_Size_Height)); 6 popDialog->setTitle(DIALOG_TITLE);
7 popDialog->setContentText(DIALOG_CONTENT, 20, 60, 250);
8 // 设置回调函数,回调传回一个 CCNode 以获取 tag 判断点击的按钮
9 popDialog->setCallbackFunc(this, callfuncN_selector(MenuScene::quitButtonCallback));
10 // 添加按钮,设置图片,文字,tag 信息
11 popDialog->addButton(BUTTON_BG1, BUTTON_BG3, OK, Btn_Quit_OK_TAG); 12 popDialog->addButton(BUTTON_BG2, BUTTON_BG3, CANCEL, Btn_Quit_Cancel_TAG); 13 this->addChild(popDialog);// 添加到当前层
14 }
PopupLayer.h 和 PopupLayer.cpp是弹出对话框的代码,
首先看PopupLayer.h
1 const int Pop_FontSize = 20;//定义字体大小
2 class PopupLayer :public Layer
3 {
4 static PopupLayer * create(const char* backgroundImage);//根据背景图创建对象 5 void setTitle(const char* title ,int fontsize=Pop_FontSize);//设置对话框标题 6 void setContentText(const char* text ,int fontsize=Pop_FontSize ,int padding=50 ,int paddintTop=1
7 00);//设置对话框文本内容
8 void setCallbackFunc(Object* target, SEL_CallFuncN callfun);//设置按键回调方法 9 bool addButton(const char* normalImage, const char* selectedImage, const char* title, int tag=0);//
10 添加对话框按键,如确认取消
11 virtual void onEnter();//当进入时调用
12 virtual void onExit();//
13 void buttonCallback(CCObject* pSender);
14 int m_contentPadding;// 文字内容两边的空白区距离
15 int m_contentPaddingTop; //文字上边空白区距离
16 CCObject* m_callbackListener;
17 SEL_CallFuncN m_callback;
18 //定义具有retain属性的变量
19 CC_SYNTHESIZE_RETAIN(Menu*, m__pMenu, MenuButton); 20 CC_SYNTHESIZE_RETAIN(Sprite*, m__sfBackGround, SpriteBackGround); …………………….
};
PopupLayer.cpp部分代码如下,
1 PopupLayer::PopupLayer():m__pMenu(NULL), m_contentPadding(0), m_contentPaddingTop(0), m_c
2 allbackListener(NULL)
3 , m_callback(NULL), m__sfBackGround(NULL), m__s9BackGround(NULL), m__ltContentText(NULL),
4 m__ltTitle(NULL)
5 {
6 }
7 //释放变量
8 PopupLayer::~PopupLayer()
9 {
1 CC_SAFE_RELEASE(m__pMenu);
0 CC_SAFE_RELEASE(m__sfBackGround); 1 CC_SAFE_RELEASE(m__ltContentText); 1 CC_SAFE_RELEASE(m__ltTitle);
1 CC_SAFE_RELEASE(m__s9BackGround); 2 }
1void PopupLayer::setCallbackFunc(cocos2d::Object *target, SEL_CallFuncN callfun)
3 {
1//menuItem根据调传入的对象,会进行方法的回调 4 m_callbackListener = target;
1 m_callback = callfun;
5 }
1bool PopupLayer::init()
6 {
1 // 初始化需要的 Menu,随后跟进参数向menu中添加Item选项
7 Menu* menu = Menu::create();
1 menu->setPosition(CCPointZero);
8 setMenuButton(menu);
1setTouchMode(Touch::DispatchMode::ONE_BY_ONE); 9 auto listener = EventListenerTouchOneByOne::create(); 2 listener->setSwallowTouches(true);
0 listener->onTouchBegan = [](Touch *t,Event *e){
2CCLog("PopupLayer touch");
1 return true; 2 };
2 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//屏蔽下层事件响应
2 return true; 3 }
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
根据参数、标题、tag、图片效果,添加MenuItem选项, 1 bool PopupLayer::addButton(const char *normalImage, const char *selectedImage, const char *title, in
2 t tag)
3 {
4 Size winSize = CCDirector::getInstance()->getWinSize(); 5 Point pCenter = ccp(winSize.width / 2, winSize.height / 2); 6 // 创建MenuItem按钮,并设置回调方法
7 MenuItemImage* menuImage = MenuItemImage::create(normalImage, selectedImage, this, menu_
8 selector(PopupLayer::buttonCallback));
9 menuImage->setTag(tag);
1 menuImage->setPosition(pCenter);
0 // 给MenuItem添加文字说明并设置在MenuItem中位置
1Size imenu = menuImage->getContentSize(); 1 LabelTTF* ttf = LabelTTF::create(title, "", 20); 1 ttf->setColor(ccc3(0, 0, 0));
2 ttf->setPosition(ccp(imenu.width / 2, imenu.height / 2)); 1 menuImage->addChild(ttf);
3 getMenuButton()->addChild(menuImage); 1 return true;
4
1
5
1
6
现在来看MenuItem的回调方法buttonCallback 1 void PopupLayer::buttonCallback(cocos2d::CCObject *pSender) 2 {
3 Node* node = dynamic_cast(pSender); 4 CCLog("touch tag: %d", node->getTag()); 5 if (m_callback && m_callbackListener){ 6 (m_callbackListener->*m_callback)(node);//这会调用setCallbackFunc,,方法传入的MenuScene
7 对象的quitButtonCallback,,方法
8 }
9 this->removeFromParent();//把对话框从父节点移除 }
对话框弹出前调用onEnter方法进行界面初始化工作 1 void PopupLayer::onEnter()
2 {
3 …………………..
4 Size contentSize;
5 // 设置对话框背景,代码省略
6 // 添加按钮,并设置其位置
7 this->addChild(getMenuButton());
8 float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1);
9 Vector vecArray = getMenuButton()->getChildren(); 1 int j=0;
0 for(auto it=vecArray.begin();it!=vecArray.end();it++) 1 {
1 Node* node = dynamic_cast(*it); 1 node->setPosition(Point(winSize.width/2 - contentSize.width/2+btnWidth*(j+1),winSize.height/2-c
2 ontentSize.height/3));
1 j++;
3 }
1
4 // 显示对话框标题内容省略 1 // 添加对话框弹出效果
5 Action* popupLayer = Sequence::create(ScaleTo::create(0.0, 0.0),
1 ScaleTo::create(0.15, 1.05),
6 ScaleTo::create(0.08, 0.95),
1 ScaleTo::create(0.08, 1.0),NULL); 7 this->runAction(popupLayer); 1}
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
代码写的稍微多了些,为便于测试,我把代码上传一下,下载,cocos2d-x-3.2_richer(第
四部分).rar