当前位置: 首页 > 知识库问答 >
问题:

ActionListener或Action for playerMovement

曾丰茂
2023-03-14

我只是试着编一个足球比赛的程序,在完成了所有的绘画和初始化球员和球之后,我现在正处于尝试移动球员的位置。我看了一些关于如何在游戏中编程运动的教程,但它们都使用了键盘监听器。正如我在网上写的,KeyListener不是一个好方法。(是否仍有使用KeyListeners的情况?)。然而,我正在努力实现一个平稳、正确的动作。我找不到以“正确”的方式教授这一点的在线资源。我之所以指定“正确”的方式,是因为我从顶级Java程序员那里读到了一些建议。我正在挣扎着用什么做运动,在哪里实现它?我考虑过使用动作并在一个玩家的内部类中实现它,也许?这是正确的方法吗?

以下是我到目前为止的代码:

快球:

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;

public class Speedball{

    public static final int AREA_WIDTH = 1400;
    public static final int AREA_HEIGHT = 700;

    public Speedball() {
    }

    private void start() {
        JFrame mainFrame = new JFrame("Speedball");
        SpeedballPanel panel = new SpeedballPanel();
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setSize(AREA_WIDTH, AREA_HEIGHT);
        mainFrame.add(panel, BorderLayout.CENTER);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        Speedball speedball = new Speedball();
        speedball.start();
    }
}

Speedball面板:

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SpeedballPanel extends JPanel implements ActionListener {

    private Timer timer;
    private Renderer renderer;
    private Player player1;
    private Player player2;

    public SpeedballPanel() {
        initPanel();
        initUserInteractions();
        initTimer();

        this.renderer = new Renderer();
        this.player1 = new Player(300, 300);
        this.player2 = new Player(500, 500);
    }

    private void initPanel() {
        this.setSize(Speedball.AREA_WIDTH, Speedball.AREA_HEIGHT);
    }


    @Override
    public void paintComponent(Graphics g) {
        renderer.drawBackground(g, getWidth(), getHeight());
        renderer.drawPlayer(g, player1.getX(), player1.getY(), player1.getWidth(), player1.getHeight());
        renderer.drawPlayer(g, player2.getX(), player2.getY(), player2.getWidth(), player2.getHeight());
    }

    /*
     * Playermovement
     */
    private void playerMoveUp(Player player) {
        player.setY(player.getY() - 10);
        System.out.println("Player Y: " + player.getY());
    }

    private void playerMoveDown(Player player) {
        player.setY(player.getY() + 10);
        System.out.println("Player Y: " + player.getY());
    }

    private void playerMoveLeft(Player player) {
        player.setX(player.getX() - 10);
        System.out.println("Player X: " + player.getX());
    }

    private void playerMoveRigth(Player player) {
        player.setX(player.getX() + 10);
        System.out.println("Player X: " + player.getX());
    }

    /*
     * Actions for playermovement
     */
    Action player1MoveUp = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveUp(player1);
        }
    };

    Action player1MoveDown = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveDown(player1);
        }
    };

    Action player1MoveLeft = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveLeft(player1);
        }
    };

    Action player1MoveRight = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveRigth(player1);
        }
    };

    Action player2MoveUp = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveUp(player2);
        }
    };

    Action player2MoveDown = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveDown(player2);
        }
    };

    Action player2MoveLeft = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveLeft(player2);
        }
    };

    Action player2MoveRight = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            playerMoveRigth(player2);
        }
    };

    /*
     * Define keys for user interaction
     */
    private void initUserInteractions() {
        //define InputMaps
        this.getInputMap().put(KeyStroke.getKeyStroke("W"), "player1MoveUp");
        this.getInputMap().put(KeyStroke.getKeyStroke("S"), "player1MoveDown");
        this.getInputMap().put(KeyStroke.getKeyStroke("A"), "player1MoveLeft");
        this.getInputMap().put(KeyStroke.getKeyStroke("D"), "player1MoveRight");

        this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "player2MoveUp");
        this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "player2MoveDown");
        this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "player2MoveLeft");
        this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "player2MoveRight");

        //define ActionMaps
        this.getActionMap().put("player1MoveUp", player1MoveUp);
        this.getActionMap().put("player1MoveDown", player1MoveDown);
        this.getActionMap().put("player1MoveLeft", player1MoveLeft);
        this.getActionMap().put("player1MoveRight", player1MoveRight);

        this.getActionMap().put("player2MoveUp", player2MoveUp);
        this.getActionMap().put("player2MoveDown", player2MoveDown);
        this.getActionMap().put("player2MoveLeft", player2MoveLeft);
        this.getActionMap().put("player2MoveRight", player2MoveRight);
    }

    private void initTimer() {
        timer = new Timer(1, this);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
}

渲染器:

package SpeedballMinimal;

import java.awt.*;

