需要帮助将我的按键听众从 main 中移除

need help taking my key press listeners out of main

我已经开始制作一款可以自由移动的游戏了。你用你的键盘移动,我让它工作,但我想做的就是把它从 main 中取出,这样我就可以调用 if multiply times from different 类。我试图将它放在 public static void 中并从 main 调用该方法,但它不起作用。 我的代码在下面提供。我一直坚持这个很长一段时间知道。

我在框架中放了一张图片以使其可见图片名为 1。

import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
public class StartGame {
    public static void main(String[] argv) throws Exception {
        JFrame MainFrame = new JFrame();
        MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainFrame.setSize(1210, 700);
        MainFrame.setLocation(new java.awt.Point(150, 30));
        MainFrame.setLayout(null);
        MainFrame.setFocusable(true);
        MainFrame.setFocusTraversalKeysEnabled(true);
        MainFrame.setIconImage(new ImageIcon("images\sword.png").getImage());
        JLabel thing = new JLabel();
        thing.setIcon(new ImageIcon("image\1.gif"));
        thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

        MainFrame.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                    if (0 < thing.getY()) { 
                        thing.setLocation(thing.getX(), thing.getY() - 10);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
                    if (thing.getY() < 570) {
                        thing.setLocation(thing.getX(), thing.getY() + 10);
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
                    if (thing.getX() < 1110) {
                        thing.setLocation(thing.getX()+ 10, thing.getY());
                    }
                }
                if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {        
                    if (0 < thing.getX()) {
                        thing.setLocation(thing.getX() - 10, thing.getY());
                    }
                }

            }

            public void keyTyped(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {


            }
        });

        MainFrame.add(thing);
        MainFrame.setVisible(true);
    }
}

不要用KeyListener,用key bindings API。使用键绑定 API 将允许您在 "window" 级别响应键事件,而不是组件级别(如果您愿意)

例如...

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

public class StartGame {

    public static void main(String[] argv) throws Exception {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(1210, 700);
        mainFrame.setLocation(new java.awt.Point(150, 30));
        mainFrame.setLayout(null);
        mainFrame.setFocusable(true);
        mainFrame.setFocusTraversalKeysEnabled(true);
        mainFrame.setIconImage(new ImageIcon("images\sword.png").getImage());
        JLabel thing = new JLabel("X");
        //thing.setIcon(new ImageIcon("image\1.gif"));
        thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

        InputMap inputMap = thing.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = thing.getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "move.up");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "move.up");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "move.down");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "move.down");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "move.left");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "move.left");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "move.right");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "move.right");

        actionMap.put("move.up", new ThingAction(thing, new Point(0, -1)));
        actionMap.put("move.down", new ThingAction(thing, new Point(0, 1)));
        actionMap.put("move.left", new ThingAction(thing, new Point(-1, 0)));
        actionMap.put("move.right", new ThingAction(thing, new Point(1, 0)));

        mainFrame.add(thing);
        mainFrame.setVisible(true);
    }

    public static class ThingAction extends AbstractAction {
        private JLabel thing;
        private Point delta;

        public ThingAction(JLabel thing, Point delta) {
            this.thing = thing;
            this.delta = delta;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            thing.setLocation(thing.getX() + delta.x, thing.getY() + delta.y);
        }
    }
}

这是一个非常基本的示例,但它演示了两个关键点:

  1. 您可以为单个操作分配多个键
  2. 您可以将输入(即键盘)与实现分离,这意味着您可以使用相同的基础 Action 来处理鼠标输入或操纵杆输入

首先你需要使用游戏循环模式。 游戏循环模式是以特定顺序处理游戏事件的循环。首先,您处理输入、更新对象并最后渲染屏幕。 此图像是该渲染的示例:

如果您在项目开始时不遵循此模式,那么后期您会遇到更多困难。

使用此模式,您可以创建一个 class 游戏,其中包含调用输入句柄 class 的方法更新。有了这个,您可以将输入句柄移动到您可以创建的另一个 class。一些例子:

class Game {

    void update(){
        InputManager.handleInput();
        updateObjects();
        render();

    }
}


class InputManager {

    void handleInput(){
        //do whatever you like

    }

}

请阅读此博客以了解更多信息:https://gamesprogramminggroup02.wordpress.com/2016/03/04/design-patterns-game-loop/