0
      public void delay() { //give delay before comp can  play
      Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            startPlay();
        }
       }, 1500);
   }

I made a game having nine buttons such that a player can play against the phone. Each of these buttons calls the delay method above to create a delay between when the player played and when the phone is given a chance to play but as I test the game sometimes the delay is fine but sometimes I notice that the delay is very short as if a call to the delay method has not been made. I would like to know why its happening like this. Thanks.

JVamcas
  • 122
  • 3
  • 12

1 Answers1

0

Since, all of your buttons are calling the same method startPlay() after a delay of 1500 ms, I don't think you'd be able to differentiate between the delay caused by individual buttons.

Assume you've 2 buttons, Button1 and Button2 and Button3. When Button1 is clicked, a delay of 1500ms happens and then immediately (after 1sec) you press Button2. So a new handler is created which will again have a delay of 1500ms. But now the delay for the Button1 is over and startPlay() would execute. Now this startPlay() belongs to Button1 and not Button2. Button2's startPlay() will be called after 500ms(1500ms - 1sec).

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • The thing is if i press say Button1, a call to the delay method is made which cause a delay between when i pressed Button1 and the time that the phone gets a chance to play. Once i press Button1 i all the buttons are inactive until when the phone has played i.e i only get the chance to play again once the computer has played. – JVamcas Jul 07 '15 at 00:53
  • Can you log the time before and after the delay? Log `System.currenttimemillis()` when the user clicks the button and then again in the `run()` method. The difference between the 2 logs should be of 1500ms. You can also log the difference and see if its different – Antrromet Jul 07 '15 at 00:56
  • I just did that and the difference i got was 1501 – JVamcas Jul 07 '15 at 01:13
  • That is perfect then. Thats what it is supposed to be right? Note that 1500 is in milliseconds. So essentially you're waiting for 1.5s. – Antrromet Jul 07 '15 at 01:14
  • if (j == 0) { buttonC3.setClickable(false); //comp chance to play prevent user playing twice before comp play buttonC2.setClickable(false); buttonC1.setClickable(false); buttonB1.setClickable(false); } } } – JVamcas Jul 07 '15 at 01:17
  • But still there comes a time when the delay seems to be almost zero and that worries me. – JVamcas Jul 07 '15 at 01:22
  • Can you print the logs each time and see where you're going wrong? Ideally the log should always print 1500. – Antrromet Jul 07 '15 at 01:23
  • To set disable the other buttons i use button.Clickable(false) and when the phone returns the chance to the player it sets the button.Clickable(true). I tested it in the debugger and they only respond once the phone has played. – JVamcas Jul 07 '15 at 01:25