您的位置:首页 > 移动开发 > Cocos引擎

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

2014-05-06 17:37 204 查看
用法简单 直接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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