coco2dx3.1 d出框实现

coco2dx3.1 d出框实现,第1张

概述cocos2dx d出框 网上有好多实现  但是都不是想要的 此d出框功能:自定义添加按钮 title  内容  模态实现 直接上代码: 头文件: #ifndef _POP_LAYER_H_#define _POP_LAYER_H_#include "cocos2d.h"#include "extensions/cocos-ext.h"USING_NS_CC;using namesp

cocos2dx d出框 网上有好多实现 但是都不是想要的

此d出框功能:自定义添加按钮 Title 内容 模态实现

直接上代码:

头文件:

#ifndef _POP_LAYER_H_#define _POP_LAYER_H_#include "cocos2d.h"#include "extensions/cocos-ext.h"USING_NS_CC;using namespace cocos2d::extension;class PopupLayer: public cclayer{public:	PopupLayer();	~PopupLayer();	virtual bool init();	CREATE_FUNC(PopupLayer);	//voID registerWithtouchdispatcher(voID);	//bool ontouchBegan(cocos2d::CCtouch *ptouch,cocos2d::CCEvent *pEvent);	static PopupLayer* create(const char* backgroundImage);	voID setTitle(const char* Title,int Fontsize = 20);	voID setContentText(const char* text,int Fontsize = 20,int padding = 50,int paddinttop = 100);	voID setCallbackFunc(Ref* 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(Ref* pSender);	bool ontouchBegan(touch* touch,Event* event);	voID ontouchmoved(touch* touch,Event* event);	voID ontouchended(touch* touch,Event* event);private:	// 文字内容两边的空白区	int m_contentpadding;	int m_contentpaddingtop;	CCObject* m_callbackListener;	SEL_CallFuncN m_callback;	CC_SYNTHESIZE_RETAIN(Menu*,m__pMenu,Menubutton);	CC_SYNTHESIZE_RETAIN(Sprite*,m__sfBackGround,SpriteBackGround);	CC_SYNTHESIZE_RETAIN(Scale9Sprite*,m__s9BackGround,Sprite9BackGround);	CC_SYNTHESIZE_RETAIN(cclabelTTF*,m__ltTitle,LabelTitle);	CC_SYNTHESIZE_RETAIN(cclabelTTF*,m__ltContentText,LabelContentText);};#endif;

类名字 随便起的 大家就不要吐槽了………………


cpp文件:

