【发布时间】:2015-01-06 18:42:09
【问题描述】:
在这段代码中,我有一个包含两个坦克的图形(paintcomponent 图形)。 paintcomponent 由变量控制,然后是 repaint()。现在,当仅按下或按住一个按钮时,它可以完美运行。但是,例如,如果我为 tank1 持有左侧,而为 tank2 持有右侧,则最后一个键是“获胜”的那个:这是唯一发生的方向。
在我的代码中,您会发现我最近的 4 次尝试都被添加到代码中。
基本设置是:Keybinding→AbstractAction→...
上面没有省略号(…)的设置代表最初的尝试。然后,在另一次尝试中,这被链接到一个线程。结果相同。在另一次尝试中,抽象操作被设置为更改数组值,该数组值连接到每 20 秒检查一次的计时器。从那里分支的两次尝试是定时器到线程的连接,以及直接重绘放入定时器。
此时我唯一的其他选择是为每个组合进行单独的键绑定(即,按住左键和 D [D 移动 tank1] 将是键码“LEFT D”的键绑定,因此将是一个单独的事件移动两者)。但是,这会很麻烦,因为我也有可以改变角度的炮塔,还有可以发射的子弹……而且我想有一个更体面的方法来做到这一点。
任何帮助将不胜感激。代码如下。
class OneMoveRightThread implements Runnable { //This is a thread that contains code to 'move' the tank1.
public void run(){
tankGraphic.TankOneMoveRight();
gameArea.repaint();
}
}
class OneMoveLeftThread implements Runnable { //thread to move tank2
public void run(){
tankGraphic.TankOneMoveLeft();
gameArea.repaint();
}
}
//Meow is a [0,0,0,0,0,0,0,0] (8 zeroes) array. When a button is clicked the value is set to 1 for the spot in the array.
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (meow[0] == 1){ //This attempt uses a timer to constantly check the array for potential movement. Still works for 1 movement at a time.
(new Thread(new OneMoveLeftThread())).start();
meow[0]=0;
}
if(meow[1]==1){
(new Thread(new OneMoveRightThread())).start(); //using code in threads by calling the thread. My hope was that this would fix it.
meow[1]=0;
}
if (meow[2]==1){ //TANK TWO MOVE LEFT using code in threads
tankGraphic.TankTwoMoveLeft();
gameArea.repaint();
meow[2]=0;
}
}
};
new Timer(delay, taskPerformer).start();
//This abstract action is a different part of the code.
Action doNothing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
lifeBar1.setText("\n Player 1: " + player1Name + " \n Health: " + player1Health + " \n Bullets: A LOT!");
lifeBar2.setText("\n Player 2: " + player2Name + " \n Health: " + player2Health + " \n Bullets: A LOT!");
}
};
//TANK TWO BELOW
Action leftMove = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) { //This is code for "When LEFT arrow is pressed, set meow[2]=1"
//tankGraphic.TankTwoMoveLeft();
//gameArea.repaint();
meow[2]=1;
}
};
Action rightMove = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) { //This is code for when RIGHT arrow is pressed, move tank directly. This and the above have same erroneous result.
tankGraphic.TankTwoMoveRight();
gameArea.repaint();
}
};
Action angleLEFT = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
tankGraphic.TankTwoAngleLeft();
gameArea.repaint();
}
};
Action angleRIGHT = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
tankGraphic.TankTwoAngleRight();
gameArea.repaint();
}
};
//TANK ONE BELOW
Action leftMove2 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
meow[0]=1;
}
};
Action rightMove2 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
//(new Thread(new OneMoveRightThread())).start();
meow[1]=1;
}
};
Action angleLEFT2 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
tankGraphic.TankOneAngleLeft();
gameArea.repaint();
}
};
Action angleRIGHT2 = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
tankGraphic.TankOneAngleRight();
gameArea.repaint();
}
};
//these two lines are irrelevant. They match the irrelevant abstractaction.
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("B"), "doNothing");
gameArea.getActionMap().put("doNothing",doNothing);
//below is the code for the keybindings.
//the names are reversed, below. (fire1 corresponds to tank two, but the right side of the keyboard)
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("LEFT"), "leftMove");
gameArea.getActionMap().put("leftMove",leftMove);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"), "rightMove");
gameArea.getActionMap().put("rightMove",rightMove);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("UP"), "angleLEFT");
gameArea.getActionMap().put("angleLEFT",angleLEFT);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("DOWN"), "angleRIGHT");
gameArea.getActionMap().put("angleRIGHT",angleRIGHT);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "leftMove2");
gameArea.getActionMap().put("leftMove2",leftMove2);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "rightMove2");
gameArea.getActionMap().put("rightMove2",rightMove2);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "angleLEFT2");
gameArea.getActionMap().put("angleLEFT2",angleLEFT2);
gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "angleRIGHT2");
gameArea.getActionMap().put("angleRIGHT2",angleRIGHT2);
//gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("E"), "fire2");
//gameArea.getActionMap().put("fire2",fire2);
//gameArea.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("SLASH"), "fire1");
//gameArea.getActionMap().put("fire1",fire1);
frame.requestFocus();
【问题讨论】:
标签: java multithreading simultaneous