【问题标题】:What is the best way to create a countdown timer in Cocos2d-x?在 Cocos2d-x 中创建倒数计时器的最佳方法是什么?
【发布时间】:2017-02-16 21:45:14
【问题描述】:

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

【问题讨论】:

    标签: timer cocos2d-x


    【解决方案1】:

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

    看看这个:

    1. 例如,创建一个名为 countdown 的私​​有 int 成员,并使用您要倒计时的秒数对其进行初始化。另外,声明计时器的Label(我们称之为lbl

    2. 在场景的 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
      
    3. 声明并实现updateTimer 看起来像这样:

      void MySceneClass::updateTimer(float dt)    
      {
          if (!countdown)
              return;     // when countdown reaches 0, stop updating to avoid negative values
          lbl->setString(std::to_string(--countdown));
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      相关资源
      最近更新 更多