之前在一个闯关游戏中第一次接触飘字效果,因为那个游戏没有发教程,所以在这里介绍下飘字效果
~~~
class FlowWord :public Node
{
public:
FlowWord();
~FlowWord();
//创建和初始化 飘字
static FlowWord* create();
bool init();
//显示飘字
void showFlowWord(const char* text, Point pos, ActionInterval* flowWord);
//显示飘字结束
void showEnd();
// ActionInterval* beginFlowWord();
// ActionInterval* propFlowWord();
ActionInterval* otherFlowWord();
protected:
private:
Label* m_textLabel;
};
~~~
在这个类中,内置了几种常见的飘字动作和一个飘字函数(需要传入 飘字内容、位置、动作)
其实现如下
~~~
bool FlowWord::init()
{
m_textLabel = Label::createWithTTF("", "fonts/DFPShaoNvW5-GB.ttf", 25);
m_textLabel->setColor(ccc3(CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255));
m_textLabel->setAnchorPoint(ccp(1, 0));
m_textLabel->setVisible(false);
this->addChild(m_textLabel);
return true;
}
//显示飘字
void FlowWord::showFlowWord(const char* text, Point pos, ActionInterval* flowWord)
{
m_textLabel->setPosition(pos);
m_textLabel->setString(text);
m_textLabel->setVisible(true);
m_textLabel->runAction(flowWord);
}
//显示飘字结束
void FlowWord::showEnd()
{
CCLOG("showWord End!");
m_textLabel->setVisible(false);
m_textLabel->removeFromParentAndCleanup(true);
}
#if 0
//游戏开始飘字
ActionInterval* FlowWord::beginFlowWord()
{
//放大缩小
ActionInterval* m_scaleLarge = ScaleTo::create(2.0f, 2.5, 2.5);
ActionInterval* m_scaleSmall = ScaleTo::create(2.0f, 0.5, 0.5);
//倾斜
ActionInterval* m_skew = SkewTo::create(2.0f, 180, 0);
ActionInterval* m_skewBack = SkewTo::create(2.0f, 0, 0);
//组合动作
ActionInterval* m_action = Spawn::create(m_scaleLarge, m_skew, NULL);
ActionInterval* m_actionBack = Spawn::create(m_scaleSmall, m_skewBack, NULL);
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(FlowWord::showEnd));
ActionInterval* flow = Sequence::create(m_action, m_actionBack, callFunc, NULL);
return flow;
}
//获得道具飘字
ActionInterval* FlowWord::propFlowWord()
{
//放大缩小
ActionInterval* m_scaleLarge = ScaleTo::create(2.0f, 2.5, 2.5);
ActionInterval* m_scaleSmall = ScaleTo::create(2.0f, 0.5, 0.5);
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(FlowWord::showEnd));
ActionInterval* flow = Sequence::create(m_scaleLarge, m_scaleSmall, callFunc, NULL);
return flow;
}
#endif
//其它飘字
ActionInterval* FlowWord::otherFlowWord()
{
//移位
ActionInterval* m_move1 = MoveBy::create(2.0f, ccp(30, 30));
ActionInterval* m_move2 = MoveBy::create(2.0f, ccp(-30, -30));
ActionInterval* m_move3 = MoveBy::create(2.0f, ccp(30, -30));
ActionInterval* m_move4 = MoveBy::create(2.0f, ccp(-30, 30));
//放大缩小
ActionInterval* m_scale1 = ScaleTo::create(2.0f, CCRANDOM_0_1() * 4, CCRANDOM_0_1() * 4);
ActionInterval* m_scale2 = ScaleTo::create(2.0f, CCRANDOM_0_1() * 4, CCRANDOM_0_1() * 4);
ActionInterval* m_scale3 = ScaleTo::create(2.0f, CCRANDOM_0_1() * 4, CCRANDOM_0_1() * 4);
ActionInterval* m_scale4 = ScaleTo::create(2.0f, CCRANDOM_0_1() * 4, CCRANDOM_0_1() * 4);
ActionInterval* m_action1 = Spawn::create(m_move1, m_scale1, NULL);
ActionInterval* m_action2 = Spawn::create(m_move2, m_scale2, NULL);
ActionInterval* m_action3 = Spawn::create(m_move3, m_scale3, NULL);
ActionInterval* m_action4 = Spawn::create(m_move4, m_scale4, NULL);
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(FlowWord::showEnd));
ActionInterval* flow = Sequence::create(m_action1, m_action2, m_action3, m_action4, callFunc, NULL);
return flow;
}
~~~