[cocos2dx动作]CCLabel类数字变化动作

cococs2dx的CCLabel类的数字变化动作

介绍: 简单的数字变化动作(适用于CCLabel类对象, 包括CCLabelTTF, CCLabelAtlas, CCLabelBMFont等等)

 

//注意 demo中支持的是CCLabelTTF ,把dynamic_cast相应的代码改成如下即可支持多种CCLabel对象

CCLabelProtocol *pLabel = dynamic_cast<CCLabelProtocol*>(m_pTarget); 

 

demo下载地址:http://pan.baidu.com/s/1c09r3jU 环境:cocos2dx 2.14, xcode 5.0.2

版权木有,欢迎转载与各种使用修改~

源代码:

//头文件 CCLabelChange.h

//  CCLabelChange.h

#ifndef __testLabelChange__CCLabelChange__
#define __testLabelChange__CCLabelChange__

#include "cocos2d.h"
USING_NS_CC;

class CCLabelChange : public CCActionInterval
{
public:
    CCLabelChange();
    
public:
    /** creates the action */
    static CCLabelChange* create(float duration, int fromNum, int toNum);
    
    /** initializes the action */
    bool initWithDuration(float duration, int fromNum, int toNum);
    
    virtual CCObject* copyWithZone(CCZone* pZone);
    virtual void startWithTarget(CCNode *pTarget);
    virtual CCActionInterval* reverse(void);
    virtual void update(float time);
    
protected:

    int m_nFromNum;
    int m_nToNum;
};

//cpp文件 CCLabelChange.cpp

#include "CCLabelChange.h"

CCLabelChange::CCLabelChange():
m_nFromNum(0),
m_nToNum(0)
{
    
}

CCLabelChange* CCLabelChange::create(float duration, int fromNum, int toNum)
{
    CCLabelChange *pRet = new CCLabelChange();
    pRet->initWithDuration(duration, fromNum, toNum);
    pRet->autorelease();
    
    return pRet;
}

bool CCLabelChange::initWithDuration(float duration, int fromNum, int toNum)
{
    if (CCActionInterval::initWithDuration(duration))
    {
        m_nFromNum = fromNum;
        m_nToNum = toNum;
        return true;
    }
    
    return false;
}

CCObject* CCLabelChange::copyWithZone(CCZone *pZone)
{
    CCZone* pNewZone = NULL;
    CCLabelChange* pCopy = NULL;
    if(pZone && pZone->m_pCopyObject)
    {
        //in case of being called at sub class
        pCopy = (CCLabelChange*)(pZone->m_pCopyObject);
    }
    else
    {
        pCopy = new CCLabelChange();
        pZone = pNewZone = new CCZone(pCopy);
    }
    
    CCActionInterval::copyWithZone(pZone);
    
    pCopy->initWithDuration(m_fDuration, m_nFromNum, m_nToNum);
    
    CC_SAFE_DELETE(pNewZone);
    return pCopy;
}

void CCLabelChange::startWithTarget(CCNode *pTarget)
{
    CCActionInterval::startWithTarget(pTarget);
    
    CCLabelProtocol *pLabel = dynamic_cast<CCLabelProtocol*>(m_pTarget);
    if (pLabel)
    {
        CCString *numStr = CCString::createWithFormat("%i", m_nFromNum);
        pLabel->setString(numStr->getCString());
    }

}

CCActionInterval* CCLabelChange::reverse(void)
{
    return CCLabelChange::create(m_fDuration, m_nToNum, m_nFromNum);
}


void CCLabelChange::update(float t)
{
  
    CCLabelProtocol *pLabel = dynamic_cast<CCLabelProtocol*>(m_pTarget);
    if (pLabel)
    {
        int num = m_nFromNum + (m_nToNum - m_nFromNum) * t;
        CCString *numStr = CCString::createWithFormat("%i", num);
        pLabel->setString(numStr->getCString());
    }

}

//使用方法

        CCLabelTTF *label = CCLabelTTF::create("", "Arial", 30);
        
        float duration = 5.0f;
        int fromNum = 20; //开始数字
        int toNum = 1000; //结束数字
        
        CCLabelChange *pChange = CCLabelChange::create(duration, fromNum, toNum);
        
        label->runAction(pChange);

 

posted @ 2014-01-23 15:59  CreeperChange  阅读(2589)  评论(0编辑  收藏  举报