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