[cocos2dx 3.x]Label类数字变化动作

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

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

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

 #ifndef __MISC_NODE_CCNUMBER_CHANGE_H__
#define __MISC_NODE_CCNUMBER_CHANGE_H__ #include <vector> #include "2d/CCAction.h"
#include "2d/CCAnimation.h"
#include "2d/CCActionInterval.h"
#include "base/CCProtocols.h"
#include "base/CCVector.h" //NS_CC_BEGIN
USING_NS_CC;
class Node;
class SpriteFrame;
class EventCustom; class NumberChange : public ActionInterval
{
public: static NumberChange* create(float duration, int fromNum, int toNum); virtual NumberChange* clone() const override;
virtual NumberChange* reverse(void) const override;
virtual void startWithTarget(cocos2d::Node *target) override;
virtual void update(float time) override; CC_CONSTRUCTOR_ACCESS:
NumberChange();
virtual ~NumberChange(); /** initializes the action */
bool initWithDuration(float duration, int fromNum, int toNum); protected:
int _fromNum;
int _toNum; private:
CC_DISALLOW_COPY_AND_ASSIGN(NumberChange);
}; //NS_CC_END #endif //__MISC_NODE_CCNUMBER_CHANGE_H__
 #include "2d/CCActionInterval.h"

 #include <stdarg.h>

 #include "2d/CCSprite.h"
#include "2d/CCNode.h"
#include "2d/CCSpriteFrame.h"
#include "2d/CCActionInstant.h"
#include "base/CCDirector.h"
#include "base/CCEventCustom.h"
#include "base/CCEventDispatcher.h"
#include "platform/CCStdC.h"
#include "deprecated/CCString.h"
#include "NumberChange.h" USING_NS_CC;
//NS_CC_BEGIN
NumberChange::NumberChange(){
} NumberChange::~NumberChange(){
} NumberChange* NumberChange::create(float duration, int fromNum, int toNum)
{
NumberChange *ret = new (std::nothrow) NumberChange();
ret->initWithDuration(duration, fromNum, toNum);
ret->autorelease(); return ret;
} bool NumberChange::initWithDuration(float duration, int fromNum, int toNum)
{
if (ActionInterval::initWithDuration(duration))
{
_fromNum = fromNum;
_toNum = toNum;
return true;
} return false;
} NumberChange* NumberChange::clone() const
{
// no copy constructor
auto a = new (std::nothrow) NumberChange();
a->initWithDuration(_duration, _fromNum, _toNum);
a->autorelease();
return a;
} void NumberChange::startWithTarget(cocos2d::Node *target)
{
ActionInterval::startWithTarget(target);
LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(target);
if (pLabel)
{
std::string numStr = cocos2d::StringUtils::format("%i", _fromNum);
pLabel->setString(numStr.c_str());
}
} NumberChange* NumberChange::reverse() const
{
return NumberChange::create(_duration, _toNum, _fromNum);
} void NumberChange::update(float t)
{
LabelProtocol *pLabel = dynamic_cast<LabelProtocol*>(_target);
if (pLabel)
{
int tempNum = (_toNum - _fromNum) * t;
int num = _fromNum + tempNum;
std::string numStr = cocos2d::StringUtils::format("%i", num);
pLabel->setString(numStr.c_str());
}
} //NS_CC_END
上一篇:ios移动端浏览器点击事件失效的解决方案


下一篇:SQL语句在数据库中可以执行在mybatis执行不了