Java 按';时无法启动游戏和音乐(同时启动);运行';日食

Java 按';时无法启动游戏和音乐(同时启动);运行';日食,java,eclipse,background-music,Java,Eclipse,Background Music,我有此代码来播放音乐(在线找到): 它单独工作很好。但问题是,在我将其应用到我的游戏中之后,它要么在我启动音乐类时播放音乐,要么在我运行整个游戏时,它在不播放音乐的情况下运行游戏。以下是我的游戏引导类: import static helpers.Artist.BeginSession; import org.lwjgl.opengl.Display; import helpers.Clock; import helpers.StateManager; public class Boot {

我有此代码来播放音乐(在线找到):

它单独工作很好。但问题是,在我将其应用到我的游戏中之后,它要么在我启动音乐类时播放音乐,要么在我运行整个游戏时,它在不播放音乐的情况下运行游戏。以下是我的游戏引导类:

import static helpers.Artist.BeginSession;
import org.lwjgl.opengl.Display;
import helpers.Clock;
import helpers.StateManager;

public class Boot {
    public Boot() {

        //Call static method in Artist class to initialize OpenGL calls
        BeginSession();

        //Main game loop
        while (!Display.isCloseRequested()) {
            Clock.update();
            StateManager.update();
            Display.update();
            Display.sync(60);
        }
        Display.destroy();
    }

    public static void main(String[] args) {
        new Boot();
    }
}

我知道音乐背景课是公开的。但是如何将其实现到启动类中?

更改背景音乐类以实现runnable:

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.lang.Runnable;

public class MusicBackground implements Runnable {

public void run() throws Exception {
URL url = MusicBackground.class.getResource("backgroundMusic.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
Thread.sleep(1000);
clip.loop();
}
}
然后你可以在游戏的主线程中为背景音乐生成一个线程。如果您只是直接调用或将背景音乐代码粘贴到主游戏循环中,那么.sleep调用将导致整个程序休眠(因为它当前是一个线程)。这就是你现在的主要方法:

import static helpers.Artist.BeginSession;
import org.lwjgl.opengl.Display;
import helpers.Clock;
import helpers.StateManager;

public class Boot {

public Boot() {

    //Call static method in Artist class to initialize OpenGL calls
    BeginSession();
    Thread backgroundPlayer;
    Try {
        backgroundPlayer = new Thread(new MusicBackground());
        backgroundPlayer.start();
    }
    catch(Exception e)
    {
        System.out.println("Problem firing the background thread");
        e.printStackTrace();
    }

    //Main game loop
    while (!Display.isCloseRequested()) {
        Clock.update();
        StateManager.update();
        Display.update();
        Display.sync(60);
    }
    Display.destroy();
}

public static void main(String[] args) {
new Boot();
}
}

您可能需要将音乐代码中的5行复制/粘贴到引导类中。您可能还需要在单独的线程中运行该代码。您可以在启动类中创建一个方法,比如说private static void playMusic(){},并在新的启动()调用之后/之前在主方法中调用它。非常感谢!爱你!
import static helpers.Artist.BeginSession;
import org.lwjgl.opengl.Display;
import helpers.Clock;
import helpers.StateManager;

public class Boot {

public Boot() {

    //Call static method in Artist class to initialize OpenGL calls
    BeginSession();
    Thread backgroundPlayer;
    Try {
        backgroundPlayer = new Thread(new MusicBackground());
        backgroundPlayer.start();
    }
    catch(Exception e)
    {
        System.out.println("Problem firing the background thread");
        e.printStackTrace();
    }

    //Main game loop
    while (!Display.isCloseRequested()) {
        Clock.update();
        StateManager.update();
        Display.update();
        Display.sync(60);
    }
    Display.destroy();
}

public static void main(String[] args) {
new Boot();
}
}