之前写了个2.14版本的动作变化,见 http://www.cnblogs.com/creeper/p/3531304.html

3.x版本变化了很多,但是核心思想还是没有变化,所以对应3.x版本的改了一下放上来

有空的话把tolua的转换方法也放上来吧:)

  1. #ifndef __MISC_NODE_CCNUMBER_CHANGE_H__
  2. #define __MISC_NODE_CCNUMBER_CHANGE_H__
  3.  
  4. #include <vector>
  5.  
  6. #include "2d/CCAction.h"
  7. #include "2d/CCAnimation.h"
  8. #include "2d/CCActionInterval.h"
  9. #include "base/CCProtocols.h"
  10. #include "base/CCVector.h"
  11.  
  12. //NS_CC_BEGIN
  13. USING_NS_CC;
  14. class Node;
  15. class SpriteFrame;
  16. class EventCustom;
  17.  
  18. class NumberChange : public ActionInterval
  19. {
  20. public:
  21.  
  22. static NumberChange* create(float duration, int fromNum, int toNum);
  23.  
  24. virtual NumberChange* clone() const override;
  25. virtual NumberChange* reverse(void) const override;
  26. virtual void startWithTarget(cocos2d::Node *target) override;
  27. virtual void update(float time) override;
  28.  
  29. CC_CONSTRUCTOR_ACCESS:
  30. NumberChange();
  31. virtual ~NumberChange();
  32.  
  33. /** initializes the action */
  34. bool initWithDuration(float duration, int fromNum, int toNum);
  35.  
  36. protected:
  37. int _fromNum;
  38. int _toNum;
  39.  
  40. private:
  41. CC_DISALLOW_COPY_AND_ASSIGN(NumberChange);
  42. };
  43.  
  44. //NS_CC_END
  45.  
  46. #endif //__MISC_NODE_CCNUMBER_CHANGE_H__
  1. #include "2d/CCActionInterval.h"
  2.  
  3. #include <stdarg.h>
  4.  
  5. #include "2d/CCSprite.h"
  6. #include "2d/CCNode.h"
  7. #include "2d/CCSpriteFrame.h"
  8. #include "2d/CCActionInstant.h"
  9. #include "base/CCDirector.h"
  10. #include "base/CCEventCustom.h"
  11. #include "base/CCEventDispatcher.h"
  12. #include "platform/CCStdC.h"
  13. #include "deprecated/CCString.h"
  14. #include "NumberChange.h"
  15.  
  16. USING_NS_CC;
  17. //NS_CC_BEGIN
  18. NumberChange::NumberChange(){
  19. }
  20.  
  21. NumberChange::~NumberChange(){
  22. }
  23.  
  24. NumberChange* NumberChange::create(float duration, int fromNum, int toNum)
  25. {
  26. NumberChange *ret = new (std::nothrow) NumberChange();
  27. ret->initWithDuration(duration, fromNum, toNum);
  28. ret->autorelease();
  29.  
  30. return ret;
  31. }
  32.  
  33. bool NumberChange::initWithDuration(float duration, int fromNum, int toNum)
  34. {
  35. if (ActionInterval::initWithDuration(duration))
  36. {
  37. _fromNum = fromNum;
  38. _toNum = toNum;
  39. return true;
  40. }
  41.  
  42. return false;
  43. }
  44.  
  45. NumberChange* NumberChange::clone() const
  46. {
  47. // no copy constructor
  48. auto a = new (std::nothrow) NumberChange();
  49. a->initWithDuration(_duration, _fromNum, _toNum);
  50. a->autorelease();
  51. return a;
  52. }
  53.  
  54. void NumberChange::startWithTarget(cocos2d::Node *target)
  55. {
  56. ActionInterval::startWithTarget(target);
  57. LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(target);
  58. if (pLabel)
  59. {
  60. std::string numStr = cocos2d::StringUtils::format("%i", _fromNum);
  61. pLabel->setString(numStr.c_str());
  62. }
  63. }
  64.  
  65. NumberChange* NumberChange::reverse() const
  66. {
  67. return NumberChange::create(_duration, _toNum, _fromNum);
  68. }
  69.  
  70. void NumberChange::update(float t)
  71. {
  72. LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(_target);
  73. if (pLabel)
  74. {
  75. int tempNum = (_toNum - _fromNum) * t;
  76. int num = _fromNum + tempNum;
  77. std::string numStr = cocos2d::StringUtils::format("%i", num);
  78. pLabel->setString(numStr.c_str());
  79. }
  80. }
  81.  
  82. //NS_CC_END

