1

I am attempting to make a song play in the main menu of my game but when the user plays the game (gets out of the main menu) the music stops. So for this I attempted to use a while loop. I have a boolean labeled, 'startPressed' that is updated to true when the user exits the main menu. However when using this public static boolean in my while loop my while loop never ends. Here is my code:

In the beginning of the game:

try {
    Clip clip = AudioSystem.getClip();
    AudioInputStream inputStream = AudioSystem.getAudioInputStream(
            Game.class.getResourceAsStream("/mymusic.wav"));
    clip.open(inputStream); // gets the audio ready
    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
    gainControl.setValue(-22.0f); // lowers the volume.
    clip.start(); // plays song (yes i know it only plays it once ill fix that later)
    while(!startPressed) {
    } // doesn't end
    clip.close();
    clip.stop();
} catch (Exception e) {
    System.err.println(e.getMessage());
}

The boolean I am referring to:

public static boolean startPressed = false;

What makes the variable update to true:

if (key == KeyEvent.VK_ENTER) {
    if (Game.startPressed == false) {
        Game.startPressed = true;
    }
}

Sorry for the long question I tried to put as little as possible.

Zeeen
  • 312
  • 1
  • 2
  • 13

0 Answers0