#include "Poplayer.h"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);}bool PopupLayer::init(){	bool bRef = false;	do{		CC_BREAK_IF(!cclayer::init());		this->setContentSize(CCSizeZero);		// 初始化需要的 Menu		Ccmenu* menu = Ccmenu::create();		menu->setposition(CCPointZero);		setMenubutton(menu);		settouchEnabled(true);		auto Listener = EventListenertouchOneByOne::create();		Listener->setSwallowtouches(true);		Listener->ontouchBegan = CC_CALLBACK_2(PopupLayer::ontouchBegan,this);		Listener->ontouchmoved = CC_CALLBACK_2(PopupLayer::ontouchmoved,this);		Listener->ontouchended = CC_CALLBACK_2(PopupLayer::ontouchended,this);		_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);		bRef = true;	}while(0);	return bRef;}bool PopupLayer::ontouchBegan(touch* ptouch,Event* event){	cclog("PopupLayer touch");	return true;}voID PopupLayer::ontouchmoved(touch* touch,Event* event){	log("move");}voID PopupLayer::ontouchended(touch* touch,Event* event){	log("ended");}PopupLayer* PopupLayer::create(const char *backgroundImage){	PopupLayer* ml = PopupLayer::create();	ml->setSpriteBackGround(Sprite::create(backgroundImage));	ml->setSprite9BackGround(Scale9Sprite::create(backgroundImage));	return ml;}voID PopupLayer::setTitle(const char *Title,int Fontsize){	cclabelTTF* ltfTitle = LabelTTF::create(Title,"Arial",Fontsize);	setLabelTitle(ltfTitle);}voID PopupLayer::setContentText(const char *text,int Fontsize,int padding,int paddingtop){	cclabelTTF* ltf = cclabelTTF::create(text,Fontsize);	setLabelContentText(ltf);	m_contentpadding = padding;	m_contentpaddingtop = paddingtop;}voID PopupLayer::setCallbackFunc(Ref* target,SEL_CallFuncN callfun){	m_callbackListener = target;	m_callback = callfun;    }bool PopupLayer::addbutton(const char *normalimage,const char *selectedImage,const char *Title,int tag){	CCSize winSize = CCDirector::sharedDirector()->getWinSize();	CCPoint pCenter = ccp(winSize.wIDth / 2,winSize.height / 2);	// 创建图片菜单按钮	CcmenuItemImage* menuImage = CcmenuItemImage::create(normalimage,selectedImage,this,menu_selector(PopupLayer::buttonCallback));	menuImage->setTag(tag);	menuImage->setposition(pCenter);	// 添加文字说明并设置位置	CCSize imenu = menuImage->getContentSize();	cclabelTTF* ttf = cclabelTTF::create(Title,20);	ttf->setcolor(ccc3(0,0));	ttf->setposition(ccp(imenu.wIDth / 2,imenu.height / 2));	menuImage->addChild(ttf);	getMenubutton()->addChild(menuImage);	return true;}voID PopupLayer::buttonCallback(Ref* pSender){	Node* node = (Node*)pSender;	cclog("touch tag: %d",node->getTag());	if (m_callback && m_callbackListener){		(m_callbackListener->*m_callback)(node);	}	int tag = node->getTag();	switch(tag)	{	case 0:		{		}	case 1:		{		}	default:		break;	}	this->removeFromParent();}voID PopupLayer::onEnter(){	cclayer::onEnter();	CCSize winSize = CCDirector::sharedDirector()->getWinSize();	CCPoint pCenter = ccp(winSize.wIDth / 2,winSize.height / 2);	CCSize contentSize;	// 设定好参数,在运行时加载	if (getContentSize().equals(CCSizeZero)) {		getSpriteBackGround()->setposition(ccp(winSize.wIDth / 2,winSize.height / 2));		this->addChild(getSpriteBackGround(),0);		contentSize = getSpriteBackGround()->getTexture()->getContentSize();	} else {		Scale9Sprite *background = getSprite9BackGround();		background->setContentSize(getContentSize());		background->setposition(ccp(winSize.wIDth / 2,winSize.height / 2));		this->addChild(background,0);		contentSize = getContentSize();	}	// 添加按钮,并设置其位置	this->addChild(getMenubutton());	float btnWIDth = contentSize.wIDth / (getMenubutton()->getChildrenCount() + 1);		 auto array =  getMenubutton()->getChildren();	Ref* pObj = NulL;	for(int i = 0;i< array.capacity();i++)	{		Node* node = array.at(i);		node->setposition(ccp( winSize.wIDth / 2 - contentSize.wIDth / 2 + btnWIDth * (i + 1),winSize.height / 2 - contentSize.height / 3));	}		// 显示对话框标题	if (getLabelTitle()){		getLabelTitle()->setposition(ccpAdd(pCenter,ccp(0,contentSize.height / 2 - 35.0f)));		this->addChild(getLabelTitle());	}	// 显示文本内容	if (getLabelContentText()){		cclabelTTF* ltf = getLabelContentText();		ltf->setposition(ccp(winSize.wIDth / 2,winSize.height / 2));		ltf->setDimensions(CCSizeMake(contentSize.wIDth - m_contentpadding * 2,contentSize.height - m_contentpaddingtop));		ltf->setHorizontalAlignment(kCCTextAlignmentleft);		this->addChild(ltf);	}	// d出效果	CCAction* popupLayer = 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(popupLayer);}voID PopupLayer::onExit(){	cclog("popup on exit.");	cclayer::onExit();}

使用方法:
PopupLayer* pl = PopupLayer::create("popuplayer/BackGround.png");	// ContentSize 是可选的设置,可以不设置,如果设置把它当作 9 图缩放	pl->setContentSize(CCSizeMake(400,350));	pl->setTitle("test");	pl->setContentText(G2U("你好,d出框"),20,60,250);	// 设置回调函数,回调传回一个 CCNode 以获取 tag 判断点击的按钮	// 这只是作为一种封装实现,如果使用 delegate 那就能够更灵活的控制参数了	pl->setCallbackFunc(this,callfuncN_selector(LoginScene::buttonCallback));	// 添加按钮,设置图片,文字,tag 信息	pl->addbutton("popuplayer/pop_button.png","popuplayer/pop_button.png",G2U("确定"),0);	pl->addbutton("popuplayer/pop_button.png",G2U("取消"),1);	// 添加到当前层	this->addChild(pl);
G2U是 gbk转UTF_8 要不然的话 中文会乱码

函数如下:

char* G2U(const char* gb2312){	int len = MultiBytetoWIDeChar(CP_ACP,gb2312,-1,NulL,0);	wchar_t* wstr = new wchar_t[len+1];	memset(wstr,len+1);	MultiBytetoWIDeChar(CP_ACP,wstr,len);	len = WIDeCharToMultiByte(CP_UTF8,NulL);	char* str = new char[len+1];	memset(str,len+1);	WIDeCharToMultiByte(CP_UTF8,str,len,NulL);	if(wstr) delete[] wstr;	return str;}
希望大家喜欢 总结

以上是内存溢出为你收集整理的coco2dx3.1 d出框实现全部内容,希望文章能够帮你解决coco2dx3.1 d出框实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存