【发布时间】:2017-02-16 21:45:14
【问题描述】:
我正在 cocos2dx 中制作游戏,但我不知道如何创建倒数计时器,以便玩家在时间用完之前只有一定的时间来完成关卡。
【问题讨论】:
我正在 cocos2dx 中制作游戏,但我不知道如何创建倒数计时器,以便玩家在时间用完之前只有一定的时间来完成关卡。
【问题讨论】:
您可以使用schedule 方法在一定时间后调用函数并相应地更新计时器的标签。
看看这个:
例如,创建一个名为 countdown 的私有 int 成员,并使用您要倒计时的秒数对其进行初始化。另外,声明计时器的Label(我们称之为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
声明并实现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));
}
【讨论】: