Jump to content

How to control three different timers using Java?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hello everyone!
I'am trying to make an easy java program/game.

In this game an user have an math problem to solve (something like 4*8 = X)
The X is the number that a user have to "hit".
I created a grid layout and when the game start: the math problem is showed and on the grid layout appears 3 solutions which have a timer. Let's say that I have a grid
layout 6*6, then the solution start in the positions let's say 6*0 ,6*3 ,6*5 , and every random seconds the number 6 (in this case) decrease for -1.
To win the user have to hit the button with solution till the solution get in to the position 0*3 (for example).

For every solution I have a timer which looks like this:


public void start_first() {


		final Timer timer = new Timer();


		timer.schedule(new TimerTask() {


			public void run() {


				grid[p][P].setText(solution);

				grid[p + 1][P].setText("");

				grid[p + 1][P].setBorder(null);

				p--;



when user == click solution // PSEUDOCODE


timer.cancel();

// HERE I WOULD LIKE TO STOP ALL THE TIMERS NOT ONLY THIS ONE



So I have 3 same timers.. for example this is a timer for the second wrong solution:



public void start_error() {


		final Timer timer = new Timer();


		timer.schedule(new TimerTask() {


			public void run() {


				grid[v][V].setText(err);

				grid[v + 1][V].setText("");

				grid[v + 1][V].setBorder(null);

				v--;


when user == click wrong solution // PSEUDOCODE


timer.cancel();

// HERE I WOULD LIKE TO STOP ALL THE TIMERS NOT ONLY THIS ONE




What I would like to do, is when a user hit the right (or wrong) solution that all the timers stop not only one..
Is this possible? Is there something that I can do to make what I want?

Guys thank you in advance for your answers.
Have a nice day!

#2
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Ok I actually found a solution but is not jet what I want..
I have a problem with this:


public void print() {


		final Timer timer = new Timer();


		timer.schedule(new TimerTask() {


			public void run() {



			    try {

			        while (true) {

			            System.out.println("1 SECOND");

			            Thread.sleep(1 * 1000);

			        }

			    } catch (InterruptedException e) {

			        e.printStackTrace();

			    }


      System.out.println("5 SECONDS");



	}, 0, 5 * 1000); 

								

	}



The problem is that now is printing only 1 SECOND and it never print 5 SECOND..
Can someone help me with this one..
Thx

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
This part of your code will run forever:
while (true)

That's what while(true) does, run forever.
I'm not sure what you're trying to accomplish but if you want a thread to sleep for 5 seconds, try sleeping for 5,000 milliseconds.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Just use a for-loop which loops 5 times instead of while(true)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users