cocos2dx3.3 便条提示语功能简单实现

=====================头文件===

#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!!!");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值