Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delay a board game between moves so a sound can play
    primarykey
    data
    text
    <p>In the board game I am designing, each user has a grid with 100 cells. The user clicks a cell in the computer's grid, which makes a sound and changes to a different colour. The computer then automatically clicks a cell in the user's grid, which makes a sound and changes to a different colour. </p> <p>The user's clicks are dealt with via the MouseListener (and the MouseClicked method). Currently, the last thing the method does is call the computerMove() method. This method executes and performs the computer move before giving changing the current player back to human. The game proceeds when the human makes another mouse click.</p> <p>Ideally, I'd like to have a pause (perhaps a second) between each player move. However, due to the fact the computerMove method is being called inside the MouseClicked method, this is proving troublesome. By using Thread.sleep and even a TimerTask, the best I can do is to slow the human player's move down. However, as soon as the player makes a mouse click, the computer instantaneously responds.</p> <p>Does anybody have any suggestions as to how I could implement a delay? Do I need to change how I am calling the methods?</p> <p>ComputerMove method:</p> <pre><code> public void computerMove() { if (currentTurn == computer) { int xPos = randomGenerator.nextInt(10); int yPos = randomGenerator.nextInt(10); LogicCell attackedCell = new LogicCell(xPos, yPos); playerGridLogic.addCellsThatHaveBeenAttacked(attackedCell); if (playerGridLogic.getCellsWithShips().contains(attackedCell)) { playerGrid.getCellArray()[xPos][yPos].setBackground(Color.ORANGE); hitSound(); } else { playerGrid.getCellArray()[xPos][yPos].setBackground(Color.MAGENTA); missSound(); } nextTurn(); } } </code></pre> <p>Corresponding MouseClick method:</p> <pre><code> @Override public void mouseClicked(MouseEvent e) { if (allComputerShipsPlaced &amp;&amp; currentTurn == human) { ViewCell currentCell = (ViewCell) e.getSource(); xPos = currentCell.getXPos(); yPos = currentCell.getYPos(); LogicCell attackedCell = new LogicCell(xPos, yPos); computerGridLogic.addCellsThatHaveBeenAttacked(attackedCell); if (computerGridLogic.getCellsWithShips().contains(attackedCell)) { cellArray[xPos][yPos].setBackground(Color.ORANGE); hitSound(); } else { cellArray[xPos][yPos].setBackground(Color.MAGENTA); missSound(); } nextTurn(); computerMove(); } } </code></pre> <p>missSound method() (hitSound is very similar):</p> <pre><code> public static void missSound() { try { Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new File("water-splash.wav"))); clip.start(); } catch (Exception exc) { exc.printStackTrace(System.out); } } </code></pre> <p>Edit: I've tried changing the sound classes to this, but to no avail:</p> <pre><code> public static void hitSound() { try { Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new File("bomb-explosion.wav"))); clip.start(); LineListener listener = new LineListener() { public void update(LineEvent event) { if (event.getType() != Type.STOP) { return; } try { queue.take(); } catch (InterruptedException e) { //ignore this } } }; clip.addLineListener(listener); } catch (Exception exc) { exc.printStackTrace(System.out); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload