Slick2D runnable jar(JarSplice)找不到声音资产? “资源未找到”错误

时间:2013-02-24 21:12:32

标签: exception audio jar resources slick2d

我昨天刚刚开始制作Slick2D游戏,一切都很顺利,直到我添加了声音。游戏本身在日食中运行良好,声音按预期播放。但是,当我将它编译成一个jar文件,然后用JarSplice编译成一个胖jar时,生成的runnable jar打开游戏窗口几分之一秒然后崩溃。如果我从命令提示符运行jar,我可以看到返回的错误是:

错误:未找到资源:资产/声音/ 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();
  }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您尝试将Asset文件夹放在generate .jar旁边(与.jar在同一文件夹中,而不在.jar中),该怎么办?