1

我正在尝试使控件适用于用 Java 编写的 2D 游戏。但是,我在运动中遇到了一个奇怪的错误。每当我在射击时尝试使用向上和向左控件(空格键)或在射击时使用向下和向右控件时,游戏都无法检测到空格键的按下。以下是相关代码摘录:

import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;

public class Player {
    private String playerPath = "images/craft.png";
    private int x, y, dx, dy;
    private Image image;
    private ArrayList missiles;
    private final int CRAFT_SIZE = 20;

    public Player() {
        ImageIcon ii = new ImageIcon(playerPath);
        image = ii.getImage();
        missiles = new ArrayList();
        x = 20;
        y = 20;
    }
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_SPACE) {
            fire();
        }
        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
        }
        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }
        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }
        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }
    }
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }
        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }
        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }
        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
    public void fire() {
        missiles.add(new Projectile(x + CRAFT_SIZE, y + CRAFT_SIZE/2));
    }
}//end class Player

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game extends JPanel implements ActionListener {
    private Player player;
    private Timer timer;

    private int winHeight;
    private int winWidth;
    private final int UP = KeyEvent.VK_UP;
    private final int DOWN = KeyEvent.VK_DOWN;
    private final int LEFT = KeyEvent.VK_LEFT;
    private final int RIGHT = KeyEvent.VK_RIGHT;
    private final int SPACE = KeyEvent.VK_SPACE;

    public Game(int w, int h) {
        addKeyListener(new TAdapter());
        setBackground(Color.BLACK);
        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);

        player = new Player();

        winHeight = h;
        winWidth = w;

        timer = new Timer(5, this);
        timer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(player.getImage(), player.getX(), player.getY(), this);

        ArrayList proj = player.getMissiles();

        for (Object p : proj) {
            Projectile b = (Projectile) p;
            g2d.drawImage(b.getImage(), b.getX(), b.getY(), this);
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // Update projectile locations
        ArrayList ms = player.getMissiles();

        for (int i = 0; i < ms.size(); i++) {
            Projectile p = (Projectile) ms.get(i);
            if (p.getX() <= winWidth) {
                p.move();
            } else {
                ms.remove(p);
            }
        }
        player.move();
        repaint();
    }

    private class TAdapter extends KeyAdapter {
        @Override
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }
    }// end class TAdapter
}// end class Game
4

1 回答 1

3

每当我在射击时尝试使用向上和向左控件(空格键)或在射击时使用向下和向右控件时,游戏都无法检测到空格键的按下。

仅针对最后按下的键生成 KeyEvents。如果要处理同时按下的多个键,则需要:

  1. 跟踪已按下/释放的键
  2. 使用 Swing Timer 来安排动画。

然后,每当计时器触发时,您检查当前按下了哪些键并为该键执行操作。

于 2013-04-07T17:28:01.240 回答