cocos2d-x带有自定义button响应事件的messagebox

前端之家收集整理的这篇文章主要介绍了cocos2d-x带有自定义button响应事件的messagebox前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

官方给的例子里MessageBox居然只有一个确定按钮,没有我想要的确认,取消按钮,没办法就自己写了一个,难点是确认按钮的回调格式怎么写,定义如下:

bool initWithNode(cocos2d::Node* node,const std::string& msg,
const float& time,cocos2d::Vec2 point,const cocos2d::ccMenuCallback& callback);

注意这里的ccMenuCallback& callback,这是我从官方menuItemImage源代码里抄来的,ccMenuCallback实际上是:

typedef std::function<void(Ref*)> ccMenuCallback;

在实现文件中是这么写:

//确认提交按钮
MenuItemImage* btnOK = MenuItemImage::create("Store/okBtn.png","Store/okBtnDown.png","Store/okBtn.png",callback); //注意这个callback,不要在这里加什么CC_CALLBACK_XX什么的了

调用的时候这么写:

BDMessageBox* msgBox = BDMessageBox::createToast(this,“对不起金钱不足,是否充值?”,1.f,
pos,CC_CALLBACK_1(UpdataSence::gotoStoreLayer,this));

这里才用到CC_CALLBACK_1


给出完整的MessageBox源码,比较简陋,可以自己修改内容,不过我还加了从小变大的弹出效果


BDMessageBox.h

/*********************************
   自定义弹出框
**********************************/
#ifndef _BDMessageBox_
#define _BDMessageBox_

#include "cocos2d.h"

class BDMessageBox : public cocos2d::Layer
{
public:
	BDMessageBox();
	~BDMessageBox();
	//初始化弹出框 node:父结点,msg:显示的消息内容,time:弹出框弹出需要的时间,暂时未用,point:显示在父结点的位置
	//callback: 确认按钮的回调函数
	bool initWithNode(cocos2d::Node* node,const float& time,const cocos2d::ccMenuCallback& callback);

	//产生一个弹出框 node:父结点,msg:显示的消息内容,time:弹出框弹出需要的时间,暂时未用,point:显示在父结点的位置
	//callback: 确认按钮的回调函数
	static BDMessageBox* createToast(cocos2d::Node* node,const cocos2d::ccMenuCallback& callback);  //std::function<void()> callback

	virtual void onEnter();
protected:
	void OkClick();  //点击OK响应,这里已经不用了,已经改成回调用户定义的callback
	void CancelClick();  //点击cancel响应事件
protected:
	cocos2d::Sprite* _bg; //背影
private:
};
#endif

BDMessageBox.cpp

#include "BDMessageBox.h"
#include "LanguageString.h"

#define TAG_TOAST_LABEL 1130
#define TAG_TOAST_LAYER 1131

using namespace std;
USING_NS_CC;

BDMessageBox::BDMessageBox()
{

}

BDMessageBox::~BDMessageBox()
{

}

bool BDMessageBox::initWithNode(cocos2d::Node* node,const ccMenuCallback& callback )
{
	if (!Layer::init())
	{
		return false;
	}
	//背景
	_bg = Sprite::create("store/msgBoxBg.png");
	this->addChild(_bg);

	Size size = _bg->getContentSize();
	this->setContentSize(size);

	//显示的信息
	//_labelExplain = Label::create(Explain,"Helvetica-Bold",16,Size(200,100),TextHAlignment::LEFT);
	Label* label = Label::create(msg.c_str(),15,TextHAlignment::LEFT,TextVAlignment::CENTER);
	label->setColor(Color3B::YELLOW);
	label->ignoreAnchorPointForPosition(false); //开启锚点有效,不设false就是label被addChild后锚点不是原来的锚点而变为(0,0)左下角,设为false还是取原来的锚点
	label->setAnchorPoint(Vec2::ANCHOR_MIDDLE); //锚点在中间
	label->setTag(TAG_TOAST_LABEL);
	label->setPosition(Vec2(size.width/2,size.height/2));

	_bg->addChild(label);

	//CallFunc* call = CallFunc::create(callback);
	//确认提交按钮
	MenuItemImage* btnOK = MenuItemImage::create("Store/okBtn.png",callback);
	btnOK->setScale(0.8f);
	Menu* btnOkMenu = Menu::create(btnOK,NULL);
	btnOkMenu->setPosition(Vec2(size.width*0.3f,size.height*0.25f));
	_bg->addChild(btnOkMenu,2);

	//取消按钮
	MenuItemImage* btnCancel = MenuItemImage::create("Store/cancelBtn.png","Store/cancelBtnDown.png","Store/cancelBtn.png",CC_CALLBACK_0(BDMessageBox::CancelClick,this));
	btnCancel->setScale(0.8f);
	Menu* btnCancelMenu = Menu::create(btnCancel,NULL);
	btnCancelMenu->setPosition(Vec2(size.width*0.7f,size.height*0.25f));
	_bg->addChild(btnCancelMenu,2);

	//初始设置小
	_bg->setScale(0.01f);
	_bg->setVisible(false);

	node->addChild(this,5);
	this->setPosition(point);

	return true;
}
BDMessageBox* BDMessageBox::createToast(cocos2d::Node* node,const ccMenuCallback& callback)
{
	BDMessageBox* pRet = new BDMessageBox();
	if (pRet && pRet->initWithNode(node,msg,time,point,callback))
	{
		return pRet;
	}
	else
	{
		delete pRet;
		pRet = nullptr;
		return pRet;
	}
}

void BDMessageBox::onEnter()
{
	Layer::onEnter();
	_bg->setVisible(true);
	//对话框由小到大
	Sequence *seq = Sequence::create(EaseElasticIn::create(ScaleTo::create(0.5f,1.1f,1.1f)),EaseElasticOut::create(ScaleTo::create(0.2f,1.f)),NULL);
	_bg->runAction(seq);
}

void BDMessageBox::OkClick()
{

}

void BDMessageBox::CancelClick()
{
	CallFunc* removeC = CallFunc::create([this]()   //飘出后要删除掉自身
	{
		_bg->setVisible(false);
		this->removeFromParentAndCleanup(true);    //这里CallFunc参数是lambda表达式,捕获参数是类自身
	});

	Sequence *seq = Sequence::create(EaseElasticOut::create(ScaleTo::create(0.5f,0.01f,0.01f)),removeC,nullptr);
	_bg->runAction(seq);
}
原文链接:https://www.f2er.com/cocos2dx/340958.html

猜你在找的Cocos2d-x相关文章