之前在一個闖關游戲中第一次接觸飄字效果,因為那個游戲沒有發教程,所以在這里介紹下飄字效果
~~~
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;
}
~~~