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(),
m_nToNum()
{ } 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", );

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

最新文章

  1. XPath
  2. 【Away3D代码解读】(四):主要模块简介
  3. Programme skills
  4. jdk-动态代理
  5. RTUILabel+正则表达式
  6. HDFS写入和读取流程
  7. 递推,大数存储E - Order Count
  8. &lt;c:if&gt;判断两个&lt;c:forEach&gt;里的数据是否相等
  9. WCF服务自我寄宿
  10. Nginx学习系列一搭建环境
  11. Centos下安装Mysql异常
  12. selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项
  13. CSS设置边框、符号、背景样式、链接属性
  14. Leetcode 344.反转字符串 By Python
  15. js获取本机id
  16. Vue属性中带’-‘的处理方式
  17. 07-border(边框)
  18. 无责任Windows Azure SDK .NET开发入门(二):使用Azure AD 进行身份验证
  19. eclipse创建spring boot项目加载不到application.properties配置文件
  20. vux在ISO中异常 this.$vux.confirm.show

热门文章

  1. ASP.NET MVC(一) 什么是Razor
  2. 20151222jquery学习笔记--验证注册表单
  3. 趣拍proguard配置
  4. NET异步调用Webserver
  5. csqlite编译相关配置问题
  6. 计数排序之python 实现源码
  7. HeadFirst设计模式
  8. python 自动化之路 logging日志模块
  9. jQuery iframe 自适应高宽度
  10. Common Configration实验