diff --git a/saves/Level3-dialogue.txt b/saves/Level3-dialogue.txt index 9e9e69e..6ddb556 100644 --- a/saves/Level3-dialogue.txt +++ b/saves/Level3-dialogue.txt @@ -1,4 +1,3 @@ -$Villain Oh you made it past the first real level Feeling acomplished aren't you... diff --git a/sound/pen.wav b/sound/pen.wav index 4a76e6d..2246ad2 100644 Binary files a/sound/pen.wav and b/sound/pen.wav differ diff --git a/src/DialogueMenu.java b/src/DialogueMenu.java index 6ddb095..1099f06 100644 --- a/src/DialogueMenu.java +++ b/src/DialogueMenu.java @@ -19,6 +19,7 @@ public class DialogueMenu extends TextBox implements Serializable { public int currentFrame = 0; public int frameCounter = 0; public boolean isNarrator; + public SoundWrapper currentSound; public DialogueMenu(int y, int yHeight, Font font, BufferedImageWrapper portrait, boolean isNarrator) { super(y, GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*3, yHeight, 0, font, null, null); @@ -104,11 +105,13 @@ public class DialogueMenu extends TextBox implements Serializable { if (frameCounter >= FREQUENCY) { frameCounter -= FREQUENCY; currentFrame += 1; - // play dialogue sound - try { - UtilityFunction.playSound("sound/pen.wav"); - } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { - throw new RuntimeException(e); + // play dialogue sound if it is not already playing + if (currentSound == null || !currentSound.sound.clip.isOpen()) { + try { + currentSound = UtilityFunction.playSound("sound/pen.wav"); + } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { + throw new RuntimeException(e); + } } } // set font of string to be drawn @@ -119,6 +122,11 @@ public class DialogueMenu extends TextBox implements Serializable { // if the text has been completely drawn (nothing left to animate), return true if (currentFrame >= text.length()) { currentFrame = 0; + // if the text is not being animated, there is no reason to play the sound either, so it is closed and then dereferenced + if (currentSound != null) { + currentSound.sound.close(); + currentSound = null; + } return true; } else { // otherwise, return false return false; diff --git a/src/UtilityFunction.java b/src/UtilityFunction.java index 3c16ad9..20ce6e4 100644 --- a/src/UtilityFunction.java +++ b/src/UtilityFunction.java @@ -44,14 +44,15 @@ public final class UtilityFunction { } // start playing a sound that is located at filePath - public static void playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException { - Sound sound = new Sound(filePath); - sound.clip.addLineListener(e -> { + public static SoundWrapper playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException { + SoundWrapper sound = new SoundWrapper(filePath); + sound.sound.clip.addLineListener(e -> { // close clip after sound is done playing if (e.getType() == LineEvent.Type.STOP) { - sound.close(); + sound.sound.close(); } }); - sound.start(); + sound.sound.start(); + return sound; } }