1
 public void paint (Graphics g)
{
    bufferGraphics.setColor (Color.black);
    bufferGraphics.clearRect (0, 0, dim.width, dim.width);
    while (input != KeyEvent.VK_SPACE)
    {
        bufferGraphics.fillRect (0, 0, dim.width, dim.height);
    }

    bufferGraphics.drawImage (track, 0, 0, dim.width, dim.height, this);
    bufferGraphics.setFont (new Font ("Calibri", Font.PLAIN, 25));
    bufferGraphics.drawString ("Level: " + level, 30, 30);
    bufferGraphics.drawImage (car, 620, myCarY, 70, 120, this);
    bufferGraphics.drawImage (opponent, 415, oppCarY, 70, 120, this);
    move ();

这是现在的代码。执行时,我得到一个甚至无法关闭的冻结空白窗口。

4

1 回答 1

1

您的问题在您的 if 语句中。

if(run = false)

永远不会执行,因为 assignment 返回被分配的值(例如 false)。

您需要将您的更改===.

您可能还想将无限 for 循环更改为 while 循环,例如

while(input != KeyEvent.VK_SPACE) {
}

还要确保将您的 KeyListener 添加到您的课程中(在 ctor 中)

addKeyListener(new MyKeyListener())

我刚刚测试了代码,它工作正常。

于 2012-06-14T20:12:12.273 回答