How do you temporarily pause a loop until a button is pressed in java -
how can make loop temporarily pause while wait button pressed? iv been looking around while, , cant seem find it, thought try here.
edit-
i creating blackjack game, added gui buttons, hitme , stand once game gets point in loop need player input either, hitme or stand added buttons execute that, cant figure out how pause loop check , see if button pressed, , 1 continue.
what have tried this:
(othercode) g.printplayercards(p1); g.totalworth(p1); thing.messagetop("your total card amount is: " + p1.gettotalworth()); thing.messagetop("hit me? or stay?"); thing.waitonbutton(); public void waitonbutton(){ wantingtobeclick = 1; do{ while(!(hitme == 0)){ hitme = 0; wantingtobeclick =0; } }while(wantingtobeclick == 1); } public void actionperformed(actionevent e) { if(wantingtobeclick == 1){ if (e.getsource() == hitme){ hitme = 1; system.out.println("iclickedhitme"); g.hitme(player1); waitonbutton(); } if(e.getsource() == stand){ hitme = 1; g.changestandhit(); } } }
it stays in infinite loop , doesn't continue on loop in main.
the way can think 'pause' loop execute more code during loop, preferably on different thread. i.e.
while (true) { if (_buttonispressed) { thread.sleep(5000); // loop paused } }
but, more point, feels might going things wrong way.
instead of running loop check see if has happened, you're better off firing action once button pressed. called event-driven programming.
Comments
Post a Comment