我得到了;
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Screen(int, int, String, Game) is undefined for the type Game
at Game.<init>(Game.java:15)
at Game.main(Game.java:94)
我将程序从JGrasp切换到Eclipse。复制并粘贴所有代码和类后,在尝试运行该程序时遇到上述错误。在JGrasp中,此代码运行良好,但是由于某些原因Eclipse不喜欢它。
这是游戏类的完整代码,希望这将有助于显示异常发生的原因。我拥有的其他类包括ID,Handler,GameObject,KeyInput和Player。每个都与他们的名字有关。希望这会有所帮助
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
//Creates background window size and holds objects by handler
public Game() {
Screen(1280, 720, "Deed", this);
start();
handler = new Handler();
addKeyListener(new KeyInput(handler));
handler.addObject(new Player(425, 745, ID.Player, handler));
}
//starts a new thread
private void start() {
isRunning = true;
thread = new Thread(this);
thread.start();
}
// Stops current thread, and catches exceptions
private void stop() {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Infinite game loop
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
//updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frames = 0;
//updates = 0;
}
}
stop();
}
public void tick() {
handler.tick();
}
//Holds extra frames before showing (3 extra)
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(5);
return;
}
Graphics g = bs.getDrawGraphics();
/////////////////Renders background first, then handlers///////////////////
g.setColor(Color.black);
g.fillRect(0,0,1280,720);
handler.render(g);
/////////////////Updates graphics////////////////////
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Game();
}
}
这是Screen类;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Screen {
public Screen(int width, int height, String title, Game Game) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.add(Game);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
答案 0 :(得分:0)
如评论中所述,有一些关于代码的指针(虽然有更多代码可以使用):
Window(int, int, String, Game)
),您需要使用具有此签名的版本重载该构造函数,或者您正在使用参数类型错误的现有模型之一Window myWindow = Window(int, int, String, Game)
)addKeyListener
?答案 1 :(得分:-1)
我认为您要导入的Window
类与您粘贴的类不同。 java.awt.Window
仔细检查导入(并确保粘贴同样的内容,我们可以检查)。此错误没有其他解释。