cocos2d-x实现飞字效果

来源:互联网 发布:ios看书软件推荐 编辑:程序博客网 时间:2024/06/03 05:09

cocos2d-x实现飞字效果

实现了三种飘字效果,可以在暴击时加一些特效和中文字“暴击”

////  LQFlyNumber.h//#ifndef __LQFlyNumber_H__#define __LQFlyNumber_H__#include "cocos2d.h"using namespace cocos2d;//定义飘字类型typedef enum{    klq_flaytype_normal=0,   //正常    klq_flaytype_break =1,   //暴击  放大字体,红色    klq_flaytype_resolve=2,  //分解  缩小字体,黄色    klq_flaytype_other=3     //???} LQFlyNumberTypeEnum;/* 示例1: LQFlyNumber* fly = LQFlyNumber::create("111"); fly->setPosition(ccpSize(this->getContentSize(),0.5)); addChild(fly); fly->startAnimation();  示例2:  LQFlyNumber* fly = LQFlyNumber::create(value,true); fly->setPosition(ccpSize(this->getContentSize(),0.5)); fly->setNumberType(klq_flaytype_break); addChild(fly); */class LQFlyNumber : public CCSprite{private:    const char* m_curValueStr; //当前串值    CCLabelBMFont *m_label;public:    static LQFlyNumber* create(int num, bool isSign);  //数字 带符号    static LQFlyNumber* create(const char* str);    LQFlyNumber();    ~LQFlyNumber();        bool initWithString(const char* str);    void startAnimation();    void removeObject();        void setCurValueStr(const char* curValue);    void drawString(const char* curValue);        //飘字类型    CC_SYNTHESIZE(LQFlyNumberTypeEnum, m_numbertype, NumberType);        //用于允许 "static create()" constructor ( node() deprecated )    CREATE_FUNC(LQFlyNumber);};#endif


////  LQFlyNumber.m//#include "LQFlyNumber.h"LQFlyNumber* LQFlyNumber::create(int num, bool isSign){    std::string str;    const char* sign = "";    if(isSign){        sign = (num>0) ? "+" : "-";    }    LQ::stringf(str, "%s%d", sign,  num);        LQFlyNumber* numsprite = LQFlyNumber::create();    numsprite->initWithString(str.c_str());    return numsprite;}LQFlyNumber* LQFlyNumber::create(const char* str){    LQFlyNumber* numsprite = LQFlyNumber::create();    numsprite->initWithString(str);    return numsprite;}LQFlyNumber::LQFlyNumber():    m_label(0), m_numbertype(klq_flaytype_normal){    }LQFlyNumber::~LQFlyNumber(){    //CC_SAFE_RELEASE_NULL(m_label);}bool LQFlyNumber::initWithString(const char* str){    this->setCurValueStr(str);    return true;}/** sets a new curValue to the CCSprite. */void LQFlyNumber::setCurValueStr(const char* curValue){    m_curValueStr = curValue;    this->drawString(curValue);}void LQFlyNumber::drawString(const char* str){    if (m_label==NULL){        const char* fontname = "flynumber.fnt";  //numberFontName(numberFontSize()).c_str()        m_label = CCLabelBMFont::create(m_curValueStr,fontname,300, kCCTextAlignmentCenter);        m_label->setColor(ccRED);        m_label->setPosition(CCPointZero);        this->addChild(m_label,1);    }else        m_label->setString(m_curValueStr);}void LQFlyNumber::startAnimation(){    //调用changeScene函数    switch (m_numbertype) {         case klq_flaytype_break:{ //暴击  放大字体,红色            CCFiniteTimeAction* endcall = CCCallFunc::create(this, callfunc_selector(LQFlyNumber::removeObject));            m_label->setColor(ccRED);            CCFiniteTimeAction* scaleby1 = CCScaleBy::create(0.5f,1.8f);            CCFiniteTimeAction* moveby1 = CCMoveBy::create(0.5f,ccp(40, 70));            CCFiniteTimeAction* delay1 = CCDelayTime::create(0.2f);            CCFiniteTimeAction* fadeout1 = CCFadeOut::create(0.2f);            CCSequence* sequAct1 = CCSequence::create(delay1, fadeout1, endcall, NULL);            CCSpawn* spAct1 = CCSpawn::create(sequAct1, moveby1, scaleby1, NULL);//            this->runAction( spAct1 );            break;        }        case klq_flaytype_resolve:{ //分解  缩小字体,黄色            CCFiniteTimeAction* endcall = CCCallFunc::create(this, callfunc_selector(LQFlyNumber::removeObject));            this->setScale(0.7f);            m_label->setColor(ccORANGE);            CCFiniteTimeAction* moveby1 = CCMoveBy::create(0.5f,ccp(20, 50));            CCFiniteTimeAction* delay1 = CCDelayTime::create(0.2f);            CCFiniteTimeAction* fadeout1 = CCFadeOut::create(0.2f);            CCSequence* sequAct1 = CCSequence::create(delay1, fadeout1, endcall, NULL);            CCSpawn* spAct1 = CCSpawn::create(sequAct1, moveby1, NULL);//            this->runAction( spAct1 );            break;        }        default:{ //正常            CCFiniteTimeAction* endcall = CCCallFunc::create(this, callfunc_selector(LQFlyNumber::removeObject));            CCFiniteTimeAction* moveby1 = CCMoveBy::create(0.5f,ccp(20, 50));            CCFiniteTimeAction* delay1 = CCDelayTime::create(0.2f);            CCFiniteTimeAction* fadeout1 = CCFadeOut::create(0.2f);            CCSequence* sequAct1 = CCSequence::create(delay1, fadeout1, endcall, NULL);            CCSpawn* spAct1 = CCSpawn::create(sequAct1, moveby1, NULL);//            this->runAction( spAct1 );            break;        }    }}void LQFlyNumber::removeObject(){    stopAllActions();    removeAllChildrenWithCleanup(true);}


颜色好像不起作用,没有时间研究

其中用到了一个函数

//=============================================================================// string result;// stringf(result, "string length is %d", 0);// 输出结果:result = string length is 0.static void stringf(string& stgt, const char *sformat, ... ){    int size = 100;    va_list ap;    while (1) {        stgt.resize(size);        va_start(ap, sformat);        int n = vsnprintf((char *)stgt.c_str(), size, sformat, ap);        va_end(ap);        if (n > -1 && n < size) {            stgt.resize(n);            return;        }        if (n > -1)            size = n + 1;        else            size *= 2;    }};




原创粉丝点击