Timer 在Cocos2d-x中创建倒计时计时器的最佳方法是什么?

Timer 在Cocos2d-x中创建倒计时计时器的最佳方法是什么?,timer,cocos2d-x,Timer,Cocos2d X,我正在cocos2dx中制作一个游戏,但我不知道如何创建倒计时计时器,以便玩家在时间用完之前只有一定的时间完成关卡。您可以使用调度方法在一定时间后调用函数,并相应地更新计时器标签 看看这个: 例如,创建一个名为countdown的私有int成员,并用您想要倒数的秒数初始化它。另外,声明计时器的标签(我们称之为lbl) 在场景的init方法中,计划一个更新程序并像下面这样初始化标签 this->lbl = Label::createWithTTF(std::to_string(this-&g

我正在cocos2dx中制作一个游戏,但我不知道如何创建倒计时计时器,以便玩家在时间用完之前只有一定的时间完成关卡。

您可以使用
调度
方法在一定时间后调用函数,并相应地更新计时器标签

看看这个:

  • 例如,创建一个名为
    countdown
    的私有
    int
    成员,并用您想要倒数的秒数初始化它。另外,声明计时器的
    标签
    (我们称之为
    lbl

  • 在场景的
    init
    方法中,计划一个更新程序并像下面这样初始化标签

    this->lbl = Label::createWithTTF(std::to_string(this->countdown), "fonts/Marker Felt.ttf", charSize / 15);    // make sure you #include <string>
    lbl->setPosition(Vec2(0,0));     // set the position to wherever you like
    this->schedule(schedule_selector(MySceneClass::updateTimer), 1.0f);    // calls updateTimer once every second
    
  • void MySceneClass::updateTimer(float dt)    
    {
        if (!countdown)
            return;     // when countdown reaches 0, stop updating to avoid negative values
        lbl->setString(std::to_string(--countdown));
    }