首页 > 代码库 > [cocos2dx 3.x]Label类数字变化动作
[cocos2dx 3.x]Label类数字变化动作
之前写了个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_BEGIN13 USING_NS_CC;14 class Node;15 class SpriteFrame;16 class EventCustom;17 18 19 class NumberChange : public ActionInterval20 {21 public:22 23 static NumberChange* create(float duration, int fromNum, int toNum);24 25 virtual NumberChange* clone() const override;26 virtual NumberChange* reverse(void) const override;27 virtual void startWithTarget(cocos2d::Node *target) override;28 virtual void update(float time) override;29 30 CC_CONSTRUCTOR_ACCESS:31 NumberChange();32 virtual ~NumberChange();33 34 /** initializes the action */35 bool initWithDuration(float duration, int fromNum, int toNum);36 37 protected:38 int _fromNum;39 int _toNum;40 41 private:42 CC_DISALLOW_COPY_AND_ASSIGN(NumberChange);43 };44 45 //NS_CC_END46 47 #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_BEGIN18 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 34 bool NumberChange::initWithDuration(float duration, int fromNum, int toNum)35 {36 if (ActionInterval::initWithDuration(duration))37 {38 _fromNum = fromNum;39 _toNum = toNum;40 return true;41 }42 43 return false;44 }45 46 NumberChange* NumberChange::clone() const47 {48 // no copy constructor49 auto a = new (std::nothrow) NumberChange();50 a->initWithDuration(_duration, _fromNum, _toNum);51 a->autorelease();52 return a;53 }54 55 void NumberChange::startWithTarget(cocos2d::Node *target)56 {57 ActionInterval::startWithTarget(target);58 LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(target);59 if (pLabel)60 {61 std::string numStr = cocos2d::StringUtils::format("%i", _fromNum);62 pLabel->setString(numStr.c_str());63 }64 }65 66 NumberChange* NumberChange::reverse() const67 {68 return NumberChange::create(_duration, _toNum, _fromNum);69 }70 71 72 void NumberChange::update(float t)73 {74 LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(_target);75 if (pLabel)76 {77 int tempNum = (_toNum - _fromNum) * t;78 int num = _fromNum + tempNum;79 std::string numStr = cocos2d::StringUtils::format("%i", num);80 pLabel->setString(numStr.c_str());81 }82 }83 84 //NS_CC_END
[cocos2dx 3.x]Label类数字变化动作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。