用法简单 直接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();
}