Giter Club home page Giter Club logo

Comments (2)

crykn avatar crykn commented on May 22, 2024

Timer#stop does indeed not work as I'd expect it to. While it blocks the current task from completing (and thus from repeating as well), the time passed still counts toward its delay. To test this, just start the example below, immediately press S to stop the timer, count to 5 and then press S again to start the timer.

Expectation: the task is executed 10 seconds later.
Reality: the timer is executed 5 seconds later, because the 5 seconds while the timer was stopped were still applied towards the task's delay.

However, pausing via Timer#delay works fine. To test this, start the example, press P to pause, count to 5 and then unpause via P again. As expected, the task is executed 10 seconds later.

public class Issue7281Test extends ApplicationAdapter {
	private int i = 0;
	private boolean timerStopped = false;
	private boolean timerPaused = false;
	private float timePassed;

	public static void main (String[] argv) {
		Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
		config.setWindowedMode(1024, 720);
		new Lwjgl3Application(new Issue7281Test(), config);
	}

	@Override
	public void create () {
		Timer.schedule(new Timer.Task() {
			@Override
			public void run () {
				Gdx.app.log(String.valueOf(timePassed), "Task " + ++i + " completed!");
			}
		}, 10F, 10F, 9);
	}

	@Override
	public void render () {
		ScreenUtils.clear(timerPaused ? Color.GRAY : (timerStopped ? Color.RED : Color.GREEN));
		timePassed += Gdx.graphics.getDeltaTime();

		if(timerPaused)
			Timer.instance().delay(Math.round(Gdx.graphics.getDeltaTime() * 1000));

		if(Gdx.input.isKeyJustPressed(Input.Keys.S)) { // Start/stop
			if(timerStopped)
				Timer.instance().start();
			 else
				Timer.instance().stop();
			timerStopped = !timerStopped;
		} else if (Gdx.input.isKeyJustPressed(Input.Keys.P)) { // Pause
			timerPaused = !timerPaused;
		} else if (Gdx.input.isKeyJustPressed(Input.Keys.D)) { // Delay
			Timer.instance().delay(2000);
		}
	}
}

from libgdx.

NathanSweet avatar NathanSweet commented on May 22, 2024

@dkoding mentions pausing a timer (which only has start/stop, not pause) and gives unexecutable code (which doesn't call start/stop), so there's nothing to be done there.

@crykn thanks for the executable repro! I'll make a PR.

from libgdx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.