Cocos2d-x中完成彈出對話框示例。本站提示廣大學習愛好者:(Cocos2d-x中完成彈出對話框示例)文章只能為提供參考,不一定能成為您想要的結果。以下是Cocos2d-x中完成彈出對話框示例正文
在游戲中我們常常會看到彈出一個對話框讓我們停止選擇,明天我們就在cocos2dx中完成這個對話框。對話框說白了也是一個層,當我們點擊某一個按鈕的時刻這個層被加進了以後的場景中,同時場景中的其他的層都是弗成點擊的,這個時刻就觸及到觸摸的優先級的一些成績,固然有些時刻你也能夠依據本身的須要讓其他的層也能夠點擊,然則事理都是一樣的,學會了這個其他的依照本身的請求去完成吧。上面我將彈出層零丁分裝成一個類,供我們挪用。
/*對話框場景類的頭文件*/
#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:
//注冊觸摸事宜,完成ccTouchBegan()辦法
void registerWithTouchDispatcher();
bool ccTouchBegan(CCTouch * touch,CCEvent * pevent);
//在彈出的對話框上加倆個按鈕,以下的函數是對應的按鈕的處置事宜
void yesButton(CCObject * object);
void noButton(CCObject * object);
//設置對話框的title
void setTitle();
//設置對話框的文本內容
void setContent();
//m_size代表的是對話框配景的年夜小
CCSize m_size;
//對話框的配景精靈
CCSprite * m_bgSprite;
};
#endif
/*對話框場景類的詳細完成*/
#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();
//設置這個層的配景圖片,而且設置其地位為全部屏幕的中點
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,代表的是菜單按鈕的觸摸優先級
//設置menu優先級,這裡設置為通俗menu的二倍減一,緣由看下邊
menu->setTouchPriority(kCCMenuHandlerPriority*2-1);
background->addChild(menu);
//設置標題和文本內容
this->setTitle();
this->setContent();
this->setTouchEnabled(true);
bRet = true;
}
while(0);
return bRet;
}
void PopScene::registerWithTouchDispatcher()
{
//kCCMenuHandlerPriority=-128,將這個值設置為-128的二倍,可以比下邊的層的優先級高
//並且ccTouchBegan的前往值為true,解釋其他的層將接收不到這個觸摸新聞了,只要這個層上邊的
//菜單的優先級比他還要打,所以它上邊的菜單是可以吸收到觸摸新聞的
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,
kCCMenuHandlerPriority*2,true);
}
bool PopScene::ccTouchBegan(CCTouch * touch,CCEvent * pevent)
{
return true;
}
//點擊菜單按鈕的時刻挪用的事宜處置函數
void PopScene::yesButton(CCObject * object)
{
this->removeFromParentAndCleanup(true);
}
void PopScene::noButton(CCObject * object)
{
this->removeFromParentAndCleanup(true);
}
//設置這個層的標題
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);
}
//設置層的內容
void PopScene::setContent()
{
CCLabelTTF * content = CCLabelTTF::create("hello! everyone,welcome to www.jb51.net",
"",24);
content->setPosition(ccp(m_size.width/2,m_size.height/2));
//設置ttf的文本域
content->setDimensions(CCSize(this->m_size.width-60,this->m_size.height-100));
//設置ttf的程度對齊方法
content->setHorizontalAlignment(kCCTextAlignmentLeft);
m_bgSprite->addChild(content);
}
//helloworld中按鈕的回調函數
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
PopScene* popLayer = PopScene::create();
this->addChild(popLayer);
}