Cocos2dx實(shí)現(xiàn)數(shù)字跳動(dòng)效果
本文實(shí)例為大家分享了Cocos2dx實(shí)現(xiàn)數(shù)字跳動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
封裝的類(lè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ù)字跳動(dòng)間隔
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);
效果如圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解CocosCreator項(xiàng)目結(jié)構(gòu)機(jī)制
- 如何使用CocosCreator對(duì)象池
- CocosCreator如何實(shí)現(xiàn)劃過(guò)的位置顯示紋理
- 整理CocosCreator常用知識(shí)點(diǎn)
- 全面講解CocosCreator熱更新
- CocosCreator經(jīng)典入門(mén)項(xiàng)目之flappybird
- CocosCreator通用框架設(shè)計(jì)之網(wǎng)絡(luò)
- 如何用CocosCreator實(shí)現(xiàn)射擊小游戲
- cocos2dx-3.10 C++實(shí)現(xiàn)滾動(dòng)數(shù)字
- cocos2dx實(shí)現(xiàn)刮獎(jiǎng)效果
- CocosCreator ScrollView優(yōu)化系列之分幀加載
相關(guān)文章
C語(yǔ)言中feof函數(shù)和ferror函數(shù)示例詳解
在C語(yǔ)言中feof函數(shù)用于檢查文件流的結(jié)束標(biāo)志,判斷文件在讀取時(shí)是否已經(jīng)到達(dá)了文件的末尾,這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中feof函數(shù)和ferror函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-09-09
C語(yǔ)言深入講解動(dòng)態(tài)內(nèi)存分配函數(shù)的使用
這篇文章主要介紹了C語(yǔ)言動(dòng)態(tài)內(nèi)存分配,C語(yǔ)言內(nèi)存管理相關(guān)的函數(shù)主要有realloc、calloc、malloc、free、柔性數(shù)組等,下面這篇文章帶大家了解一下2022-05-05
FFmpeg實(shí)現(xiàn)多線程編碼并保存mp4文件
這篇文章主要為大家介紹了FFmpeg如何持續(xù)的從指定內(nèi)存中讀取原始數(shù)據(jù),再將解碼數(shù)據(jù)存入隊(duì)列中,并通過(guò)單獨(dú)的線程進(jìn)行編碼,最后保存為mp4文件,感興趣的可以了解下2023-08-08
C語(yǔ)言實(shí)現(xiàn)順序表的基本操作指南(注釋很詳細(xì))
線性表是最簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu),而順序表又是最簡(jiǎn)單的線性表,其基本思想是用一段地址連續(xù)的儲(chǔ)存單元依次存儲(chǔ)線性表的數(shù)據(jù)元素,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言實(shí)現(xiàn)順序表的基本操作,需要的朋友可以參考下2021-10-10
C++實(shí)現(xiàn)LeetCode(126.詞語(yǔ)階梯之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(126.詞語(yǔ)階梯之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言實(shí)現(xiàn)進(jìn)程5狀態(tài)模型的狀態(tài)機(jī)
狀態(tài)機(jī)在實(shí)際工作開(kāi)發(fā)中應(yīng)用非常廣泛,用這幅圖就可以很清晰的表達(dá)整個(gè)狀態(tài)的流轉(zhuǎn)。本篇通過(guò)C語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的進(jìn)程5狀態(tài)模型的狀態(tài)機(jī),讓大家熟悉一下?tīng)顟B(tài)機(jī)的魅力,需要的可以參考一下2022-10-10

