playerMovement 的 ActionListener 或 Action
ActionListener or Action for playerMovement
我只是想编写一个小足球比赛,在完成所有绘画并初始化球员和球之后,我现在正处于尝试移动球员的位置。我看了一些教程如何在游戏中编程运动,但他们都使用 KeyListeners。正如我在网上写的那样,KeyListener 不是一个好方法。 (是否还有使用 KeyListeners 的情况?)。但是,我正在尝试实现流畅且适合 OO 举止的正确移动。我找不到以 "correct" 方式教授该内容的在线资源。我指定 "correct" 方式是因为我从 SO 上的顶级 Java 程序员那里读到了建议。我正在为 WHAT 用于移动和 WHERE 来实现它而苦苦挣扎?我想过使用 Actions 并在 Player 的内部 class 中实现它吗?这是正确的方法吗?
到目前为止,这是我的代码:
速球:
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();
}
}
速球面板:
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 为该椭圆创建运动。我使用了这个资源:
- How to use Key Bindings instead of Key Listeners
- https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
我现在想解决以下问题:
- 我无法同时移动两个播放器,而且我在网上找不到任何解决方法。当玩家 1 正在移动时,我按下玩家 2 的向上键,玩家 1 停止移动。现在我想知道,如何让两个玩家同时移动
编辑 2:
这是我基于 c0der
回答的新代码
SpeedballPanel(class 已更改):
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
}
注意:其余 classes 未更改
实际上我不想随机或自动移动 player2,我希望它通过箭头键移动,如我对 InputMap 的定义所示。但是我仍然有同样的问题,当我尝试使用 WASD 键移动 player1 并尝试同时使用 UP、DOWN、LEFT、RIGHT - 箭头键移动 player2 时,第一个移动的玩家停止移动,另一个玩家停止移动开始移动,但它们无法同时移动。抱歉,如果我在第一次 post 中不清楚玩家的移动。
从代码可以假设您希望计时器移动另一个球。
根据以下代码应用更改以移动玩家 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();
}
};
我只是想编写一个小足球比赛,在完成所有绘画并初始化球员和球之后,我现在正处于尝试移动球员的位置。我看了一些教程如何在游戏中编程运动,但他们都使用 KeyListeners。正如我在网上写的那样,KeyListener 不是一个好方法。 (是否还有使用 KeyListeners 的情况?)。但是,我正在尝试实现流畅且适合 OO 举止的正确移动。我找不到以 "correct" 方式教授该内容的在线资源。我指定 "correct" 方式是因为我从 SO 上的顶级 Java 程序员那里读到了建议。我正在为 WHAT 用于移动和 WHERE 来实现它而苦苦挣扎?我想过使用 Actions 并在 Player 的内部 class 中实现它吗?这是正确的方法吗?
到目前为止,这是我的代码:
速球:
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();
}
}
速球面板:
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 为该椭圆创建运动。我使用了这个资源:
- How to use Key Bindings instead of Key Listeners
- https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
我现在想解决以下问题:
- 我无法同时移动两个播放器,而且我在网上找不到任何解决方法。当玩家 1 正在移动时,我按下玩家 2 的向上键,玩家 1 停止移动。现在我想知道,如何让两个玩家同时移动
编辑 2:
这是我基于 c0der
回答的新代码SpeedballPanel(class 已更改):
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
}
注意:其余 classes 未更改
实际上我不想随机或自动移动 player2,我希望它通过箭头键移动,如我对 InputMap 的定义所示。但是我仍然有同样的问题,当我尝试使用 WASD 键移动 player1 并尝试同时使用 UP、DOWN、LEFT、RIGHT - 箭头键移动 player2 时,第一个移动的玩家停止移动,另一个玩家停止移动开始移动,但它们无法同时移动。抱歉,如果我在第一次 post 中不清楚玩家的移动。
从代码可以假设您希望计时器移动另一个球。
根据以下代码应用更改以移动玩家 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();
}
};