-2

So I'm making a game, and I want the user to be able to fight crimes. Only I don't want them to be able to fight crimes again for a period of time, a 10 second delay max. I know that I could use Math.Random() somehow, but I'm having trouble figuring out how to use it in this context.

To clarify, I want a label and two buttons saying yes or no to become visible after a random period of time, 20 seconds max. Can someone explain how this could be done, or if there is a better way to do this?

Thanks in advance.

Nemo
  • 15
  • 2
  • You can disable your button say for 20Second , after user Clicks on button , `JButton#setDisabled(true);` – Neeraj Jain Mar 19 '15 at 06:36
  • Basicly you create a timestamp like this `System.currentTimeMillis()` – SomeJavaGuy Mar 19 '15 at 06:36
  • The question is not clear about what you are asking for. If I read only the title, the answer is very simple and already explained here: [java: Random Time generator](http://stackoverflow.com/questions/14984545/java-random-time-generator) – mins Mar 19 '15 at 06:39

2 Answers2

0

Basicly you create a timestamp like this long timestamp = System.currentTimeMillis() after you have done the "fight". You instantly can disable the button now

Then you can create a Random object and call it´s next int method with the span you want to create for this 10-20 sec like this

Random r = new Random();
int span = r.nextInt(10);

Furthermore you just mathematicly add id up to milliseconds

span += 1000;

Then you would create a method that would be called in your gui update function, or a seperate thread, which would check the currentMillis against the timestamp of your last "fight".

if(System.currentTimeMillis() >= span + timestamp) {
    // activate
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
0

I would try creating a new Thread that does not but sleep for your random amount of time and then enables the button again.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58