使用方法:CCScale9Sprite* background=CCScale9Sprite::create("back.png");
background->setContentSize(CCSizeMake(400,200));//400x200是要生成的尺寸
background->setPosition(Center);
this->addChild(background);
4、回调函数的实现方案
对于回调一般有两种方式,一种是 delegate回调,这需要定义接口,由上层,继承实现接口,并把自己当作参数,传入弹出层,由弹出层调用 delegate 的接口方法实现,在Cocos2d-x 里面很多地方用到此方式。另一种则是函数绑定,就像 CCMenu 那样的绑定函数。
在这里设计的是按钮个数可变,功能也不尽相同,所以我们选择绑定函数!进一步封装,将弹出层的按钮回调函数设置为内部实现,然后在回调给它的上层,之后关闭对话框(关闭的操作由对话框层来完成)。回调给它的上层的时候传递CCNode参数,以其 tag 标示,点击的是哪个按钮。【用voidsetCallbackFunc(CCObject* target,SEL_CallFuncN callfun);作为外部接口,设置上层对象和上层的回调函数。】
5、onEnter动态创建弹出层
根据设置的属性去创建层有两种方式:
- 其一,实时设置,实时刷新。比如在 staticPopupLayer* create(const char* gackgroundImage)的实现里面,创建一个精灵,并设置好图片,添加到当前层,如果调用了 setContentSize我们再在此方法获取精灵后去修改这个精灵的大小
- 其二,保留属性,动态组建。也就是说前面一些封装的函数比如setTitle()、setContentText(),addButton()、setCallbackFunc()只用于设置属性参数(即给变量赋值)。参数设置好以后,在一个适当的执行时期,根据以上参数,动态创建符合需求的精灵/层,而这个操作在onEnter 里尤为合适。
在这里我们使用动态组建的方式,即在前面用一些变量来保存对话框所需的信息,
然后在 onEnter 中,读取这些信息,以实时添加至界面之中。
封装
//PopupLayer.h
#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
classPopupLayer : public CCLayer{
public:
PopupLayer();
~PopupLayer();
virtualbool init();
//需要重写触摸注册函数,重新给定触摸级别
virtualvoid registerWithTouchDispatcher();
//重写触摸函数,返回true,屏蔽其它层,达到“模态”效果
boolccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
//静态创建函数,创建一个弹出层,设置背景图片
staticPopupLayer* create(const char* backgroundImage);
//设置标题
voidsetTitle(const char* title,int fontsize = 20);
//设置文本内容,padding为文字到对话框两边预留的距离,这是可控的,距上方的距离亦是如此
voidsetContentText(const char* text,int fontsize=20,int padding=50,int paddingTop=100);
//设置上层对象和上层回调函数,用于回调时传递CCNode参数
voidsetCallBackFunc(CCObject* target,SEL_CallFuncNcallfun);
//添加menuItem按钮,封装了一个函数,传入些必要的参数
booladdButton(const char* normalImage,const char* selectedImage,constchar* title,int tag=0);
//为了在显示层时的属性生效,选择在onEnter里动态生成
virtualvoid onEnter();
virtualvoid onExit();
CREATE_FUNC(PopupLayer);
private:
voidbuttonCallBack(CCObject* pSender);
//文字内容两边的空白区域
intm_contentPadding;
intm_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);
};
//PopupLayer.cpp
#include"PopupLayer.h"
USING_NS_CC;
//构造函数中变量设初值
PopupLayer::PopupLayer()
{
m_callbackListener =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);
}
//初始化
boolPopupLayer::init()
{
this->setContentSize(CCSizeZero);
CCMenu* menu =CCMenu::create();
menu->setPosition(CCPointZero);
setMenuButton(menu);//set()方法
setTouchEnabled(true);//开启触摸响应
}
//重写触摸注册函数,重新给定触摸级别
voidPopupLayer::registerWithTouchDispatcher(){
//这里的触摸优先级设置为-128,与CCMenu同级,保证了屏蔽下方的触摸
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128,true);
}
//触摸函数ccTouchBegan,返回true
boolPopupLayer::ccTouchBegan( CCTouch *pTouch,CCEvent *pEvent){
}
//创建一个弹出层,给背景精灵变量赋值
PopupLayer*PopupLayer::create( const char* backgroundImage){
PopupLayer* popup =PopupLayer::create();
popup->setSpriteBackGround(CCSprite::create(backgroundImage));
popup->setSprite9BackGround(CCScale9Sprite::create(backgroundImage));
}
//给标题变量赋值
void PopupLayer::setTitle(const char* title,int fontsize ){
CCLabelTTF* ltfTitle =CCLabelTTF::create(title,"Arial",fontsize);
ltfTitle->setColor(ccc3(0,0));
}
//给文本变量赋值
voidPopupLayer::setContentText( const char* text,int fontsize,intpadding,int paddingTop ){
CCLabelTTF* content =CCLabelTTF::create(text,fontsize);
content->setColor(ccc3(0,0));
setLabelContentText(content);
m_contentPadding =padding;
m_contentPaddingTop =paddingTop;
}
//给下层层变量和回调函数变量赋值
voidPopupLayer::setCallBackFunc( CCObject* target,SEL_CallFuncNcallfun ){
m_callbackListener =target;
}
//给menu菜单变量添加Item
bool PopupLayer::addButton(const char* normalImage,const char*title,int tag ){
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->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);
}
//销毁弹出框,传递参数node给下层
voidPopupLayer::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->removeFromParentAndCleanup(true);
}
//全部参数都设定好后,在运行时动态加载
voidPopupLayer::onEnter(){
CCSize winSize =CCDirector::sharedDirector()->getWinSize();
CCPoint center =ccp(winSize.width/2,winSize.height/2);
if(getContentSize().equals(CCSizeZero)){
getSpriteBackGround()->setPosition(center);
this->addChild(getSpriteBackGround(),0);
contentSize =getSpriteBackGround()->getTexture()->getContentSize();
CCScale9Sprite* background =getSprite9BackGround();
background->setContentSize(getContentSize());
background->setPosition(center);
this->addChild(background,0);
contentSize =getContentSize();
this->addChild(getMenuButton());
float btnWidth =contentSize.width /(getMenuButton()->getChildrenCount()+1);
CCArray* array =getMenuButton()->getChildren();
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));
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);
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);
}
//退出
voidPopupLayer::onExit(){
}
弹出层调用:
//定义一个弹出层,传入一张背景图片
PopupLayer* popup =PopupLayer::create("popupBackGround.png");
//ContentSize是可选的设置,可以不设置,如果设置则把它当做9图缩放
//popup->setContentSize(CCSizeMake(400,360));
popup->setTitle("Message");
popup->setContentText("Mostpeople... blunder round this city.",20,50,150);
//设置回调函数,回调传回一个CCNode以获取tag判断点击的按钮
//这只是作为一种封装实现,如果使用delegate那就能够更灵活的控制参数了
popup->setCallBackFunc(this,callfuncN_selector(HelloWorld::buttonCallBack));
//添加按钮,设置图片、文字,tag信息
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