How do I create a countdown timer?

How do I create a countdown timer?
0

#1

I am making a game in cocos2d-x but I don’t know how to create a countdown timer so that the player only has a certain amount of time to complete the level before time runs out.


#2

Probably a few ways to do it, but one way would be to use the update function. Set a variable to the total time the player has for the level, decrease it whenever the update function is called, and, when the variable reaches zero, do whatever it is that happens when the time runs out. If you have a label displaying the time, remember to display the time remaining when the game starts, and update it each time the update function is called.

There also appears to be a Node called ProgressTimer, which I think is used for progress bars, and has the actions ProgressTo and ProgressFromTo to fill/empty the bar. I have never used this so I don’t really know about it, but if you wanted to display your “time remaining” as a progress bar rather than a label with the exact time, you might want to check this out.


#3

Could you make a simple example.


#4

For a label displaying the countdown, something like:
Game.h:

float timeRemaining;
Label* labelTime;

Game.cpp

bool Game::init() {
  // other init code here

  // time limit in seconds
  timeRemaining = 100.f;

  // create labelTime here

  this->scheduleUpdate();

  return true;
}

void Game::update(float dt) {
  // make sure game is still running
  if (timeRemaining > 0.f) {
    timeRemaining -= dt;
    // update the label or whatever displays the time here. if displaying the number, use ceilf to round
    // up the time remaining so that you don't display lots of numbers after the decimal point
    labelTime->setString("Time Remaining: " + std::to_string(ceilf(timeRemaining));
    // check if time ran out
    if (timeRemaining <= 0.f) {
      // timer ran out; react here
    }
  }
}

I haven’t tested this, but this should be how a timer basically works. You would probably have your time limit set in your level file or game data, so you would probably not have timeRemaining = 100.f;, but instead something like timeRemaining = getTimeLimit(); that would get the time limit for the level.


#5

Hi, I’m using ui::Text and the setString method is very cost, I’m losing frames.


#6

Don’t call setString for each update, cast float to integer and call setString only when integer value is changed


#8

I’m calling setString for every second. First I called setString in update(float dt), and later I’ve used a scheduler for call setString at every second. In both cases, I’ve lost frames.


#9

Show your code


#10

bool Circle::init() {

if (!Node::init()) {
return false;
}
this->setTag(CIRCLE);
this->setVisible(true);
this->setFrame();
this->countDownStarted = false;
this->speedUp = 1.f;
this->setDotAngularVelocity(DOT_VELOCITY);
this->schedule(CC_SCHEDULE_SELECTOR(Circle::updateTimeText), 1);

return true;
}

void Circle::updateTimeText(float dt) {
auto timeText = this->getTimeText();
if (timeText != nullptr) {
this->setRemaindingTimeToDisappear(MAX(this->getRemaindingTimeToDisappear() - dt, 0));
int time = (int) this->getRemaindingTimeToDisappear();
// The setString method campare current value with new value, before change string
timeText->setString(std::to_string(time));
}
}


#11

I found the error, the problem was the font size, the font size very big