首页 > 代码库 > cocos2d-x 弹窗

cocos2d-x 弹窗

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 registerWithTouchDispatcher();    //重写触摸函数,返回true,屏蔽其它层,达到“模态”效果    bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);    //静态创建函数,创建一个弹出层,设置背景图片    static PopupLayer* create(const char* backgroundImage);    //设置标题    void setTitle(const char* title, int fontsize = 20);        //设置文本内容,padding 为文字到对话框两边预留的距离,这是可控的,距上方的距离亦是如此    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);};
View Code

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;}//重写触摸注册函数,重新给定触摸级别void PopupLayer::registerWithTouchDispatcher(){    // 这里的触摸优先级设置为-128,与CCMenu同级,保证了屏蔽下方的触摸    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true);}//触摸函数ccTouchBegan,返回truebool PopupLayer::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent ){    return true;}//创建一个弹出层,给背景精灵变量赋值PopupLayer* PopupLayer::create( const char* backgroundImage ){    PopupLayer* popup = PopupLayer::create();    popup->setSpriteBackGround(CCSprite::create(backgroundImage));    popup->setSprite9BackGround(CCScale9Sprite::create(backgroundImage));    return popup;}//给标题变量赋值void PopupLayer::setTitle( const char* title, int fontsize ){    CCLabelTTF* ltfTitle = CCLabelTTF::create(title, "Arial", fontsize);    ltfTitle->setColor(ccc3(0, 0, 0));    setLabelTitle(ltfTitle);}//给文本变量赋值void PopupLayer::setContentText( const char* text, int fontsize, int padding, int paddingTop ){    CCLabelTTF* content = CCLabelTTF::create(text, "Arial", fontsize);    content->setColor(ccc3(0, 0, 0));    setLabelContentText(content);    m_contentPadding = padding;    m_contentPaddingTop = paddingTop;}//给下层层变量和回调函数变量赋值void PopupLayer::setCallBackFunc( CCObject* target, SEL_CallFuncN callfun ){    m_callbackListener = target;    m_callback = callfun;}//给menu菜单变量添加Itembool PopupLayer::addButton( const char* normalImage, const char* selectedImage, 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->setTag(tag);    menuImage->setPosition(center);    // 添加文字说明并设置位置    CCSize menuSize = menuImage->getContentSize();    CCLabelTTF* ttf = CCLabelTTF::create(title, "Arial", 15);    ttf->setColor(ccc3(0, 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 = (CCNode *)pSender;    CCLog("touch tag: %d", node->getTag());    if (m_callback && m_callbackListener)    {    //执行HelloWorld层的回调函数,传递node参数    (m_callbackListener->*m_callback)(node);    }    this->removeFromParentAndCleanup(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,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 = (CCNode *)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),    CCScaleTo::create(0.08, 1.0), NULL);    this->runAction(popupActions);}//退出void PopupLayer::onExit(){    CCLayer::onExit();}
View Code

HelloWorldScene.h 

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::CCLayer{public:    // Method ‘init‘ in cocos2d-x returns bool, instead of ‘id‘ in cocos2d-iphone (an object pointer)    virtual bool init();    // there‘s no ‘id‘ in cpp, so we recommend to return the class instance pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);    // preprocessor macro for "static create()" constructor ( node() deprecated )    CREATE_FUNC(HelloWorld);        void menuCallback(cocos2d::CCObject *pSender);    void popupLayer();    void buttonCallback(cocos2d::CCNode *pNode);};#endif // __HELLOWORLD_SCENE_H__
View Code

HelloWorldScene.cpp 

#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#include "PopupLayer.h"using namespace cocos2d;using namespace CocosDenshion;CCScene* HelloWorld::scene(){    // ‘scene‘ is an autorelease object    CCScene *scene = CCScene::create();        // ‘layer‘ is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }    CCSize winSize = CCDirector::sharedDirector()->getWinSize();    CCPoint pointCenter = ccp(winSize.width / 2, winSize.height / 2);        // 添加背景图片    CCSprite* background = CCSprite::create("HelloWorld.png");    background->setPosition(pointCenter);    background->setScale(1.5f);    this->addChild(background);         // 添加菜单    CCMenu* menu = CCMenu::create();        CCMenuItemFont* menuItem = CCMenuItemFont::create("popup", this, menu_selector(HelloWorld::menuCallback));    menuItem->setPosition(ccp(winSize.width / 2, winSize.height / 2));    menuItem->setColor(ccc3(0, 0, 0));    menu->addChild(menuItem);            menu->setPosition(CCPointZero);    this->addChild(menu);                return true;}void HelloWorld::menuCallback(cocos2d::CCObject *pSender){    popupLayer();}void HelloWorld::popupLayer(){    // 定义一个弹出层,传入一张背景图片    PopupLayer* popup = PopupLayer::create("HelloWorld.png");    // ContentSize是可选的设置,可以不设置,如果设置则把它当做9图缩放    popup->setContentSize(CCSizeMake(250, 260));    popup->setTitle("Message");    popup->setContentText("Most people... ", 20, 50, 150);    // 设置回调函数,回调传回一个CCNode以获取tag判断点击的按钮    // 这只是作为一种封装实现,如果使用delegate那就能够更灵活的控制参数了    popup->setCallBackFunc(this, callfuncN_selector(HelloWorld::buttonCallback));    //添加按钮,设置图片、文字,tag信息    popup->addButton("CloseNormal.png", "CloseSelected.png", "Ok", 0);    popup->addButton("CloseNormal.png", "CloseSelected.png", "Cancel", 1);    this->addChild(popup);}void HelloWorld::buttonCallback(cocos2d::CCNode *pNode){    CCLog("button call back. tag: %d", pNode->getTag());}void HelloWorld::menuCloseCallback(CCObject* pSender){    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}
View Code

参考图:

 

转载自

http://blog.sina.com.cn/s/blog_705a5ff00101ox4s.html

http://www.tuicool.com/articles/VzAFJr

cocos2d-x 弹窗