走路和射击不对角线工作
Hy,我有一个问题来管理输入游戏
我将在8个方向上执行所有8个动作+拍摄,现在我可以沿8个方向移动,在8个方向上拍摄但静止,并在北部,南部,东部,西部和东北部移动并拍摄。
我有5个易变的布尔值(问题没有变化没有易失性)
private volatile boolean north;
private volatile boolean east;
private volatile boolean south;
private volatile boolean west;
private volatile boolean shoot;
当使用键绑定按下箭头键时设置为true,否则设置为false
private void setKeyBindings() {
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "UP" + "pressed");
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "UP" + "released");
actMap.put( "UP" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
north=true;
}
});
actMap.put("UP" + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, false);
north=false;
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "DOWN" + "pressed");
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "DOWN" + "released");
actMap.put( "DOWN" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
south=true;
}
});
actMap.put("DOWN" + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, false);
south=false;
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "LEFT" + "pressed");
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "LEFT" + "released");
actMap.put( "LEFT" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
west=true;
}
});
actMap.put("LEFT" + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, false);
west=false;
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "RIGHT" + "pressed");
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "RIGHT" + "released");
actMap.put( "RIGHT" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
east=true;
}
});
actMap.put("RIGHT" + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, false);
east=false;
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "SPACE" + "pressed");
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), "SPACE" + "released");
actMap.put( "SPACE" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
shoot=true;
}
});
actMap.put("SPACE" + "released", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, false);
shoot=false;
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0, false), "SUCCESSIVA" + "pressed");
actMap.put( "SUCCESSIVA" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
mondo.player.selectArmaSuccessiva();
}
});
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0, false), "PRECEDENTE" + "pressed");
actMap.put( "PRECEDENTE" + "pressed", new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
//directionMap.put(direction, true);
mondo.player.selectArmaPrecedente();
}
});
}
在线程中控制布尔变量以实现对角线运动
if(shoot){
System.out.println("SHOOT");
if(mondo.player.selectArm!=-1)
mondo.proiettili.add(mondo.player.spara());
}
if(north && west && !south && !east){
System.out.println("NORD OVEST");
mondo.player.direction=(PuntiCardinali.NORTH_WEST);
}
if(north && east && !south && !west){
System.out.println("NORD EST");
mondo.player.direction=(PuntiCardinali.NORTH_EAST);
}
if(south && east && !north && !west){
System.out.println("SUD EST");
mondo.player.direction=(PuntiCardinali.SOUTH_EAST);
}
if(south && west && !north && !east){
System.out.println("SUD OVEST");
mondo.player.direction=(PuntiCardinali.SOUTH_WEST);
}
if(north && !west && !east && !south){
System.out.println("NORD");
mondo.player.direction=(PuntiCardinali.NORTH);
}
if(south && !west && !east && !north){
System.out.println("SUD ");
mondo.player.direction=(PuntiCardinali.SOUTH);
}
if(east && !north && !south && !west){
System.out.println("EST");
mondo.player.direction=(PuntiCardinali.EAST);
}
if(west && !north && !south && !east){
System.out.println("OVEST");
mondo.player.direction=(PuntiCardinali.WEST);
}
if(!north && !south && !east && !west)
mondo.player.direction=(PuntiCardinali.STOP);
当我只移动它时表现很好,但是当我拍摄它时,它只能在北部,南部,东部,西部和东北部使用。 为什么只有东北? 如果我停止朝向任何对角线的球员并射击它,那就好像我移动对角线射门变成虚假
链接地址: http://www.djcxy.com/p/32035.html上一篇: Walk and shoot not work in diagonal
下一篇: Change one variable assigned to an object to another object?