本文介绍了在Eclipse上按“运行"时,无法同时启动游戏和音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以播放音乐(在线找到):

I have this code to play music (found online):

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

public class MusicBackground {
    public static void main(String[] args) throws Exception {


        URL url = MusicBackground.class.getResource("backgroundMusic.wav");
        AudioClip clip = Applet.newAudioClip(url);
        clip.play();
        Thread.sleep(1000);
        clip.loop();
    }
}

单独运行即可.但问题是,在将其实现到游戏中之后,它要么在我启动音乐课程时播放音乐,要么当我运行整个游戏时,它在没有音乐的情况下运行游戏.这是我的游戏的Boot类:

It works fine alone. But the thing is that after I implemented it into my game, it either plays the music when I'm launching the music class, or when I run the entire game, it runs the game without the music. Here is my Boot class for my game:

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();
    }
}

我知道音乐背景类位于public static void main中.但是如何将其实现到引导类中?

I know that the music background class is in public static void main. But how do I implement it into the boot class ?

推荐答案

更改背景音乐类以实现可运行:

Change your background music class to implement 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调用将导致整个程序进入睡眠状态(因为它目前是一个线程).因此,这就是您的主要方法现在的样子:

Then you can spawn a thread for the background music in your game's main thread. If you just direct call or paste that background music code in the main game loop, then the .sleep call will cause the entire program to sleep (as it is currently one thread). So, this is what your main method will look like now:

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();
}
}

这篇关于在Eclipse上按“运行"时,无法同时启动游戏和音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:31