0
我想要動畫一個精靈。有一個包含玩家對象的主類。這個播放器對象擴展了一個精靈對象。玩家對象將它的圖像數組賦給精靈類,這應該在不同的圖像之間切換。 但現在,每當我試圖用while()寫東西時,我得到一個異常? 一切工作正常,但我不能得到任何while循環開始......While循環導致異常?
遊戲類繪製圖像:
g2d.drawImage(player.getImage(),(int) player.getX(),(int) player.getY(), null);
播放器擴展了Sprite類。這包含x和y座標。
public Player(double x, double y) {
bitmap = new Bitmap("/sprites.png");
sprites = new BufferedImage[2];
sprites[0] = bitmap.getSprite(2, 2, 48, 48);
sprites[1] = bitmap.getSprite(52, 2, 48, 48);
this.x = x;
this.y = y;
setImages(sprites);
}
這就是玩家類的構造函數。
但是,爲什麼我不能使用while循環?
你需要更多的代碼嗎?
這裏是我的例外
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.ochs.game.Game.paint(Game.java:59)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
這裏的game.java代碼:
public class Game extends JPanel implements Runnable{
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
public boolean isRunning;
private Player player;
public Game() {
start();
}
public void start() {
isRunning = true;
new Thread(this).start();
}
public void stop() {
isRunning = false;
}
public void run() {
init();
while(isRunning) {
update();
repaint();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
System.out.println("Failed to sleep");
}
}
}
public void init() {
player = new Player(100, 100);
}
public void update() {
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(player.getImage(),(int) player.getX(),(int) player.getY(), null);
}
public static void main(String[] args) {
Game gameComponent = new Game();
Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
JFrame frame = new JFrame("Invaders");
frame.setVisible(true);
frame.setSize(size);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(gameComponent);
}
}
粘貼Game.Java代碼。 –
將game.java代碼添加到問題 – user2410644
什麼是59行? – Pavlos