欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

Dialog(弹窗)封装:FDDialogLayer

本文为firedragonpzy原创,转载务必在明显处注明:
转载自【Softeware MyZone】原文链接: 
http://www.firedragonpzy.com.cn/index.php/archives/3056

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
【加群请写:Software MyZone或者是firedragonpzy】
群论坛:火龙论坛正试运营阶段,欢迎大家多提些建设性意见……

本问主要说的就是弹窗的问题,游戏中避免不了弹窗,像如你退出游戏的时候,购买的时候等等,今天简单封装了一下,与大家分享:
下面是我的:FDDialogLayer.h

//
//  FDDialogLayer.h
//  CKCastle
//
//  Created by firedragonpzy on 13-1-29.
//
//

#ifndef __CKCastle__FDDialogLayer__
#define __CKCastle__FDDialogLayer__

#include "cocos2d.h"
#include "MySource.h"
#include "SimpleAudioEngine.h"
#include "FDMenu.h"
USING_NS_CC;
class FDDialogLayer : public CCLayer{
public:
    FDDialogLayer( unsigned dialogSpecies );
    virtual bool init();
    void onEnter();
    void onExit();
    bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    void setDialogSpecies( unsigned dialogSpecies );
    static FDDialogLayer* create(unsigned dialogSpecies  = DIALOG_GAME_EXIT );
private:
    unsigned m_pDialogSpecies;
    void moveIn();
	void moveOut();
    void moveOver();
    void initParams();
    void yesMenuCallBack( CCObject *pSender );
    void noMenuCallBack( CCObject *pSender );
};

#endif /* defined(__CKCastle__FDDialogLayer__) */

下面是我的:FDDialogLayer.cpp

//

//  FDDialogLayer.cpp

//  CKCastle

//

//  Created by firedragonpzy on 13-1-29.

//

//

#include "FDDialogLayer.h"

FDDialogLayer::FDDialogLayer( unsigned dialogSpecies ) :m_pDialogSpecies(dialogSpecies)

{

}

FDDialogLayer* FDDialogLayer::create(unsigned dialogSpecies)

{

FDDialogLayer *dialog = new FDDialogLayer(dialogSpecies);

if (dialog && dialog->init()) {

dialog->autorelease();

return dialog;

}

CC_SAFE_DELETE(dialog);

dialog = NULL;

return NULL;

}

bool FDDialogLayer::init()

{

bool bRet = false;

do {

CC_BREAK_IF(!CCLayer::init());

this->initParams();

bRet=true;

} while (0);

return bRet;

}

void FDDialogLayer::onEnter()

{

CCLayer::onEnter();

//    if (m_pDialogSpecies != DIALOG_GAME_LOADING) {
         this->moveIn();
//    }
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, true);

}

void FDDialogLayer::onExit()

{

CCLayer::onExit();

CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCTextureCache::sharedTextureCache()->removeAllTextures();
//2013/04/02 注意:再你使用了CCSpriteBatchNode的时候,这里如果还是将纹理全部移除,会有断言报错,提示:
//CCAssert(pSprite->getTexture()->getName() == m_pobTextureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id"),这时候推荐大家使用:
//CCTextureCache::sharedTextureCache()->removeUnusedTextures();

}

bool FDDialogLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){

CCLog("began");

return true;

}

void FDDialogLayer::moveOut(){

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCActionInterval* moveByAction = CCMoveBy::create(0.2f, CCPointMake(0, -winSize.height));

CCActionInterval* easeMove = CCEaseIn::create(moveByAction, 2.0f);

CCFiniteTimeAction* actionCall = CCCallFunc::create(this, callfunc_selector(FDDialogLayer::moveOver));

this->runAction(CCSequence::create(easeMove, actionCall, NULL));

}

void FDDialogLayer::moveIn(){

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCActionInterval* moveByAction = CCMoveBy::create(0.2f, CCPointMake(0, -winSize.height));

CCActionInterval* easeMove = CCEaseOut::create(moveByAction, 2.0f);

this->runAction(easeMove);

}

void FDDialogLayer::moveOver()

{

this->removeFromParentAndCleanup(true);

}

void FDDialogLayer::initParams()

