首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第四部分 退出对话框

Cocos2d-x 3.2 大富翁游戏项目开发-第四部分 退出对话框

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">这部分代码从网上查阅了一下,基本都是以前版本编写的,需要稍微改动一下即可,效果如图</span>


首先看看在MenuScene.cpp如是如何调用起对话框的
void MenuScene::popupLayer(){
    // 定义一个弹出层,传入一张背景图
    PopupLayer* popDialog = PopupLayer::create(DIALOG_BG);
    // ContentSize 是可选的设置,可以不设置,如果设置把它当作 9 图缩放
    popDialog->setContentSize(CCSizeMake(Quit_Dialog_Size_Width, Quit_Dialog_Size_Height)); 
    popDialog->setTitle(DIALOG_TITLE);
    popDialog->setContentText(DIALOG_CONTENT, 20, 60, 250);
    // 设置回调函数,回调传回一个 CCNode 以获取 tag 判断点击的按钮
    popDialog->setCallbackFunc(this, callfuncN_selector(MenuScene::quitButtonCallback));
    // 添加按钮,设置图片,文字,tag 信息
    popDialog->addButton(BUTTON_BG1, BUTTON_BG3, OK, Btn_Quit_OK_TAG);
    popDialog->addButton(BUTTON_BG2, BUTTON_BG3, CANCEL, Btn_Quit_Cancel_TAG);
    this->addChild(popDialog);// 添加到当前层
}

PopupLayer.h 和 PopupLayer.cpp是弹出对话框的代码:

首先看PopupLayer.h
const int Pop_FontSize = 20;//定义字体大小
class PopupLayer :public Layer
{
	static PopupLayer * create(const char* backgroundImage);//根据背景图创建对象
	void setTitle(const char* title ,int fontsize=Pop_FontSize);//设置对话框标题
	void setContentText(const char* text ,int fontsize=Pop_FontSize ,int padding=50 ,int paddintTop=100);//设置对话框文本内容
	void setCallbackFunc(Object* target, SEL_CallFuncN callfun);//设置按键回调方法
	bool addButton(const char* normalImage, const char* selectedImage, const char* title, int tag=0);//添加对话框按键,如确认取消
	virtual void onEnter();//当进入时调用
	virtual void onExit();//
void buttonCallback(CCObject* pSender);
    int m_contentPadding;// 文字内容两边的空白区距离
int m_contentPaddingTop;  //文字上边空白区距离
 CCObject* m_callbackListener;
SEL_CallFuncN m_callback;
//定义具有retain属性的变量
    CC_SYNTHESIZE_RETAIN(Menu*, m__pMenu, MenuButton);
CC_SYNTHESIZE_RETAIN(Sprite*, m__sfBackGround, SpriteBackGround);
…………………….
};


PopupLayer.cpp部分代码如下:


PopupLayer::PopupLayer():m__pMenu(NULL), m_contentPadding(0), m_contentPaddingTop(0), 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__ltContentText);
    CC_SAFE_RELEASE(m__ltTitle);
    CC_SAFE_RELEASE(m__s9BackGround);
}


void PopupLayer::setCallbackFunc(cocos2d::Object *target, SEL_CallFuncN callfun)
{
//menuItem根据调传入的对象,会进行方法的回调
    m_callbackListener = target;
    m_callback = callfun;    
}


bool PopupLayer::init()
{     
        // 初始化需要的 Menu,随后跟进参数向menu中添加Item选项
        Menu* menu = Menu::create();
        menu->setPosition(CCPointZero);
        setMenuButton(menu);
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
auto listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
  listener->onTouchBegan = [](Touch *t,Event *e){
CCLog("PopupLayer touch");
           return true;
    };
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);//屏蔽下层事件响应
	return true;
}


根据参数,标题,tag ,图片效果,添加MenuItem选项
bool PopupLayer::addButton(const char *normalImage, const char *selectedImage, const char *title, int tag)
{
Size winSize = CCDirector::getInstance()->getWinSize();
    Point pCenter = ccp(winSize.width / 2, winSize.height / 2);
    // 创建MenuItem按钮,并设置回调方法
    MenuItemImage* menuImage = MenuItemImage::create(normalImage, selectedImage, this, menu_selector(PopupLayer::buttonCallback));
    menuImage->setTag(tag);
    menuImage->setPosition(pCenter);
    // 给MenuItem添加文字说明并设置在MenuItem中位置
Size imenu = menuImage->getContentSize();
    LabelTTF* ttf = LabelTTF::create(title, "", 20);
    ttf->setColor(ccc3(0, 0, 0));
    ttf->setPosition(ccp(imenu.width / 2, imenu.height / 2));
    menuImage->addChild(ttf);
    getMenuButton()->addChild(menuImage);
    return true;

现在来看MenuItem的回调方法buttonCallback


void PopupLayer::buttonCallback(cocos2d::CCObject *pSender)
{
    Node* node = dynamic_cast<Node*>(pSender);
    CCLog("touch tag: %d", node->getTag());
    if (m_callback && m_callbackListener){
        (m_callbackListener->*m_callback)(node);//这会调用setCallbackFunc()方法传入的MenuScene对象的quitButtonCallback()方法
    }
    this->removeFromParent();//把对话框从父节点移除 
}

对话框弹出前调用onEnter方法进行界面初始化工作

void PopupLayer::onEnter()
{
…………………..
    Size contentSize;
    // 设置对话框背景,代码省略
    // 添加按钮,并设置其位置
    this->addChild(getMenuButton());
    float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1);
Vector<Node*> vecArray = getMenuButton()->getChildren();
	int j=0;
	for(auto it=vecArray.begin();it!=vecArray.end();it++)
	{
		Node* node = dynamic_cast<Node*>(*it);
		node->setPosition(Point(winSize.width/2 - contentSize.width/2+btnWidth*(j+1),winSize.height/2-contentSize.height/3));
		j++;
	}

    // 显示对话框标题内容省略
    // 添加对话框弹出效果
    Action* popupLayer = Sequence::create(ScaleTo::create(0.0, 0.0),
                                          ScaleTo::create(0.15, 1.05),
                                          ScaleTo::create(0.08, 0.95),
    ScaleTo::create(0.08, 1.0),NULL);
   this->runAction(popupLayer);
}

代码写的稍微多了些,为便于测试,我把代码上传一下,下载地址如下:

http://download.csdn.net/detail/lideguo1979/8263669

未完待续……………………………



Cocos2d-x 3.2 大富翁游戏项目开发-第四部分 退出对话框