Cocos2d-x中實現(xiàn)彈出對話框示例
更新時間:2014年09月12日 10:13:34 投稿:junjie
這篇文章主要介紹了Cocos2d-x中實現(xiàn)彈出對話框示例,注意本文代碼中的注釋,本文同時給出了效果圖,需要的朋友可以參考下
在游戲中我們經(jīng)常會看到彈出一個對話框讓我們進行選擇,今天我們就在cocos2dx中實現(xiàn)這個對話框。對話框說白了也是一個層,當我們點擊某一個按鈕的時候這個層被加進了當前的場景中,同時場景中的其他的層都是不可點擊的,這個時候就涉及到觸摸的優(yōu)先級的一些問題,當然有些時候你也可以根據(jù)自己的需要讓其他的層也可以點擊,但是道理都是一樣的,學(xué)會了這個其他的按照自己的要求去實現(xiàn)吧。下面我將彈出層單獨分裝成一個類,供我們調(diào)用。

/*對話框場景類的頭文件*/
#ifndef _POP_SCENE_H_
#define _POP_SCENE_H_
#include "cocos2d.h"
using namespace cocos2d;
class PopScene : public CCLayer
{
public:
static CCScene * scene();
bool init();
CREATE_FUNC(PopScene);
private:
//注冊觸摸事件,實現(xiàn)ccTouchBegan()方法
void registerWithTouchDispatcher();
bool ccTouchBegan(CCTouch * touch,CCEvent * pevent);
//在彈出的對話框上加倆個按鈕,以下的函數(shù)是對應(yīng)的按鈕的處理事件
void yesButton(CCObject * object);
void noButton(CCObject * object);
//設(shè)置對話框的title
void setTitle();
//設(shè)置對話框的文本內(nèi)容
void setContent();
//m_size代表的是對話框背景的大小
CCSize m_size;
//對話框的背景精靈
CCSprite * m_bgSprite;
};
#endif
/*對話框場景類的具體實現(xiàn)*/
#include "PopScene.h"
CCScene * PopScene::scene()
{
CCScene * scene = NULL;
do
{
scene = CCScene::create();
PopScene * layer = PopScene::create();
scene->addChild(layer);
}
while(0);
return scene;
}
bool PopScene::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//設(shè)置這個層的背景圖片,并且設(shè)置其位置為整個屏幕的中點
CCSprite * background = CCSprite::create("background.png");
m_bgSprite = background;
background->setPosition(ccp(winSize.width/2,winSize.height/2));
this->addChild(background);
//獲得背景圖片的大小
CCSize contentSize = background->getContentSize();
m_size = contentSize;
//添加倆個菜單在這個層中
CCMenuItemImage * item1 = CCMenuItemImage::create("btn-play-normal.png",
"btn-play-selected.png","",
this,menu_selector(PopScene::yesButton));
CCMenuItemImage * item2 = CCMenuItemImage::create("btn-highscores-normal.png",
"btn-highscores-selected.png","",
this,menu_selector(PopScene::noButton));
CCMenu * menu = CCMenu::create(item1,item2,NULL);
menu->alignItemsHorizontallyWithPadding(5);
menu->setPosition(ccp(contentSize.width/2,contentSize.height/3));
//kCCMenuHandlerPriority的值為-128,代表的是菜單按鈕的觸摸優(yōu)先級
//設(shè)置menu優(yōu)先級,這里設(shè)置為普通menu的二倍減一,原因看下邊
menu->setTouchPriority(kCCMenuHandlerPriority*2-1);
background->addChild(menu);
//設(shè)置題目和文本內(nèi)容
this->setTitle();
this->setContent();
this->setTouchEnabled(true);
bRet = true;
}
while(0);
return bRet;
}
void PopScene::registerWithTouchDispatcher()
{
//kCCMenuHandlerPriority=-128,將這個值設(shè)置為-128的二倍,可以比下邊的層的優(yōu)先級高
//而且ccTouchBegan的返回值為true,說明其他的層將接受不到這個觸摸消息了,只有這個層上邊的
//菜單的優(yōu)先級比他還要打,所以它上邊的菜單是可以接收到觸摸消息的
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
kCCMenuHandlerPriority*2,true);
}
bool PopScene::ccTouchBegan(CCTouch * touch,CCEvent * pevent)
{
return true;
}
//點擊菜單按鈕的時候調(diào)用的事件處理函數(shù)
void PopScene::yesButton(CCObject * object)
{
this->removeFromParentAndCleanup(true);
}
void PopScene::noButton(CCObject * object)
{
this->removeFromParentAndCleanup(true);
}
//設(shè)置這個層的題目
void PopScene::setTitle()
{
//CCLabelTTF * title = CCLabelTTF::create("Tips","",24);
CCLabelBMFont * title = CCLabelBMFont::create("Tips","bitmapFontChinese.fnt");
title->setPosition(ccp(m_size.width/2,m_size.height-title->getContentSize().height/2));
m_bgSprite->addChild(title);
}
//設(shè)置層的內(nèi)容
void PopScene::setContent()
{
CCLabelTTF * content = CCLabelTTF::create("hello! everyone,welcome to www.dbjr.com.cn",
"",24);
content->setPosition(ccp(m_size.width/2,m_size.height/2));
//設(shè)置ttf的文本域
content->setDimensions(CCSize(this->m_size.width-60,this->m_size.height-100));
//設(shè)置ttf的水平對齊方式
content->setHorizontalAlignment(kCCTextAlignmentLeft);
m_bgSprite->addChild(content);
}
//helloworld中按鈕的回調(diào)函數(shù)
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
PopScene* popLayer = PopScene::create();
this->addChild(popLayer);
}

您可能感興趣的文章:
- 剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作
- iOS開發(fā)中使用cocos2d添加觸摸事件的方法
- cocos2dx骨骼動畫Armature源碼剖析(三)
- cocos2dx骨骼動畫Armature源碼剖析(二)
- cocos2dx骨骼動畫Armature源碼剖析(一)
- Cocos2d-x 3.x入門教程(二):Node節(jié)點類
- Cocos2d-x 3.x入門教程(一):基礎(chǔ)概念
- Cocos2d-x中調(diào)用Lua及HelloWorld.lua源碼分解
- Cocos2d-x中CCEditBox文本輸入框的使用實例
- Cocos2d-x中使用CCScrollView來實現(xiàn)關(guān)卡選擇實例
- Cocos2d-x觸摸事件實例
- Cocos2d-x人物動作類實例
- 詳解iOS游戲開發(fā)中Cocos2D的坐標位置關(guān)系
相關(guān)文章
C++ Opencv imfill孔洞填充函數(shù)的實現(xiàn)思路與代碼
在Matlab下,使用imfill可以很容易的完成孔洞填充操作,下面這篇文章主要給大家介紹了關(guān)于C++ Opencv imfill孔洞填充函數(shù)的實現(xiàn)思路與代碼,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09