public class Renderer {

    public Renderer() {
    }

    public void drawBackground(Graphics g, int areaWidth, int areaHeight) {
        g.setColor(new Color(106, 237, 49));
        g.fillRect(0,0, areaWidth, areaHeight);
    }

    public void drawPlayer(Graphics g, int playerX, int playerY, int playerWidth, int playerHeight) {
        g.setColor(Color.BLACK);
        g.fillOval(playerX, playerY, playerWidth, playerHeight);
    }
}

玩家:

package SpeedballMinimal;

import java.util.Random;

public class Player {

    private int x;
    private int y;
    private int width;
    private int height;
    private int velocity = 2;

    private Random rand = new Random();

    public Player() {
        this.x = 300;
        this.y = 300;
        this.width = 50;
        this.height = 50;
    }

   public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

public int getY() {
    return y;
}

public void setY(int x) {
    this.y = y;
}

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

编辑:

我编辑了我的代码到一个最小的例子。这个代码在我的JPanel上产生一个椭圆形。我试图按照建议用KeyBindings为这个椭圆形创建运动。我使用了以下资源:

  • 如何使用密钥绑定而不是密钥监听器
  • https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html

我现在想解决以下问题:

  • 我不能同时移动两个玩家,我在网上找不到任何方法来解决这个问题。当玩家1移动时,我按下玩家2的向上键,玩家1停止移动。现在我想知道,我如何让两个玩家同时移动

编辑2:

这是我基于c0der答案的新代码

SpeedballPanel(课程已更改):

package SpeedballMinimal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class SpeedballPanel extends JPanel {

    private Renderer renderer;
    private Player player1;
    private Player player2;

    public SpeedballPanel() {
        initPanel();
        initUserInteractions();

        this.renderer = new Renderer();
        this.player1 = new Player(Team.ONE);
        this.player2 = new Player(Team.TWO);
    }

    private void initPanel() {
        this.setSize(Speedball.AREA_WIDTH, Speedball.AREA_HEIGHT);
    }


    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        renderer.drawBackground(g, getWidth(), getHeight());
        renderer.drawField(g, getWidth(), getHeight());
        renderer.drawGoals(g, getWidth(), getHeight());
        renderer.drawPlayer(g, player1.getX(), player1.getY(), player1.getWidth(), player1.getHeight());
        renderer.drawPlayer(g, player2.getX(), player2.getY(), player2.getWidth(), player2.getHeight());
    }

    /*
     * Playermovement
     */
    private void movePlayer(Player player, MovementDirection direction) {
        if (direction == MovementDirection.UP) {
            player.setY(player.getY() - 10);
        }
        if (direction == MovementDirection.DOWN) {
            player.setY(player.getY() + 10);
        }
        if (direction == MovementDirection.LEFT) {
            player.setX(player.getX() - 10);
        }
        if (direction == MovementDirection.RIGHT) {
            player.setX(player.getX() + 10);
        }
        repaint();
    }

    /*
     * Actions for playermovement
     */
    Action player1MoveUp = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player1, MovementDirection.UP);
        }
    };

    Action player1MoveDown = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player1, MovementDirection.DOWN);
        }
    };

    Action player1MoveLeft = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player1, MovementDirection.LEFT);
        }
    };

    Action player1MoveRight = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player1, MovementDirection.RIGHT);
        }
    };

    Action player2MoveUp = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player2, MovementDirection.UP);
        }
    };

    Action player2MoveDown = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player2, MovementDirection.DOWN);
        }
    };

    Action player2MoveLeft = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player2, MovementDirection.LEFT);
        }
    };

    Action player2MoveRight = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            movePlayer(player2, MovementDirection.RIGHT);
        }
    };

    /*
     * Define keys for user interaction
     */
    private void initUserInteractions() {
        //define InputMaps
        this.getInputMap().put(KeyStroke.getKeyStroke("W"), "player1MoveUp");
        this.getInputMap().put(KeyStroke.getKeyStroke("S"), "player1MoveDown");
        this.getInputMap().put(KeyStroke.getKeyStroke("A"), "player1MoveLeft");
        this.getInputMap().put(KeyStroke.getKeyStroke("D"), "player1MoveRight");

        this.getInputMap().put(KeyStroke.getKeyStroke("UP"), "player2MoveUp");
        this.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "player2MoveDown");
        this.getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "player2MoveLeft");
        this.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "player2MoveRight");

        //define ActionMaps
        this.getActionMap().put("player1MoveUp", player1MoveUp);
        this.getActionMap().put("player1MoveDown", player1MoveDown);
        this.getActionMap().put("player1MoveLeft", player1MoveLeft);
        this.getActionMap().put("player1MoveRight", player1MoveRight);

        this.getActionMap().put("player2MoveUp", player2MoveUp);
        this.getActionMap().put("player2MoveDown", player2MoveDown);
        this.getActionMap().put("player2MoveLeft", player2MoveLeft);
        this.getActionMap().put("player2MoveRight", player2MoveRight);
    }
}

