如何用Cocos2dx制作数字实现跳动效果的长尾词?

2026-04-19 02:352阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计478个文字,预计阅读时间需要2分钟。

如何用Cocos2dx制作数字实现跳动效果的长尾词?

本文将分享如何在大家族项目中使用Cocos2dx实现数字跳动效果的完整代码示例。以下是大致内容:

封装的类:`.h` 文件cppclass DigitalBeatText : public cocos2d::Node {public: DigitalBeatText(); ~DigitalBeatText() override;

// ...};

实现细节:- `DigitalBeatText` 类继承自 `cocos2d::Node`。- 类中定义了构造函数和析构函数。- 需要进一步实现数字跳动效果的相关方法。

本文实例为大家分享了Cocos2dx实现数字跳动效果的具体代码,供大家参考,具体内容如下

封装的类如下:

.h文件

class DigitalBeatText:public cocos2d::Node { public: DigitalBeatText(); ~DigitalBeatText(); static DigitalBeatText *create(int value); void setValue(int newValue); protected: bool init(int value); void setValueNoAction(int newValue); void startRoll(); void stopRoll(); void onTimeHandler(float dt); protected: cocos2d::ui::Text * m_txt; int m_lastValue; int m_newValue; int m_valueGap; //数字跳动间隔 float m_scheduleInterval;//调度器间隔 bool m_isReverse; // true: 从大到小 };

.cpp文件:

DigitalBeatText::DigitalBeatText() :m_txt(nullptr) , m_lastValue(0) , m_newValue(0) , m_valueGap(0) , m_scheduleInterval(1.0f/60.f) , m_isReverse(false) { } DigitalBeatText::~DigitalBeatText() { } DigitalBeatText* DigitalBeatText::create(int value) { auto pRet = new DigitalBeatText; if (pRet->init(value)) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return nullptr; } bool DigitalBeatText::init(int value) { if (!Node::init()){ return false; } char buff[16] = { 0 }; m_txt = ui::Text::create(); m_txt->setFontSize(36); sprintf(buff, "%d", value); m_txt->setString(buff); this->addChild(m_txt); return true; } void DigitalBeatText::setValue(int newValue) { m_isReverse = newValue < m_lastValue; stopRoll(); m_newValue = newValue; startRoll(); } void DigitalBeatText::setValueNoAction(int newValue) { m_lastValue = newValue; m_newValue = newValue; m_txt->setString(cocos2d::StringUtils::toString(m_lastValue)); } void DigitalBeatText::startRoll() { int count = m_newValue - m_lastValue; if (count>0){ m_valueGap = ceil(count / (1.0 / m_scheduleInterval)); }else{ m_valueGap = floor(count / (1.0 / m_scheduleInterval)); } schedule(CC_SCHEDULE_SELECTOR(DigitalBeatText::onTimeHandler), m_scheduleInterval); } void DigitalBeatText::stopRoll() { unschedule(CC_SCHEDULE_SELECTOR(DigitalBeatText::onTimeHandler)); } void DigitalBeatText::onTimeHandler(float dt) { m_lastValue += m_valueGap; bool stop = false; if (!m_isReverse) { if (m_lastValue >= m_newValue){ m_lastValue = m_newValue; stop = true; } } else{ if (m_lastValue <= m_newValue){ m_lastValue = m_newValue; stop = true; } } m_txt->setString(cocos2d::StringUtils::toString(m_lastValue)); if (stop){ this->stopRoll(); } }

使用示例:

如何用Cocos2dx制作数字实现跳动效果的长尾词?

m_index = 1; m_beatT = DigitalBeatText::create(m_index); m_beatT->setPosition(visibleSize*0.5); this->addChild(m_beatT); m_index += RandomHelper::random_int(0, 100) - 50; m_beatT->setValue(m_index);

效果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计478个文字,预计阅读时间需要2分钟。

如何用Cocos2dx制作数字实现跳动效果的长尾词?

本文将分享如何在大家族项目中使用Cocos2dx实现数字跳动效果的完整代码示例。以下是大致内容:

封装的类:`.h` 文件cppclass DigitalBeatText : public cocos2d::Node {public: DigitalBeatText(); ~DigitalBeatText() override;

// ...};

实现细节:- `DigitalBeatText` 类继承自 `cocos2d::Node`。- 类中定义了构造函数和析构函数。- 需要进一步实现数字跳动效果的相关方法。

本文实例为大家分享了Cocos2dx实现数字跳动效果的具体代码,供大家参考,具体内容如下

封装的类如下:

.h文件

class DigitalBeatText:public cocos2d::Node { public: DigitalBeatText(); ~DigitalBeatText(); static DigitalBeatText *create(int value); void setValue(int newValue); protected: bool init(int value); void setValueNoAction(int newValue); void startRoll(); void stopRoll(); void onTimeHandler(float dt); protected: cocos2d::ui::Text * m_txt; int m_lastValue; int m_newValue; int m_valueGap; //数字跳动间隔 float m_scheduleInterval;//调度器间隔 bool m_isReverse; // true: 从大到小 };

.cpp文件:

DigitalBeatText::DigitalBeatText() :m_txt(nullptr) , m_lastValue(0) , m_newValue(0) , m_valueGap(0) , m_scheduleInterval(1.0f/60.f) , m_isReverse(false) { } DigitalBeatText::~DigitalBeatText() { } DigitalBeatText* DigitalBeatText::create(int value) { auto pRet = new DigitalBeatText; if (pRet->init(value)) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return nullptr; } bool DigitalBeatText::init(int value) { if (!Node::init()){ return false; } char buff[16] = { 0 }; m_txt = ui::Text::create(); m_txt->setFontSize(36); sprintf(buff, "%d", value); m_txt->setString(buff); this->addChild(m_txt); return true; } void DigitalBeatText::setValue(int newValue) { m_isReverse = newValue < m_lastValue; stopRoll(); m_newValue = newValue; startRoll(); } void DigitalBeatText::setValueNoAction(int newValue) { m_lastValue = newValue; m_newValue = newValue; m_txt->setString(cocos2d::StringUtils::toString(m_lastValue)); } void DigitalBeatText::startRoll() { int count = m_newValue - m_lastValue; if (count>0){ m_valueGap = ceil(count / (1.0 / m_scheduleInterval)); }else{ m_valueGap = floor(count / (1.0 / m_scheduleInterval)); } schedule(CC_SCHEDULE_SELECTOR(DigitalBeatText::onTimeHandler), m_scheduleInterval); } void DigitalBeatText::stopRoll() { unschedule(CC_SCHEDULE_SELECTOR(DigitalBeatText::onTimeHandler)); } void DigitalBeatText::onTimeHandler(float dt) { m_lastValue += m_valueGap; bool stop = false; if (!m_isReverse) { if (m_lastValue >= m_newValue){ m_lastValue = m_newValue; stop = true; } } else{ if (m_lastValue <= m_newValue){ m_lastValue = m_newValue; stop = true; } } m_txt->setString(cocos2d::StringUtils::toString(m_lastValue)); if (stop){ this->stopRoll(); } }

使用示例:

如何用Cocos2dx制作数字实现跳动效果的长尾词?

m_index = 1; m_beatT = DigitalBeatText::create(m_index); m_beatT->setPosition(visibleSize*0.5); this->addChild(m_beatT); m_index += RandomHelper::random_int(0, 100) - 50; m_beatT->setValue(m_index);

效果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。