How do i keep track of elapsed time in Java's 3rd party API “LibGDX”?

StackOverflow https://stackoverflow.com/questions/9442924

  •  12-11-2019
  •  | 
  •  

質問

I am making a game in which the player ("Bob") moves vertically and collects coins continuously. If the player does not manage to collect any coins for 5 seconds, "Bob" begins to fall. As time continues to count down, he will fall more quickly.

My question is this: How does one keep track of elapsed time in a LibGDX (Java) application?

Sample code follows.

public void update (float deltaTime)
{
`velocity.add(accel.x * deltaTime,accel.y*deltaTime);`

    position.add(velocity.x * deltaTime, velocity.y * deltaTime);
    bounds.x = position.x - bounds.width / 2;
    bounds.y = position.y - bounds.height / 2;
    if (velocity.y > 0 && state == BOB_COLLECT_COINE)
    {
     if (state== BOB_STATE_JUMP)
      {
       state = BOB_STATE_Increase;
        stateTime = 0;
    }
    else
    {
    if(state != BOB_STATE_JUMP)
     {
      state = BOB_STATE_JUMP;//BOB_STATE_JUMP
       stateTime = 0;

        }
      }
    }

     if (velocity.y < 0 && state != BOB_COLLECT_COINE)
      {
       if (state != BOB_STATE_FALL) {
       state = BOB_STATE_FALL;
        stateTime = 0;
      }
     }
       if (position.x < 0) position.x = World.WORLD_WIDTH;
    if (position.x > World.WORLD_WIDTH) position.x = 0;

      stateTime += deltaTime;
     }



     public void hitSquirrel ()
       {
       velocity.set(0, 0);
       state = BOB_COLLECT_COINE;s
       stateTime = 0;
       }  

     public void collectCoine()
      {

      state = BOB_COLLECT_COINE;
       velocity.y = BOB_JUMP_VELOCITY *1.5f;
        stateTime = 0;
       }

and call the collectmethod in World class in upate Bob as --

  private void updateBob(float deltaTime, float accelX)
    {

  diff = collidetime-System.currentTimeMillis();
  if (bob.state != Bob.BOB_COLLECT_COINE && diff>2000) //bob.position.y <= 0.5f)
    {
     bob.hitSquirrel();
   }
役に立ちましたか?

解決

Seeing how this answer has a lot a views, I should point out an issue with the accepted answer and provide an alternate solution.

Your 'timer' will slowly drift the longer you run the program because of rounding caused by the following line of code:

time = 0;

The reason is the if condition checks if the time value is greater or equal to 5 (very likely its going to be greater because of rounding errors and time between frames can vary). A more robust solution is to not 'reset' the time, but to subtract the time you waited:

private static final float WAIT_TIME = 5f;
float time = 0;

public void update(float deltaTime) {
    time += deltaTime;
    if (time >= WAIT_TIME) {
        // TODO: Perform your action here

        // Reset timer (not set to 0)
        time -= WAIT_TIME;
    }
}

You are very likely to not notice this subtle issue during quick tests, but running an app for a couple of minutes you may start to notice it if you are carefully looking at the timing of events.

他のヒント

i did it like this

float time=0;

public void update(deltaTime){

  time += deltaTime;
  if(time >= 5){
     //Do whatever u want to do after 5 seconds
    time = 0; //i reset the time to 0

  }
}

have you tried to use Gdx.graphics.getElapsedTime()
(not sure with the exact function name)

The method as in build 0.9.7 is 'Gdx.graphics.getDeltaTime()' so the above suggestion is absolutely on the spot.

it's

float time = 0;


//in update/render  
time += Gdx.app.getGraphics().getDeltaTime();
if(time >=5)
{
    //do your stuff here
    Gdx.app.log("timer ", "after 5 sec :>");
    time = 0; //reset
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top