Skip to main content
added 558 characters in body
Source Link
Joseph
  • 191
  • 5

here is how i'm doing it in my score scene:

auto score = Label::createWithTTF("Score : 0", MY_FONT_NAME, 18);
score->enableOutline(Color4B::ORANGE, 1);
this->addChild(score);

// increment score
score->schedule([this, score](float dt) {
    int initialValue = 0;
    int finalValue = currentLvScore;
    static float duration = 4.f;
    static float timeElapsed = 0.f;

    timeElapsed += dt;
    float prograss = timeElapsed / duration;

    int val = MathUtil::lerp(initialValue, finalValue, prograss);
    score->setString("Score : " + std::to_string(val));

    if (timeElapsed >= duration)
    {
        timeElapsed = 0.f;
        score->unschedule("CountTo");
        score->setString("Score : " + std::to_string(currentLvScore));
    }

}, 0.f, CC_REPEAT_FOREVER, 2.0f, "CountTo");

Edit: a much cleaner way to do it would be using ActionFloat.

    void ::onEnter() {
        Layer::onEnter();
    
        auto lbl = Label::createWithTTF("0", "fonts/Marker Felt.ttf", 12.f);
        this->addChild(lbl);
        lbl->setPosition(center);
    
        // animate float from 0 to 100 in 5 sec
        auto actionfloat = ActionFloat::create(5, 0, 100, [=](float value) {
            lbl->setString(std::to_string(value));
        });
    
        this->runAction(actionfloat);
    }

here is how i'm doing it in my score scene:

auto score = Label::createWithTTF("Score : 0", MY_FONT_NAME, 18);
score->enableOutline(Color4B::ORANGE, 1);
this->addChild(score);

// increment score
score->schedule([this, score](float dt) {
    int initialValue = 0;
    int finalValue = currentLvScore;
    static float duration = 4.f;
    static float timeElapsed = 0.f;

    timeElapsed += dt;
    float prograss = timeElapsed / duration;

    int val = MathUtil::lerp(initialValue, finalValue, prograss);
    score->setString("Score : " + std::to_string(val));

    if (timeElapsed >= duration)
    {
        timeElapsed = 0.f;
        score->unschedule("CountTo");
        score->setString("Score : " + std::to_string(currentLvScore));
    }

}, 0.f, CC_REPEAT_FOREVER, 2.0f, "CountTo");

here is how i'm doing it in my score scene:

auto score = Label::createWithTTF("Score : 0", MY_FONT_NAME, 18);
score->enableOutline(Color4B::ORANGE, 1);
this->addChild(score);

// increment score
score->schedule([this, score](float dt) {
    int initialValue = 0;
    int finalValue = currentLvScore;
    static float duration = 4.f;
    static float timeElapsed = 0.f;

    timeElapsed += dt;
    float prograss = timeElapsed / duration;

    int val = MathUtil::lerp(initialValue, finalValue, prograss);
    score->setString("Score : " + std::to_string(val));

    if (timeElapsed >= duration)
    {
        timeElapsed = 0.f;
        score->unschedule("CountTo");
        score->setString("Score : " + std::to_string(currentLvScore));
    }

}, 0.f, CC_REPEAT_FOREVER, 2.0f, "CountTo");

Edit: a much cleaner way to do it would be using ActionFloat.

    void ::onEnter() {
        Layer::onEnter();
    
        auto lbl = Label::createWithTTF("0", "fonts/Marker Felt.ttf", 12.f);
        this->addChild(lbl);
        lbl->setPosition(center);
    
        // animate float from 0 to 100 in 5 sec
        auto actionfloat = ActionFloat::create(5, 0, 100, [=](float value) {
            lbl->setString(std::to_string(value));
        });
    
        this->runAction(actionfloat);
    }
Source Link
Joseph
  • 191
  • 5

here is how i'm doing it in my score scene:

auto score = Label::createWithTTF("Score : 0", MY_FONT_NAME, 18);
score->enableOutline(Color4B::ORANGE, 1);
this->addChild(score);

// increment score
score->schedule([this, score](float dt) {
    int initialValue = 0;
    int finalValue = currentLvScore;
    static float duration = 4.f;
    static float timeElapsed = 0.f;

    timeElapsed += dt;
    float prograss = timeElapsed / duration;

    int val = MathUtil::lerp(initialValue, finalValue, prograss);
    score->setString("Score : " + std::to_string(val));

    if (timeElapsed >= duration)
    {
        timeElapsed = 0.f;
        score->unschedule("CountTo");
        score->setString("Score : " + std::to_string(currentLvScore));
    }

}, 0.f, CC_REPEAT_FOREVER, 2.0f, "CountTo");