=====================头文件===
#ifndef __H_NOTETIP_H__
#define __H_NOTETIP_H__
#include "Global.h"
class NoteTip : public Layer
{
public:
NoteTip();
~NoteTip();
CREATE_FUNC(NoteTip);
static void show(const char *content);
virtual bool init();
virtual void onEnter();
virtual void onExit();
};
#endif
============cpp文件======
#include "NoteTip.h"
USING_NS_CC;
NoteTip::NoteTip()
{
}
NoteTip::~NoteTip()
{
}
bool NoteTip::init()
{
if (!Layer::init())
{
return false;
}
return true;
}
void NoteTip::show(const char *content)
{
NoteTip *noteTip = NoteTip::create();
Size visibelSize = Director::getInstance()->getVisibleSize();
Label *label = Label::create(content, "", 30);
label->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
ui::Scale9Sprite *sp = ui::Scale9Sprite::create("general/img/NoteTip.png");
float fontSize = label->getSystemFontSize();
float spWidth = label->getStringLength()*(fontSize / 2);
const float minWidth = 250;
sp->setContentSize(Size((spWidth > minWidth ? spWidth : minWidth) + 20, fontSize + 10));
sp->setCapInsets(Rect(6, 6, 1, 1));
sp->setScale9Enabled(true);
sp->setPosition(visibelSize.width/2, visibelSize.height/ 2.5f);
sp->setTag(100);
label->setPosition(sp->getContentSize()/2);
sp->addChild(label);
noteTip->addChild(sp);
auto scene = Director::getInstance()->getRunningScene();
scene->addChild(noteTip);
}
void NoteTip::onEnter()
{
Layer::onEnter();
ui::Scale9Sprite *sp = (ui::Scale9Sprite*)this->getChildByTag(100);
if (sp)
{
auto seq = Sequence::create(
MoveBy::create(1.0f, Vec2(0, 180)),
DelayTime::create(0.2f),
FadeOut::create(0.2f),
CallFunc::create([&](){this->removeFromParent();}),
nullptr
);
sp->runAction(seq);
}
}
void NoteTip::onExit()
{
Layer::onExit();
}
============调用方法====
NoteTip::show("Just is a TEST!!!");