0

所以我正在用 Java 编写一个简单的格斗游戏,其中包括标题屏幕 JPanel 和 Playspace JPanel。添加标题屏幕后,我在让 Playspace 的 KeyListener 工作时遇到了一些问题。我已经尝试了其他 SO 帖子中的许多其他建议,但到目前为止都没有看到任何工作。下面是我的 Playspace 构造函数,int 模式是单人或多人,app 是 JFrame。需要注意的是,标题和播放屏幕都在同一帧中。

/** Constructor, sets frame and arrays up **/
    public Playspace(int mode, Application app) {

        // Sets up JPanel
        super();

        // Application variables
        this.app = app;
        HEIGHT = app.getHeight();
        WIDTH = app.getWidth();
        setLayout(null);
        setLocation(0, 0);
        setSize(WIDTH, HEIGHT);
        backgroundColor = new Color(50, 50, 60);
        backgroundColor.brighter();
        setBackground(backgroundColor);
        setVisible(true);
        setFocusable(true);
        requestFocusInWindow();
        addKeyListener(new KeyListener() {
            /** Keypress detection **/
            @Override
            public void keyPressed(KeyEvent e) {

                int i = 0;
                if (runnable[i]) {
                    // WASD Controls
                    if (e.getKeyCode() == KeyEvent.VK_A) {
                        players[i].setLeftPressed(true);
                        players[i].setDirection(-1);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_D) {
                        players[i].setRightPressed(true);
                        players[i].setDirection(1);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_S) {
                        players[i].setDownPressed(true);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_W) {
                        players[i].setUpPressed(true);
                        players[i].setUpReleased(false);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT
                            && players[i].isShiftReleased()) {
                        players[i].setShiftPressed(true);
                        players[i].setShiftReleased(false);
                    }
                }

                i = 1;
                if (runnable[i]) {
                    // ULDR Controls
                    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                        players[i].setLeftPressed(true);
                        players[i].setDirection(-1);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                        players[i].setRightPressed(true);
                        players[i].setDirection(1);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        players[i].setDownPressed(true);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_UP) {
                        players[i].setUpPressed(true);
                        players[i].setUpReleased(false);
                    }

                    if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT
                            && players[i].isShiftReleased()) {
                        players[i].setShiftPressed(true);
                        players[i].setShiftReleased(false);
                    }
                }
            }

            /** Key Release detection **/
            @Override
            public void keyReleased(KeyEvent e) {

                if (e.getKeyCode() == KeyEvent.VK_F11) {
                    developerMode = !developerMode;
                }

                int i = 0;
                // WASD Controls
                if (e.getKeyCode() == KeyEvent.VK_A) {
                    players[i].setLeftPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_S) {
                    players[i].setDownPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_D) {
                    players[i].setRightPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_W) {
                    players[i].setUpPressed(false);
                    players[i].setUpReleased(true);
                }
                if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT) {
                    players[i].setShiftPressed(false);
                    players[i].setShiftReleased(true);
                }

                i = 1;
                // ULDR Controls
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    players[i].setUpPressed(false);
                    players[i].setUpReleased(true);
                }

                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    players[i].setDownPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    players[i].setLeftPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    players[i].setRightPressed(false);
                }

                if (e.getKeyCode() == KeyEvent.VK_SHIFT && e.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT) {
                    players[i].setShiftPressed(false);
                    players[i].setShiftReleased(true);
                }
            }

            /** We need this, but would rather forget it... **/
            @Override
            public void keyTyped(KeyEvent e) {
            }
        });
        setVisible(true);

**它从这里继续,但这是整个设置

感谢所有可以提供帮助的人

还要注意,我尝试向 KeyListener 添加一些打印语句,但无济于事,从未调用过侦听器。

4

0 回答 0