(I feel so ashamed for needing to ask so many questions here, and not being able to help other people, but once again I need your help)
I'm trying to add a pause button to my game. I've coded so when the image used is the "pause button" the game is running, and I would like to make so when the "play icon" is being used all the actors that are subclasses of SmoothMover() stop acting. I thought of doing something like this:
Game class (subclass of World):
Pause button (subclass of Actor):
But it doesn't work (the IllegalMonitorStateException is catch directly when I press run). Can anyone help me achieve this, please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /** * Will pause the game, stopping all actors from moving and will show Main Menu and Replay option */ public void pauseGame() { addObject( new Replay(), WIDE/ 4 , HIGH/ 2 ); addObject( new Replay(), WIDE/ 4 * 3 , HIGH/ 2 ); try { getObjects(SmoothMover. class ).notifyAll(); } catch (IllegalMonitorStateException e) { System.out.println( "Error with SmoothMover class: " + e.getMessage()); System.exit(- 1 ); } } /** * Will resume the game, if there was a pause menu before, * Panels will be removed and the actors will be awaken */ public void resumeGame() { removeObjects(getObjects(Panels. class )); try { getObjects(SmoothMover. class ).wait(); } catch (InterruptedException e) { System.out.println( "Error with SmoothMover class: " + e.getMessage()); System.exit(- 1 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void act() { if ( this .getImage() == images[ 0 ]) { ((Game) getWorld()).resumeGame(); //resumes the game } else { ((Game) getWorld()).pauseGame(); //pauses the game } } |