我昨天刚开始制作 Slick2D 游戏,一切都很好,直到我添加了声音。游戏本身在 Eclipse 中运行良好,并且声音按预期播放。但是,当我将其编译成 jar 文件,然后使用 JarSplice 编译成胖 jar 时,生成的可运行 jar 会在几分之一秒内打开游戏窗口,然后崩溃。如果我从命令提示符运行 jar,我可以看到返回的错误是:
错误:找不到资源:Assets/sound/THRUST.wav Java.lang.RuntimeException:找不到资源:Assets/sound/THRUST.wav
...等等。我检查了jar文件,所有声音文件都成功添加了。我还尝试在没有声音代码的情况下构建 jar,它工作正常,因此图像资源正确加载。它只存在健全资产的问题。这是我的代码:
package starMiner;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.*;
public class StarMiner extends BasicGame
{
static AppGameContainer app;
static int resX=800;
static int resY=600;
Image bg = null;
Image playerShip = null;
Image shipMove = null;
Image shipIdle = null;
float x = 0;
float y = 0;
float scale = 1.0f;
float xMomentum = 0;
float yMomentum = 0;
float speed = 0;
Sound fuel,engine;
public StarMiner()
{
super("StarMiner");
}
@Override
public void init(GameContainer gc) throws SlickException
{
bg = new Image("Assets/stars.png");
playerShip = new Image("Assets/starship.png");
shipIdle = new Image("Assets/starship.png");
shipMove = new Image("Assets/starshipgo.png");
fuel = new Sound("Assets/sound/THRUST.wav");
engine = new Sound("Assets/sound/engine.wav");
}
@Override
public void update(GameContainer gc, int delta) throws SlickException
{
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_LEFT))
{
playerShip.rotate(-0.1f * delta);
}
if(input.isKeyDown(Input.KEY_ESCAPE))
{
app.exit();
}
if(input.isKeyDown(Input.KEY_F4))
{
app.setFullscreen(!app.isFullscreen());
}
if(input.isKeyDown(Input.KEY_RIGHT))
{
playerShip.rotate(0.1f * delta);
}
if(input.isKeyDown(Input.KEY_UP))
{
playerShip.setTexture(shipMove.getTexture());
float hip = 0.4f * delta;
float rotation = playerShip.getRotation();
xMomentum+= hip * Math.sin(Math.toRadians(rotation));
yMomentum-= hip * Math.cos(Math.toRadians(rotation));
if(!fuel.playing()){fuel.play();}
}
else
{
fuel.stop();
playerShip.setTexture(shipIdle.getTexture());
}
if(input.isKeyDown(Input.KEY_DOWN))
{
float hip = 0.4f * delta;
float rotation = playerShip.getRotation();
xMomentum-= hip * Math.sin(Math.toRadians(rotation));
yMomentum+= hip * Math.cos(Math.toRadians(rotation));
}
x+=xMomentum/800;
y+=yMomentum/800;
xMomentum-=(xMomentum)/1300;
yMomentum-=(yMomentum)/1300;
if(Math.abs(xMomentum)<.000001){xMomentum=0;}
if(Math.abs(yMomentum)<.000001){yMomentum=0;}
speed = (float) Math.sqrt((0-xMomentum)*(0-xMomentum) + (0-yMomentum)*(0-yMomentum));
if(!engine.playing()){engine.play();}
}
@Override
public void render(GameContainer gc, Graphics g) throws SlickException
{
int xDif = 640;
int yDif = 480;
int pModx = (int) ((x*.8)%xDif);
int pMody = (int) ((y*.8)%yDif);
int bgExt = 4*(resX/800);
for(int i = -(bgExt/2)+1; i <= bgExt/2; i++)
{
for(int j = -(bgExt/2)+1; j <= bgExt/2; j++)
{
bg.draw(((resX/2)-16)-pModx-(xDif*i), ((resY/2)-16)-pMody-(yDif*j));
}
}
bg.draw(((resX/2)-16)-pModx, ((resY/2)-16)-pMody);
bg.draw(((resX/2)-16)-pModx-xDif, ((resY/2)-16)-pMody);
bg.draw(((resX/2)-16)-pModx-xDif, ((resY/2)-16)-pMody-yDif);
bg.draw(((resX/2)-16)-pModx, ((resY/2)-16)-pMody-yDif);
playerShip.draw(((resX/2)-16), ((resY/2)-16), scale);
g.drawString("x: "+x, 10, 50);
g.drawString("y: "+y, 10, 70);
g.drawString("xMomentum: "+xMomentum, 10, 90);
g.drawString("yMomentum: "+yMomentum, 10, 110);
g.drawString("angle: "+playerShip.getRotation(), 10, 130);
g.drawString("speed: "+speed, 10, 150);
}
public static void main(String[] args) throws SlickException
{
app = new AppGameContainer(new StarMiner());
app.setDisplayMode(resX, resY, false);
app.start();
}
}
提前致谢!