Walk and shoot not work in diagonal
Hy, i have a problem to manage imput game
i would implement all 8 movement + shoot in 8 direction, now i can move in 8 direction , shoot but stationary in 8 direction , and move and shoot in north,south,east,west,and north-east.
i have 5 volatile boolean (the problem not change without volatile)
private volatile boolean north;
private volatile boolean east;
private volatile boolean south;
private volatile boolean west;
private volatile boolean shoot;
that set true when arrow key is pressed with key binding and set false otherwise
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();
}
});
}
in the thread to control the booleans variable to realize also diagonal movement
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);
When i move only it work good, but when i shoot it work only for north,south,est,west and only north-east. why only north-east? if i stop direction with player oriented in any diagonal and shoot it shoot, It is as if when i move diagonal shoot became false
链接地址: http://www.djcxy.com/p/32036.html上一篇: 如何将NaN从parseInt变为0以获得空字符串?
下一篇: 走路和射击不对角线工作