0

我正在尝试为精灵设置动画。有一个包含播放器对象的主类。这个播放器对象扩展了一个精灵对象。播放器对象将其图像数组提供给精灵类,这应该在不同图像之间切换。但是现在,每当我试图用 while() 写东西时,我都会得到一个异常?一切正常,但我无法启动任何 while 循环......

游戏类绘制图像:

g2d.drawImage(player.getImage(),(int) player.getX(),(int) player.getY(),  null);

播放器扩展了精灵类。这包含 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);
}
}
4

1 回答 1

0

我意识到,在一个while循环中启动一个while循环不会同时运行......我应该尝试用“if”代替......从我的错误中吸取教训:D

于 2014-02-12T22:49:53.453 回答