我正在学习编程,所以我在上其中一门课。在上课期间,一切进展顺利,直到添加小精灵为止,我已经尝试了很多类似使用此代码的方法
我尝试使用以下代码:How do I add a resources folder to my Java project in Eclipse
这是JAVA
中的完整代码:
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static JFrame frame;
private boolean isRunning = true;
private Thread thread;
private final int WIDTH = 160;
private final int HEIGHT = 120;
private final int SCALE = 3;
private BufferedImage image;
public Spritesheet sheet;
public List<Entity> entities;
public Game(){
this.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
frame = new JFrame();
frame.add(this);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//iniciando objetos\\
entities = new ArrayList<Entity>();
sheet = new Spritesheet("/spritesheet.png");
Player player = Player(0,0,16,16, spritesheet.getSprite(33, 0, 16,16));
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop(){
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();}
}
public static void main (String args[]) {
Game game = new Game();
game.start();
}
public void tick(){
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.tick();
}
}
public void render () {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(19,19,19,19));
g.fillRect(0,0, WIDTH, HEIGHT);
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, WIDTH*12, HEIGHT*12, null);
bs.show();
for(int i = 0; i < entities.size(); i++) {
Entity e = entities.get(i);
e.render(g);
}
}
public void run() {
long lastTime = System.nanoTime();
double amountofticks = 60.0;
double ns = 1000000000 / amountofticks;
double delta = 0;
int frame = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta+= (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick();
render();
frame++;
delta--;
}
if(System.currentTimeMillis() - timer >= 1000) {
System.out.println("FPS: " + frame);
frame = 0;
timer+=1000;
}
}
stop();
}
}
控制台上显示的错误:
线程“主”中的异常java.lang.Error:未解决的编译 问题:无法解决Spritesheet。