现在我们运用我们已经学过的知识实现一个类来完成游戏中主角或者障碍物受到攻击时闪烁提示信息。
没有就新的东西,所以菜鸟们都可以看懂的,另外我还要特意强调一下,小的方面游戏需要创新,游戏开发中的效果需要创新,创新不是多么的难,运用简单的知识就能创新就像我们的这个类。
废话说多了,直接看代码吧:
class FlowWord : public CCNode {
public:
static FlowWord* create();
bool init();
public:
void showWord(const char* text, CCPoint pos);
private:
CCLabelTTF* m_textLab;
void flowEnd();
};
FlowWord* FlowWord::create() {
FlowWord* flowWord = new FlowWord();
if(flowWord && flowWord->init()) {
flowWord->autorelease();
}
else {
CC_SAFE_DELETE(flowWord);
}
return flowWord;
}
bool FlowWord::init() {
m_textLab = CCLabelTTF::create("", "Arial", 35);
m_textLab->setColor(ccc3(255, 0, 0));
m_textLab->setAnchorPoint(ccp(1, 0));
m_textLab->setVisible(false);
this->addChild(m_textLab);
return true;
}
void FlowWord::showWord( const char* text, CCPoint pos ) {
m_textLab->setString(text);
m_textLab->setPosition(pos);
m_textLab->setVisible(true);
/* 组合两个动作,放大后缩小 */
CCActionInterval* scaleLarge = CCScaleTo::create(0.3f, 2.5f, 2.5f);
CCActionInterval* scaleSmall = CCScaleTo::create(0.5f, 0.5f, 0.5f);
CCCallFunc* callFunc = CCCallFunc::create(this, callfunc_selector(FlowWord::flowEnd));
CCActionInterval* actions = CCSequence::create(scaleLarge, scaleSmall, callFunc, NULL);
m_textLab->runAction(actions);
}
void FlowWord::flowEnd() {
/* 动作结束,从父节点中删除自身 */
m_textLab->setVisible(false);
m_textLab->removeFromParentAndCleanup(true);
}
现在我们就可以直接用它来实现我们的效果了,在需要的地方添加如下代码:
FlowWord *flowword = FlowWord::create();
this->addChild(flowword);
flowword->showWord("-15",GetSprite()->getPosition());
效果如下:
版权声明:本文为vanquishedzxl原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。