0

我一直在关注 Buckys java 游戏开发,并使用 slick 进行游戏的基本设置,从那时起我就将它掌握在自己手中。目前我只是在构建菜单,它几乎完成了,除了我想给它添加声音。我已经坚持了一个星期,所以它不像我没有为此做过任何研究,我只是无法让它发挥作用。现在我找到了一些代码来执行此操作,这似乎很有意义,我该如何实现它,或者如果您对如何添加一些声音有更好的想法,不胜感激。

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

static String soundtrack = "res/doxx.wav";

public void sound(String path){

    try{
        AudioInputStream audio = AudioSystem.getAudioInputStream(Menu.class.getResource(path));
        Clip clip = AudioSystem.getClip();
        clip.open(audio);
        clip.start();
    } catch (Exception e){
        System.out.println("check "+path+"\n");
        e.printStackTrace();
    }
}

编辑:此代码现在已从程序中取出,但请随时根据它发布答案

这是到目前为止与添加声音相关的代码,这是我的菜单类的全部,它不是很大。老实说,您可能不需要详细查看它,它目前完全基于图形和两个正在旋转的按钮(开始和退出)。

package javagame;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;


public class Menu extends BasicGameState {

double pi=3.14159265359;
float beanPosY = 330;
float beanPosX = 70;
double gravity = 0.01;
double angleStart=1.5*pi;
double angleQuit=0.5*pi;
int radius=120;
int centerX=300;
int centerY=160;
float startPosX = (float) (centerX + Math.sin(angleStart)*radius);
float startPosY = (float) (centerY + Math.cos(angleStart)*radius);
float quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
float quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);
double force = 0;
public Menu(int state){
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    Sound music = new Sound();
    music.playBackGround("res/doxx.wav");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{     
    Image background = new Image("res/background640x480.fw.png");
    g.drawImage(background, 0, 0);

    Image start = new Image("res/Start100x100.fw.png");
    Image quit = new Image("res/quit100x100.fw.png");
    start.draw(startPosX,startPosY);
    quit.draw(quitPosX,quitPosY);

    Image grass = new Image("res/grass640x150.fw.png");
    g.drawImage(grass,0,340);

    Image bean = new Image("res/bean.jpg");
    bean.draw(beanPosX, beanPosY);
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    int posX = Mouse.getX();
    int posY = Mouse.getY();
    double constant=0.002*pi;

    startPosX = (float) (centerX + Math.sin(angleStart)*radius);
    startPosY = (float) (centerY + Math.cos(angleStart)*radius);
    quitPosX = (float) (centerX + Math.sin(angleQuit)*radius);
    quitPosY = (float) (centerY + Math.cos(angleQuit)*radius);

    angleStart+=constant;
    angleQuit+=constant;
    if (angleStart>=2*pi){
        angleStart-=2*pi;
    }
    if (angleQuit>=2*pi){
        angleQuit-=2*pi;
    }
    //button interactions
    menuInteraction(posX,posY,sbg);


    if (beanPosY>=330){
        force=1;
    }
    beanPosY-=force;
    force-=gravity;
}

public int getID(){
    return 0;
}

private void menuInteraction(int posX, int posY, StateBasedGame sbg){
    //play button
    float startXDist=posX-(startPosX+50);
    float startYDist=(480-posY)-(startPosY+50);
    float startDist=(float) Math.sqrt((startXDist*startXDist)+(startYDist*startYDist));
    if(startDist<=50){
        if(Mouse.isButtonDown(0)){
            sbg.enterState(1);
        }
    }

    //quit button
    float quitXDist=posX-(quitPosX+50);
    float quitYDist=(480-posY)-(quitPosY+50);
    float quitDist=(float) Math.sqrt((quitXDist*quitXDist)+(quitYDist*quitYDist));
    if(quitDist<=50){
        if(Mouse.isButtonDown(0)){
            System.exit(0);
        }
    }
}

}

编辑:上面的代码现在在“init()”方法中创建一个对象,该方法调用“声音”类及其方法“playBackGround()”。Sound 类的代码在下面来自 JavaNewb 的答案中给出。

编辑:此代码产生的错误如下:

java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at javagame.Sound.run(Sound.java:45)
at java.lang.Thread.run(Unknown Source)

最后一点,我尝试过诸如 LWJGL 库之类的东西,我无法理解它们。我也意识到我要问多少,因为我基本上是在要求有人将其编码到我的游戏中,我希望我不必这样做,但我不知道如何解决我的问题而不将其发布在这里.

PS。不是我自己没有尝试过,所以不要痛苦,说我没有努力

4

2 回答 2

0

当您尝试从空 URL 创建 AudioInputStream 时会发生此错误。确保 url 不为空,确保您引用了正确的文件。

于 2014-02-06T15:49:41.983 回答
0
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;

public class Sound implements Runnable
{
    private boolean running = false;
    private Thread thread;

    public Sound()
    {
        this.start();
    }

    public void start()
    {
        if(running)
            return;
        this.thread = new Thread(this);
        this.running = true;
        this.thread.start();
    }

    //
    private boolean playSong = false;
    private AudioInputStream inputStream;
    private String url;
    private Clip clip;

    @Override
    public void run()
    {
        while(running)
        {
            if(inputStream == null && playSong)
            {
                this.playSong = false;
                try
                {
                    this.inputStream = AudioSystem.getAudioInputStream(Sound.class.getResource(url));
                    this.clip.open(inputStream);
                    this.clip.loop(10);
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    public void playBackGround(String string) // call to play .wav file
    {
        if(this.clip != null)
        {
            this.clip.stop();
            this.clip.close();
        }
        try
        {
            this.clip = AudioSystem.getClip();
        }
        catch(LineUnavailableException e)
        {
            e.printStackTrace();
        }
        url = string + ".wav";
        this.playSong = true;
        this.inputStream = null;
    }

    public void disposeSound()
    {
        if(this.clip != null)
        {
            this.clip.stop();
            this.clip.close();
        }
        this.clip = null;
        this.playSong = false;
        this.inputStream = null;
    }
}
于 2013-07-18T19:15:46.963 回答