简体   繁体   中英

How to increment time every second in Libgdx

I try to add time to my game that will increment every second. How to fix that?

I add my code below

float timer;
timer += delta;
if(timer<=delta+1000)//every one sec
{
  time = time+1;
  timePoint.setSentence(""+time/100);
  timer = 0f;
 }

as your note, delta is Gdx.graphics.getDeltaTime(). 'time' is string. but looks like the time increment slowly. means that when time already run about 1,5 sec, 'time' still increase 1. I divide by 100 because if not it will increase more faster/sec,

i also use TimeUtils class from libgdx but it produced similar result.

Thanks before.

This should work. Note that time/100 results in 0 for 0-99 and 1 for 100-199. That's probably not the effect you wanted.

float timer;

timer += delta;
if (timer >= 1) {
    time++;
    timePoint.setSentence(""+time);
    timer -= 1;
}

The problem was that you set the timer back to 0. If it was 1.1f for example (because the last delta was 0.1f), then you basically lose 100ms. Don't reset the timer to 0f, but decrease it by 1f instead.

I would do it like this:

float time = 0f;

In render:

time += delta;
timePoint.setSentence(Integer.toString((int)time));

delta is giving the ellapsed time in seconds, so for 30FPS it is 1/30 seconds. So you only need to add it to your time . To print the difference (i guess the setSentence is printing the text) you only need to cast it to int , so that it cuts the fraction digits.

Hope it helps.

EDIT: If you need the timer and the time variable somewhere you need to store them in 2 different variables.
For that i would do it like this:

float timer = 0f;
int time = 0;

And in render :

timer+=delta;
if (timer >= 1f) {
   time++;
   timePoint.setSentence(Integer.toString(time));
   timer-=1f;
}

By doing this you are not loosing the few milli seconds you would loose if you reset the timer . This means, that if timer goes from 0.99f to 1.05f in one renderloop and you reset the timer to 0f, you loose 0.05 seconds. If this happens every second you loose 1 second every 20 seconds (if i am not wrong^^)

Like in pure java:

Timer timer = new Timer();
timer.schedule(new incrementYourValue(), 0, 1000); //1000 is time in mili sec
                                                   //0 is the time waited before the beginning.

Had similar problem but for me using delta time was kinda overkill. Instead I used Libgdx Timer that "Executes tasks in the future on the main loop thread ". Using Timer from java.util may cause Concurrency problems in libgdx.

import com.badlogic.gdx.utils.Timer;
public class Scheduler {
public Scheduler(Runnable runnable, float periodInSeconds) {
    Timer timer = new Timer();
    timer.scheduleTask(new Timer.Task() {
        @Override
        public void run() {
            runnable.run();
        }
    }, 0, periodInSeconds);
}}

And usage:

new Scheduler(() -> updateSth(), 1f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM