【发布时间】:2014-04-16 18:01:24
【问题描述】:
所以我继续像往常一样开始创作游戏。除了这次我得到一个错误 O.o.我试图找到答案,但我认为也许在这里我可以得到一个很好的答案!这是错误:
描述资源路径位置类型 无法对非静态字段图像 Game.java /POGA/src/packagehere 第 71 行 Java 问题进行静态引用
描述资源路径位置类型 无法从 Canvas Game.java /POGA/src/packagehere 第 66 行 Java 问题中对非静态方法 createBufferStrategy(int) 进行静态引用
我得到了更多这些类型,但我想如果你向我和这个问题的观众展示如何修复这些错误,我们将能够修复其余的“相同”错误......
package packagehere;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public static boolean running = false;
JFrame frame;
public static String title = "POGA game thingy - 0.1 Aplha";
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final Dimension gameDim = new Dimension(WIDTH, HEIGHT);
synchronized void start() {
Thread thread = new Thread();
thread.start();
running = true;
}
public void run() {
while(running) {
tick();
render();
}
}
synchronized void stop() {
running = false;
System.exit(0);
}
public Game() {
setMaximumSize(gameDim);
setMinimumSize(gameDim);
setPreferredSize(gameDim);
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.requestFocus();
}
public static void tick() {
}
public static void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
bs = createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
}
【问题讨论】:
-
为什么你有这么多标记为“静态”的东西?这看起来不对。如果你想说
Game game = new Game()然后在game上调用方法,不要使用static。 -
错误信息说明了一切......您不能从静态块/方法中引用非静态字段。如果静态修饰符包含在字段或方法声明中,则不需要使用该类的实例。字段或方法与类相关联,而不是与单个对象相关联。也看看stackoverflow.com/questions/17622088/…
-
很可能是
render() should *not* bestatic`。 -
@Potatolng 请回答
on what object are you calling getWidth() methods in render method?
标签: java static non-static