Cocos2dx實現(xiàn)數(shù)字跳動效果
更新時間:2020年09月18日 11:03:16 作者:auccy
這篇文章主要為大家詳細介紹了Cocos2dx實現(xiàn)數(shù)字跳動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Cocos2dx實現(xiàn)數(shù)字跳動效果的具體代碼,供大家參考,具體內(nèi)容如下
封裝的類如下:
.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; //數(shù)字跳動間隔
float m_scheduleInterval;//調(diào)度器間隔
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();
}
}
使用示例:
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);
效果如圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解CocosCreator項目結(jié)構(gòu)機制
- 如何使用CocosCreator對象池
- CocosCreator如何實現(xiàn)劃過的位置顯示紋理
- 整理CocosCreator常用知識點
- 全面講解CocosCreator熱更新
- CocosCreator經(jīng)典入門項目之flappybird
- CocosCreator通用框架設(shè)計之網(wǎng)絡(luò)
- 如何用CocosCreator實現(xiàn)射擊小游戲
- cocos2dx-3.10 C++實現(xiàn)滾動數(shù)字
- cocos2dx實現(xiàn)刮獎效果
- CocosCreator ScrollView優(yōu)化系列之分幀加載
相關(guān)文章
關(guān)于STL的erase()陷阱-迭代器失效問題的總結(jié)
下面小編就為大家?guī)硪黄P(guān)于STL的erase()陷阱-迭代器失效問題的總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題
Lambda表達式是現(xiàn)代C++的一個語法糖,挺好用的。但是如果使用不當(dāng),會導(dǎo)致內(nèi)存泄露或潛在的崩潰問題,這里總結(jié)下c++ lambda捕獲this 導(dǎo)致多線程下類釋放后還在使用的錯誤問題,感興趣的朋友一起看看吧2023-02-02
Vscode Remote Development遠程開發(fā)調(diào)試的實現(xiàn)思路
這篇文章主要介紹了Vscode Remote Development遠程開發(fā)調(diào)試的相關(guān)資料,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
如何在Qt中實現(xiàn)關(guān)于Json?的操作
JSON是一種輕量級數(shù)據(jù)交換格式,常用于客戶端和服務(wù)端的數(shù)據(jù)交互,不依賴于編程語言,在很多編程語言中都可以使用JSON,這篇文章主要介紹了在Qt中實現(xiàn)關(guān)于Json的操作,需要的朋友可以參考下2023-08-08
C語言驅(qū)動開發(fā)內(nèi)核枚舉IoTimer定時器解析
這篇文章主要為大家介紹了C語言驅(qū)動開發(fā)內(nèi)核枚舉IoTimer定時器解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

