使用Java Canvas在半主要方向上移动玩家

时间:2013-11-25 20:20:19

标签: java canvas

我正在尝试创建一个游戏,以便玩家可以向所有方向移动(N,NW,W,SW,S,SE,E,NE),但是当我捕捉到击键时它会一次一个地移动我然后,如果我按下它们,它会切换到一个基本方向(N,W,S,E)。

主类密钥监听器

    private class MultiKeyPressListener extends KeyAdapter {

    private final Set<Character> pressed = new HashSet<Character>();

    @Override
    public synchronized void keyPressed(KeyEvent e) {       
        pressed.add(e.getKeyChar());
        if(pressed.size() > 1){

            System.out.println(pressed);
            //LEFT UP PRESSED
            if (e.getKeyCode() == KeyEvent.VK_LEFT && e.getKeyCode() == KeyEvent.VK_UP
                    || e.getKeyCode() == KeyEvent.VK_A && e.getKeyCode() == KeyEvent.VK_W) {
                player.direction = "NW";
            }
            //UP RIGHT PRESSED
            if (e.getKeyCode() == KeyEvent.VK_UP && e.getKeyCode() == KeyEvent.VK_RIGHT
                    || e.getKeyCode() == KeyEvent.VK_W && e.getKeyCode() == KeyEvent.VK_D) {
                player.direction = "NE";

            }
            //RIGHT DOWN PRESSED
            if (e.getKeyCode() == KeyEvent.VK_RIGHT && e.getKeyCode() == KeyEvent.VK_DOWN
                    || e.getKeyCode() == KeyEvent.VK_S && e.getKeyCode() == KeyEvent.VK_D) {
                player.direction = "SE";
            }
        }else{
            //DOWN LEFT PRESSED
            if (e.getKeyCode() == KeyEvent.VK_LEFT && e.getKeyCode() == KeyEvent.VK_DOWN
                    || e.getKeyCode() == KeyEvent.VK_S && e.getKeyCode() == KeyEvent.VK_A) {
                player.direction = "SW";
            }
            //LEFT PRESSED
            if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
                player.direction = "W";
            }
            //DOWN PRESSED
            if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
                player.direction = "S";
            }
            //RIGHT PRESSED
            if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
                player.direction = "E";
            }
            //UP PRESSED
            if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
                player.direction = "N";
            }
        }
        player.move();
    }

    @Override
    public synchronized void keyReleased(KeyEvent e) {
        pressed.remove(e.getKeyChar());
    }
}

玩家类

        public void move(){
    if(direction.equals("N")){
        y-= moveRate;
    }else if(direction.equals("NW")){
        y-= moveRate;
        x-= moveRate;
    }else if(direction.equals("W")){
        x-= moveRate;
    }else if(direction.equals("SW")){
        x-= moveRate;
        y+= moveRate;
    }else if(direction.equals("S")){
        y+= moveRate;
    }else if(direction.equals("SE")){
        y+= moveRate;
        x+= moveRate;
    }else if(direction.equals("E")){
        x+= moveRate;
    }else if(direction.equals("NE")){
        x+= moveRate;
        y-= moveRate;
    }
    //System.out.println("Moving " + direction);
}

1 个答案:

答案 0 :(得分:0)

你不会同时收到两个活动,即你不会收到KeyEvent.VK_LEFTKeyEvent.VK_UP,你只会收到一个接一个。

你应该尝试做的事情是,每次按下一个键,举起一个标志或将其添加到某种List,当一个键被释放时,重置标志或将其从{{{ 1}}。

当你想确定角色的移动方向时,你会检查每一个旗帜并做出关于移动的决定......

我还建议Key Bindings使用List,因为它不会受到与焦点相关的问题的影响......

例如