Cocos2d-x 自制提示框(就是游戏登陆失败之类的提示一下消失效果)

本文介绍如何使用Cocos2d-x创建提示框组件,并提供了代码实现,包括初始化、显示和移除提示框的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


用法简单 直接create就OK了,不用添加在layer上的,感觉改成静态方法会更好的,暂时就这样先吧,静态有空在改。


直接代码了,没啥好讲的


.h文件

#pragma once

#include "cocos2d.h"

USING_NS_CC;

class HintBox : public CCLayerColor
{
public:
	HintBox(void);
	~HintBox(void);

	static HintBox* createBox(const char* content = "NULL", const ccColor4B& color = ccc4(200,200,200,200), float width = 400.0f, float high = 100.0f);

	virtual bool init(const char* content, const ccColor4B& color, float width, float high);

	virtual void onEnter();

	virtual void onExit();

	void deleteLayer(CCNode* obj);

};

.cpp 文件


#include "HintBox.h"

HintBox::HintBox(void)
{
}


HintBox::~HintBox(void)
{
}


HintBox* HintBox::createBox(const char* content, const ccColor4B& color, float width, float high)
{
	HintBox* box = new HintBox();
	if(box && box->init(content, color, width, high))
	{
		return box;
	}
	return NULL;
}

bool HintBox::init(const char* content, const ccColor4B& color, float width, float high)
{
	CCLayerColor::initWithColor(color);

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

	this->setContentSize(CCSize(width, high));

	CCLabelTTF* ttf = CCLabelTTF::create(content, "黑体", 30);

	this->addChild(ttf);

	ttf->setPosition(ccp(width/2, high/2));

	CCDirector::sharedDirector()->getRunningScene()->addChild(this, 100);

	this->setPosition(ccp(size.width/2-width/2, size.height/2));

	return true;
}

void HintBox::onEnter()
{
	CCLayerColor::onEnter();

	this->runAction(CCSequence::create( CCMoveBy::create(0.8f,ccp(0,50)), CCCallFuncN::create(this,callfuncN_selector(HintBox::deleteLayer)), NULL ));


}

void HintBox::onExit()
{
	CCLayerColor::onExit();

}

void HintBox::deleteLayer(CCNode* obj)
{
	this->removeFromParent();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值