Skip to content Skip to sidebar Skip to footer

How Can I Set Timer In LIBGDX

I want to change my balloon's location (randomly) in every second. I wrote this code: public void render() { int initialDelay = 1000; // start after 1 seconds int period

Solution 1:

Don't fire threads inside the render method, it's not safe, can cause thread leaks, a lot of other problems and will be harder to maintain your code, to handle time use a variable adding delta time every time render is called, when this variable is superior a 1.0f means that one second has passed, your code would be something like this:

private float timeSeconds = 0f;
private float period = 1f;

public void render() {
    //Execute handleEvent each 1 second
    timeSeconds +=Gdx.graphics.getRawDeltaTime();
    if(timeSeconds > period){
        timeSeconds-=period;
        handleEvent();
    }
    Gdx.gl.glClearColor(56, 143, 189, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    batch.draw(balloon, balloon_rec.x, balloon_rec.y);
    batch.end();

}

public void handleEvent() {
    rand_x = (r.nextInt(1840));
    rand_y = (r.nextInt(1000));
    balloon.x = rand_x;
    balloon.y = rand_y;
    System.out.println("deneme");
}

Post a Comment for "How Can I Set Timer In LIBGDX"