[cocos2dx 3.x]Label类数字变化动作的更多相关文章

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

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

  2. [Quick-x lua]CCLabel类数字变化动作

    之前写了个C++版本的,现在改成lua的, 两者原理是一样,但是动作的执行方式有些微区别 (因为lua无法继承CCActionInterval类,单纯使用lua的话无法调用action的update方 ...

  3. cocos2d-X学习之主要类介绍:动作:CCAction

    引用自:http://www.cnblogs.com/lhming/archive/2012/07/01/2572238.html 类继承图: 主要函数: virtual CCObject *  co ...

  4. 【Cocos2d-X开发学习笔记】第18期:动作类之改变动作对象、函数回调动作以及过程动作的使用

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.改变动作执行对象 CCTargetedAct ...

  5. Cocos2d-x 3.0标签类Label

    Cocos2d-x 3.0后推出了新的标签类Label,这种标签通过使用FreeType[1]来使它在不同的平台上有相同的视觉效果.由于使用更快的缓存代理,它的渲染也将更加快速.Label提供了描边和 ...

  6. Cocos2dx源码赏析(4)之Action动作

    Cocos2dx源码赏析(4)之Action动作 本篇,依然是通过阅读源码的方式来简单赏析下Cocos2dx中Action动画的执行过程.当然,这里也只是通过这种方式来总结下对Cocos2dx引擎的理 ...

  7. 关于SWT中的Label类和Text类

    Label类的继承关系图 Label是SWT中最简单的界面组件,给出他的一个实例如下: public class Label1 { public static void main(String[] a ...

  8. 让数字变化炫酷起来,数字滚动Text组件[Unity]

    让数字滚动起来 上周我的策划又提了样需求,当玩家评分发生变动时,屏幕出现人物评分浮层UI,播放评分数字滚动动画.这类数字滚动需求非常常见,我就按一般思路,将startvalue与endvalue每隔一 ...

  9. cocos2d-x实例学习之常用类及其概要作用

    CCLayer,CCScene CCLayer类对应cocos2d-x引擎里的布景.每个游戏场景中都可以有很多层,每一层负责各自的任务,例如专门负责显示背景.专门负责显示道具和专门负责显示人物角色等. ...

随机推荐

  1. jquery slideDown slideUp 对于table无效

    jquery slideDown slideUp 对于table无效,需要在table外面套一层div才可以使用

  2. eclipse怎么切换SVN的用户

    在用eclipse的时候会经常用到SVN来进行代码的版本控制,为了方便起见,我们会保存密码,从此之后就不会再出现输入或者修改用户名和密码的地方了,这时候想切换用户怎么办,在本地操作的一种方法是删除SV ...

  3. ios code style

    注释 建议使用VVDocumenter插件 多行注释 格式: /** 注释内容 */ 单行注释 格式: ///在对文件.类.函数进行注释时推荐使用多行注释,在函数体内对代码块进行注释时,使用单行注释 ...

  4. JEditorPane中html文档中文乱码解决

    Culturally dependent information in some documents is handled through a mechanism called character e ...

  5. Java实战之03Spring-04Spring的数据库访问

    四.Spring的数据库访问 1.DAO模式 /** * 抽取的一个类 * @author zhy * */ public class JdbcDaoSupport { private QueryRu ...

  6. 16_会话技术_Session案例

    [购物车中的信息保存] [Book.java] package com.Higgin.shopping; public class Book { private String id; private ...

  7. Microsoft Word Regular Expression

    Microsoft Word Regular Expression Word裏的正則表達式-不一樣的符號. 一.Normal Find and Replace 二.Search by using wi ...

  8. OpenJudge 2795 金银岛

    1.链接地址: http://bailian.openjudge.cn/practice/2795/ 2.题目: 总Time Limit: 3000ms Memory Limit: 65536kB D ...

  9. Poj/OpenJudge 1042 Gone Fishing

    1.链接地址: http://bailian.openjudge.cn/practice/1042/ http://poj.org/problem?id=1042 2.题目: Gone Fishing ...

  10. Linux C 程序 两变量值交换(FIVE)

    example:1.运算符: #include<stdio.h> int main(){ int a , b , c ,d ; a = ; b = a++;//先赋值给b,a再自增 c = ...