飘字的原理很简单,就是让一个Label不管它是什么颜色,让它上升到一定的高度,当然在
上升的过程中,要做一些左右移动啊,扭动啊等等动作,最后在上升结束后消失。当然要当时
被血量的信息。
FlyWord.h
//
// FlyWord.h
// Fight
//
// Created by Cytzrs on 15/2/4.
//
//
#ifndef __Fight__FlyWord__
#define __Fight__FlyWord__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class FlyWord : public Node
{
public:
static FlyWord* create(const char *word,const int fontSize,Point begin);
bool init(const char *word,const int fontSize,Point begin);
void Flying();
void Flyend();
Point _begin;
Label* m_plabel;
};
#endif /* defined(__Fight__FlyWord__) */
FlyWord.cpp
//
// FlyWord.cpp
// Fight
//
// Created by Cytzrs on 15/2/4.
//
//
#include "FlyWord.h"
FlyWord* FlyWord::create(const char *word,const int fontSize,Point begin){
FlyWord* ret = new FlyWord();
//这样写更安全一些
if(ret && ret->init(word,fontSize,begin)){
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);//安全删除
return nullptr;
}
bool FlyWord::init(const char *word,const int fontSize,Point begin){
if(!CCNode::init()){
return false;
}
//初始化
_begin = begin;
m_plabel = Label::createWithSystemFont(word,"fonts/Marker Felt",fontSize);
//设置颜色
Color3B RGB;
RGB.r=255;
RGB.g=0;
RGB.b=0;
m_plabel->setColor(RGB);
this->addChild(m_plabel);
this->setPosition(Vec2(begin.x,begin.y));
//初始化完成不可见
this->setVisible(false);
return true;
}
//文字从下到上飘动
void FlyWord::Flying()
{
auto moveact=MoveBy::create(0.5f,Vec2(0,70));//0.5秒向上移动70
//创建回调动作,文字飘动完后
auto callFunc=CallFunc::create(CC_CALLBACK_0(FlyWord::Flyend, this));
//创建连续动作
auto act=Sequence::create(moveact,callFunc,NULL);
//设置可见性
this->setVisible(true);
this->runAction(act);
}
void FlyWord::Flyend()
{
//完成之后设置隐藏
this->setVisible(false);
//回复原位
this->setPosition(Vec2(_begin.x,_begin.y));
}
使用方法:
wen_zi=FlyWord::create("-10",30,Vec2(0,0));//放在当前怪物的锚点位置,
this->addChild(wen_zi,2);
然后再monster受伤时调用wen_zi->Flying()来播放动作。
PS:多写博客,帮助自己,方便他人!