MovementDirection(已创建枚举):

package SpeedballMinimal;

public enum MovementDirection {
    UP, DOWN, LEFT, RIGHT
}

注意:剩余的类没有改变

实际上,我不想随机或自动移动player2,我希望它通过箭头键移动,就像我在输入地图的定义中看到的那样。但是我仍然有同样的问题,当我试图用WASD键移动player1,并试图用向上、向下、向左、向右箭头键同时移动player2时,第一个移动的玩家停止移动,另一个玩家开始移动,但是他们不能同时移动。抱歉,如果我在第一篇文章中对玩家的移动不清楚。

共有1个答案

时恩
2023-03-14

从代码中可以假设你想让计时器移动另一个球
根据以下代码应用更改以移动玩家1(关键操作是移动玩家2,尽管您希望它移动玩家1。我没有检查原因)。

private void initTimer() {
    timer = new Timer(500, this);
    timer.start();
}

@Override
public void actionPerformed(ActionEvent e) {
    //move player 1
    randomMovePlayer(player1);
    repaint();
}

//randomly move player
private void randomMovePlayer(Player player) {

    Random rand = new Random(); //refactor it to a field
    float direction = rand.nextFloat();
    if(direction < .25) {
        playerMoveLeft(player);
    }else if(direction < .5) {
        playerMoveUp(player);
    }else if(direction < .75) {
        playerMoveDown(player);
    }else  {
        playerMoveRigth(player1);
    }
    //todo improve logic to get the right movement within bounds
}

另外,在移动后需要重新绘制的四种移动方法中的每一种:

private void playerMoveUp(Player player) {
    player.setY(player.getY() - 10);
    System.out.println("Player Y: " + player.getY());
    repaint(); //repaint after each move
} 

附注:所有四个动作都可以用一种方法来处理:

enum Direction{LEFT, RIGHT, UP, DOWN};

private void movePalyer(Player player, Direction dir) {
    //todo move logic 
}

响应编辑2:
尝试在按下键时开始连续运动(通过启动计时器),并在释放相同的键时停止它。

//do on w key press
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0, false), "player1MoveUp");
//do on w key release
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0, true), "stop");

Action player1MoveUp = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        move(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                movePlayer(player1, MovementDirection.UP);
            }
        });
    }
};

//note that you can't use the same timer for both
//players if you don't want a button release stop both
private void move(Action play) {
    if((timer !=null) && timer.isRunning()) { //timer is a field 
        return;
    }
    timer = new Timer(100, play);
    timer.setInitialDelay(0);
    timer.start();
}

Action stop = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
        timer.stop();
    }
};
 类似资料:
  • 问题内容: 我有一个JTable 作为数据。现在,我想检测双击JLabel或表单元格(但仅在其中一列中)。如何在JLabel分别表单元格上添加Action / MouseListener? 问题答案: 怎么样:

  • 当用户与组件交互时,例如h:commandButton或h:link,JSF会触发可以通过两种方式处理的动作事件。 S.No 技术与描述 1 Method Binding 在UI Component的actionListener属性中传递托管bean方法的名称。 2 ActionListener 实现ActionListener接口并将实现类名称传递给UI Component的actionList

  • 处理ActionEvent的类应该实现此接口。该类的对象必须在组件中注册。 可以使用addActionListener()方法注册该对象。 当动作事件发生时,将调用该对象的actionPerformed方法。 接口声明 以下是java.awt.event.ActionListener接口的声明: public interface ActionListener extends EventLis

  • 处理ActionEvent的类应该实现此接口。 该类的对象必须在组件中注册。 可以使用addActionListener()方法注册该对象。 当动作事件发生时,将调用该对象的actionPerformed方法。 接口声明 (Interface Declaration) 以下是java.awt.event.ActionListener接口的声明 - public interface ActionLi

  • 问题内容: 我想知道是否可以测试是否单击了JMenu(而不是JMenuItem)。我尝试向其中添加一个ActionListener,但似乎无法识别它。我只需要它在按下JMenu按钮时执行一个操作,以便可以在打开菜单之前更改该菜单的JMenuItems。也欢迎所有取得这一结果的工作! 谢谢 问题答案: 对于使用 码 仅用于ButtonModel

  • 问题内容: 嘿,大家晚上好,我对名为“提交”的第二个按钮有问题,因为我无法将我输入的所有信息都转移到框架中的空JList中,这是我的代码,到目前为止,我的问题是如果我单击“提交”,我的所有信息都将出现在我的代码中框架中的消息区域需要列出。谢谢 问题答案: 好吧,据我所知,这将从Fields中获取信息,并将其放入您在那里的列表中 b2.addActionListener(new ActionList