所以我的程序中有一行代码确实不起作用。在游戏课里面。在main方法中调用game类。
问题就出在代码的底部。g、 setcolor(颜色:黑色);有时不起作用。有时它的工作如预期,我会再次运行它,它不会。
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 = 1088212668520606240L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
private Handler handler;
public Game() {
handler = new Handler();
this.addKeyListener(new KeyInput(handler));
handler.addObject(new Player(WIDTH/2-32, HEIGHT/2-32, ID.Player));
handler.addObject(new Player(WIDTH/2+64, HEIGHT/2-32, ID.Player2));
new Window(WIDTH, HEIGHT, "Ino", this);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
this.render();
}
public synchronized void stop() {
try {
thread.join();
running = false;
}
catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS; " + frames);
frames = 0;
}
}
stop();
}
private void tick() {
handler.tick();
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Game();
}
}
看起来很随意。它会像预期的那样连续工作几次,然后甚至在不更改代码的情况下也不会工作。有人知道密码有什么问题吗。处理程序对象不会像图形g那样出现。它甚至不显示错误。只是有时候根本就不会出现。
暂无答案!
目前还没有任何答案,快来回答吧!