{

CCSprite* dialogBg = NULL;
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    if (m_pDialogSpecies == DIALOG_GAME_LOADING) {
        dialogBg = CCSprite::create(DIALOG_GAME_LOADING_BG);
        dialogBg->setPosition(ccp(winSize.width/2,winSize.height/2));
        this->addChild(dialogBg,2);
        return;

    }else if(m_pDialogSpecies == DIALOG_GAME_EXIT)
    {
        dialogBg = CCSprite::create(DIALOG_GAME_EXIT_BG);
    }

    if (dialogBg) {
        dialogBg->setPosition(ccp(winSize.width/2,winSize.height/2));
        this->addChild(dialogBg,2);
        //button
        CCMenuItemImage* yesMenuItem = CCMenuItemImage::create(DIALOG_YES, DIALOG_YES_SELECTED, this, menu_selector(FDDialogLayer::yesMenuCallBack));
        CCMenuItemImage* noMenuItem = CCMenuItemImage::create(DIALOG_NO, DIALOG_NO_SELECTED, this,  menu_selector(FDDialogLayer::noMenuCallBack));
        FDMenu* menu = FDMenu
        ::create(yesMenuItem, noMenuItem, NULL);
        menu->setPosition(ccp(winSize.width/2, winSize.height/2));
        menu->alignItemsHorizontallyWithPadding(winSize.width*0.2);
        this->addChild(menu,3);
    }else //loading
    {

        CCLog("loading");
    }
}

void FDDialogLayer::yesMenuCallBack( CCObject *pSender )

{

CCLog("FDDialogLayer::yesMenuCallBack");

if (m_pDialogSpecies == DIALOG_GAME_EXIT) {

CocosDenshion::SimpleAudioEngine::sharedEngine()->end();

CCDirector::sharedDirector()->end();

}else

{

this->moveOut();

}

}

void FDDialogLayer::noMenuCallBack(CCObject *pSender)

{

CCLog("FDDialogLayer::noMenuCallBack");

this->moveOut();

}

为了体现出我moveIn的效果,你在使用本对话框的时候需要设置一下位置:贴出调用方法:

FDDialogLayer *dialog = FDDialogLayer::create(DIALOG_GAME_EXIT);

dialog->setPosition(ccp(0,CCDirector::sharedDirector()->getWinSize().height));

this->addChild(dialog);

大体就是这样子的,对了这里面还牵扯到一个FDMenu,贴出源码:

FDMenu.h


//

//  FDMenu.h

//  CKCastle

//

//  Created by firedragonpzy on 13-1-10.

//

//

#ifndef __FDMenu_h__

#define __FDMenu_h__

#include "cocos2d.h"

USING_NS_CC;

class FDMenu : public CCMenu

{

public:

/** creates an empty CCMenu */

static FDMenu* create();

/** creates a CCMenu with it's items */

static FDMenu* create(CCMenuItem* item, ...);

/** creates a CCMenu with a CCArray of CCMenuItem objects */

static FDMenu* create(CCArray* pArrayOfItems);

virtual void registerWithTouchDispatcher();

};

#endif

FDMenu.cpp


//

//  FDMenu.cpp

//  CKCastle

//

//  Created by firedragonpzy on 13-1-10.

//

//

#include "FDMenu.h"

USING_NS_CC;

#include "cocos-ext.h"
using namespace extension;

FDMenu* FDMenu::create()

{

return FDMenu::create(NULL, NULL);

}

FDMenu * FDMenu::create(CCMenuItem* item, ...)

{

va_list args;

va_start(args,item);

FDMenu *pRet = new FDMenu();

if (pRet && pRet->initWithItems(item, args))

{

pRet->autorelease();

va_end(args);

return pRet;

}

va_end(args);

CC_SAFE_DELETE(pRet);

return NULL;

}

FDMenu* FDMenu::create(CCArray* pArrayOfItems)

{

FDMenu *pRet = new FDMenu();

if (pRet && pRet->initWithArray(pArrayOfItems))

{

pRet->autorelease();

}

else

{

CC_SAFE_DELETE(pRet);

}

return pRet;

}

void FDMenu::registerWithTouchDispatcher()

{

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -129, true);

}

  1. Pingback: Dialog(弹窗)封装 - 移动端开发 - 无忧网