在用Cocos2DX引擎开发游戏的过程中,我们经常需要弹出一个对话框或者提示框,通知玩家一些必要的信息。这时候我们就需要考虑怎样设计和封装一个这样的弹出对话框。首先,这样的弹出框一般都是“模态窗口”,即在没有对当前弹出的对话框进行确认的时候,不能继续往下操作。
@H_403_25@1、弹出层的触摸优先级,操作与显示相一致
@H_403_25@2、定制按钮个数
@H_403_25@3、窗口的大小可变
- 如果没有设置 ContentSize ,那么采取的方案是,窗口大小与传入图片一样大。
- 如果设置了ContentSize,则将窗口设定为指定大小。这时候需要将背景图片进行缩放,如何缩放? 【答案】是利用9宫格图CCScale9Sprite缩放图片,缩放带@H_403_25@圆角的图片。原理如下:
@H_403_25@5、onEnter动态创建弹出层
- 其一,实时设置,实时刷新。比如在 static PopupLayer* create(const char* gackgroundImage) 的实现里面,创建一个精灵,并设置好图片,添加到当前层,如果调用了 setContentSize 我们再在此方法获取精灵后去修改这个精灵的大小
- 其二,保留属性,动态组建。也就是说前面一些封装的函数比如setTitle()、setContentText(),addButton()、setCallbackFunc()只用于设置属性参数(即给变量赋值)。参数设置好以后,在一个适当的执行时期,根据以上参数,动态创建符合需求的精灵/层,而这个操作在 onEnter 里尤为合适。
@H_403_25@
@H_403_25@封装
@H_403_25@//PopupLayer.h
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
class PopupLayer : public CCLayer{
public:
PopupLayer();
~PopupLayer();
virtual bool init();
virtual void registerWithTouchDispatc
//重写触摸函数,返回true,屏蔽其它层,达到“模态”效果
bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
static PopupLayer* create(const char* backgroundImage);
//设置标题
void setTitle(const char* title,int fontsize = 20);
void setContentText(const char* text,int fontsize=20,int padding=50,int paddingTop=100);
//设置上层对象和上层回调函数,用于回调时传递CCNode参数
void setCallBackFunc(CCObject* target,SEL_CallFuncN callfun);
//添加menuItem按钮,封装了一个函数,传入些必要的参数
bool addButton(const char* normalImage,const char* selectedImage,const char* title,int tag=0);
//为了在显示层时的属性生效,选择在onEnter里动态生成
virtual void onEnter();
virtual void onExit();
CREATE_FUNC(PopupLayer);
private:
void buttonCallBack(CCObject* pSender);
int m_contentPadding;
int m_contentPaddingTop;
CCObject* m_callbackListener;
SEL_CallFuncN m_callback;
//定义了CCMenu*类型变量m_pMenu,并且直接定义默认的set/get方法
CC_SYNTHESIZE_RETAIN(CCMenu*,m_pMenu,MenuButton);
CC_SYNTHESIZE_RETAIN(CCSprite*,m_sfBackGround,SpriteBackGround);
CC_SYNTHESIZE_RETAIN(CCScale9Sprite*,m_s9BackGround,Sprite9BackGround);
CC_SYNTHESIZE_RETAIN(CCLabelTTF*,m_ltTitle,LabelTitle);
CC_SYNTHESIZE_RETAIN(CCLabelTTF*,m_ltContentText,LabelContentText);
};
@H_403_25@//PopupLayer.cpp
#include "PopupLayer.h"
USING_NS_CC;
// 构造函数中变量设初值
PopupLayer::PopupLayer()
{
m_contentPadding = 0;
m_contentPaddingTop = 0;
m_pMenu = NULL;
m_callbackListener = NULL;
m_callback = NULL;
m_sfBackGround = NULL;
m_s9BackGround = NULL;
m_ltContentText = NULL;
m_ltTitle = NULL;
}
//释放
PopupLayer::~PopupLayer()
{
CC_SAFE_RELEASE(m_pMenu);
CC_SAFE_RELEASE(m_sfBackGround);
CC_SAFE_RELEASE(m_s9BackGround);
CC_SAFE_RELEASE(m_ltContentText);
CC_SAFE_RELEASE(m_ltTitle);
}
//初始化
bool PopupLayer::init()
{
if ( !CCLayer::init() ){
return false;
}
this->setContentSize(CCSizeZero);
//初始化需要的Menu
CCMenu* menu = CCMenu::create();
menu->setPosition(CCPointZero);
setMenuButton(menu);
//set()方法
setTouchEnabled(true);
//开启触摸响应 return true;
// 这里的触摸优先级设置为-128,与CCMenu同级,保证了屏蔽下方的触摸
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128,true);
return true;
}
//创建一个弹出层,给背景精灵变量赋值
PopupLayer* PopupLayer::create( const char* backgroundImage ){
PopupLayer* popup = PopupLayer::create();
popup->setSpriteBackGround(CCSprite::create(backgroundImage));
popup->setSprite9BackGround(CCScale9Sprite::create(backgroundImage));
return popup;
CCLabelTTF* ltfTitle = CCLabelTTF::create(title,"Arial",fontsize);
ltfTitle->setColor(ccc3(0,0));
setLabelTitle(ltfTitle);
}
//给文本变量赋值
void PopupLayer::setContentText( const char* text,int fontsize,int padding,int paddingTop ){
CCLabelTTF* content = CCLabelTTF::create(text,fontsize);
content->setColor(ccc3(0,0));
setLabelContentText(content);
m_contentPadding = padding;
m_contentPaddingTop = paddingTop;
m_callbackListener = target;
m_callback = callfun;
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint center = ccp(winSize.width/2,winSize.height/2);
CCMenuItemImage* menuImage = CCMenuItemImage::create(
normalImage,selectedImage,this,menu_selector(PopupLayer::buttonCallBack));
menuImage->setTag(tag);
menuImage->setPosition(center);
CCSize menuSize = menuImage->getContentSize();
CCLabelTTF* ttf = CCLabelTTF::create(title,15);
ttf->setColor(ccc3(0,0));
ttf->setPosition(ccp(menuSize.width/2,menuSize.height/2));
menuImage->addChild(ttf);
getMenuButton()->addChild(menuImage);
return true;
}
//销毁弹出框,传递参数node给下层
void PopupLayer::buttonCallBack( CCObject* pSender ){
CCNode* node = dynamic_cast(pSender);
CCLog("touch tag: %d",node->getTag());
if (m_callback && m_callbackListener)
{
//执行HelloWorld层的回调函数,传递node参数
(m_callbackListener->*m_callback)(node);
}
this->removeFromParentAndClean
up(true);
}
//全部参数都设定好后,在运行时动态加载
void PopupLayer::onEnter(){
CCLayer::onEnter();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint center = ccp(winSize.width/2,winSize.height/2);
CCSize contentSize;
// 设定好参数,在运行时加载
if (getContentSize().equals(CCSizeZero)){
getSpriteBackGround()->setPosition(center);
this->addChild(getSpriteBackGround(),0);
contentSize = getSpriteBackGround()->getTexture()->getContentSize();
}
else{
CCScale9Sprite* background = getSprite9BackGround();
background->setContentSize(getContentSize());
background->setPosition(center);
this->addChild(background,0);
contentSize = getContentSize();
}
//添加按钮,并根据Item的个数设置其位置
this->addChild(getMenuButton());
float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount()+1);
CCArray* array = getMenuButton()->getChildren();
CCObject* pObj = NULL;
int i = 0;
CCARRAY_FOREACH(array,pObj){
CCNode* node = dynamic_cast(pObj);
node->setPosition(ccp(winSize.width/2 - contentSize.width/2 + btnWidth*(i+1),
winSize.height/2 - contentSize.height/3)); i++;
}
if (getLabelTitle()){
getLabelTitle()->setPosition(ccpAdd(center,ccp(0,contentSize.height/2 - 25.0f)));
this->addChild(getLabelTitle());
}
if (getLabelContentText()){
CCLabelTTF* ltf = getLabelContentText();
ltf->setPosition(center);
ltf->setDimensions(CCSizeMake(contentSize.width - m_contentPadding*2,contentSize.height - m_contentPaddingTop));
ltf->setHorizontalAlignment(kCCTextAlignmentLeft);
this->addChild(ltf);
}
//弹出效果
CCSequence *popupActions = CCSequence::create(
CCScaleTo::create(0.0,0.0),
CCScaleTo::create(0.06,1.05),
CCScaleTo::create(0.08,0.95),1.0),NULL);
this->runAction(popupActions);
CCLayer::onExit();
}
弹出层调用:
// 定义一个弹出层,传入一张背景图片
PopupLayer* popup = PopupLayer::create("popupBackGround.png");
// ContentSize是可选的设置,可以不设置,如果设置则把它当做9图缩放
//popup->setContentSize(CCSizeMake(400,360));
popup->setTitle("Message");
popup->setContentText("Most people... blunder round this city.",20,50,150);
// 设置回调函数,回调传回一个CCNode以获取tag判断点击的按钮
// 这只是作为一种封装实现,如果使用delegate那就能够更灵活的控制参数了
popup->setCallBackFunc(this,callfuncN_selector(HelloWorld::buttonCallBack));
popup->addButton("button.png","button.png","Ok",0);
popup->addButton("button.png","Cancel",1);
this->addChild(popup);
测试截图:
这样,对话框的基本模型就完成了,它实现了以下功能:
参考文:http://blog.csdn.net/u012421525/article/details/14231205