简体   繁体   中英

My KeyBindings don't work and I want to know why

I have just started on learning how to use keyBindings and I cannot find out what I am doing wrong as when I press the UP arrow on my keyboard it does nothing.

My Main Game Window

public class GameWindow extends JFrame{

private static final long serialVersionUID = 1L;

public int WIDTH = 160, HEIGHT = WIDTH/12 *9, SCALE = 3;

public boolean running = false;

BackGround bg = new BackGround();

Ranger R = new Ranger();

TimerClass T = new TimerClass();

public static void main(String[] args) {
    new GameWindow();

}

public GameWindow() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(WIDTH * SCALE, HEIGHT * SCALE);
    setResizable(false);

    running = true;

    add(bg);
    bg.add(R);

    bg.setFocusable(true);

    R.setFocusable(true);

    setFocusable(true);
    setVisible(true);
    bg.repaint();

    run();
}

public void run() {
    while (running) {
        render();
    }
}

public void render() {
    bg.setLocation(Ranger.bgX, Ranger.bgY);
    R.setLocation(Ranger.X, Ranger.Y);
    R.setIcon(Ranger.rangerA[Ranger.I]);
    R.repaint();
    bg.repaint();
}

}

And My Ranger Class

public class Ranger extends JLabel {

private static final long serialVersionUID = 1L;

public static int X, Y, dX, dY, bgX, bgY, I = 0, jumpTime = 100;

public static boolean moving = false, movingLeft = false,
        movingRight = false, onFloor = false, jumping = false,
        movingUp = false, movingDown = false;

public int totalImages = 6;

public BufferedImage ranger1, ranger2, ranger3, ranger4, ranger5, ranger6;

public static ImageIcon[] rangerA;

static TileMap TileMap = new TileMap();

public Ranger() {

    try {
        // not moving
        ranger1 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger0.png"));
        ranger2 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger1.png"));
        // moving Left
        ranger3 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger2.png"));
        ranger4 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger3.png"));
        // moving Right
        ranger5 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger4.png"));
        ranger6 = ImageIO.read(getClass().getResource(
                "/Images/Sprites/ranger/Ranger5.png"));

    } catch (IOException e) {
        e.printStackTrace();
    }

    array();

}

public void array() {
    rangerA = new ImageIcon[6];
    {
        rangerA[0] = new ImageIcon(ranger1);
        rangerA[1] = new ImageIcon(ranger2);
        rangerA[2] = new ImageIcon(ranger3);
        rangerA[3] = new ImageIcon(ranger4);
        rangerA[4] = new ImageIcon(ranger5);
        rangerA[5] = new ImageIcon(ranger6);
    }
}

public void move() {

    AbstractAction moveUp = new AbstractAction() {

        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            System.out.println("Move up");
        }

    };

    this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "moveUp");
    this.getActionMap().put("moveUp", moveUp);

    X += dX;
    Y += dY;

    dX = 0;
    dY = 0;

    if (movingRight || movingLeft) {
        moving = true;
    }

}

}

I am trying it to output that it will move up in the console so that i can lean how to make new KeyBindings but I don't know why it dosn't work. Any solutions/tips will be much appreciated.

PS I am new to Java so sorry for simple mistakes and I am also aware of the wild loop in the main game window class.

EDIT: move() is called every few miliseconds in a seperate timer class.

KeyStroke.getKeyStroke("W") doesn't do what you think it does. Using this form requires a verbose description of the action, ie typed w .

For this reason, I never use it. Instead, I prefer to use KeyStroke.getKeyStroke(KeyEvent.VK_W, 0) which is much more direct

See KeyStroke#getKeyStroke(int, int) for more details.

You're also not binding the key strokes as you never call the move method. Instead, bind the key strokes in the classes constructor or some other method which should be called only once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM