使用Java Canvas在半主要方向移动播放器 [英] Moving player in semi cardinal directions using Java Canvas

查看:220
本文介绍了使用Java Canvas在半主要方向移动播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个游戏,以便玩家可以在所有方向(N,NW,W,SW,S,SE,E,NE)移动,虽然当我捕捉键击时,它会移动我一次(N,W,S,E)。

I am trying to create a game so that the player can move in all directions (N,NW,W,SW,S,SE,E,NE) although when I capture the key strokes it will move me once in one of the diagonal directions then, but if I hold them down it switches to one of the cardinal directions(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);
}


推荐答案

同时接收两个事件,也就是说,您不会收到 KeyEvent.VK_LEFT AND KeyEvent.VK_UP

You're not going to receive two events simultaneously, that is you won't receive KeyEvent.VK_LEFT AND KeyEvent.VK_UP, you'll only receive one after the other.

你应该尝试和做什么,是每次按下一个键,提出一个标志或添加到某种 List 时,释放键时,重置标志或将其从列表中删除​​。

What you should try and do, is each time a key is pressed, raise a flag or add it to some kind of List, when a key is released, reset the flag or remove it from the List.

当你想要确定角色的移动方向时,你将检查每个标志,并做出关于动作的决定...

When you want to determine which direction the character is moving, you would inspect each of these flags and make your decision about the movement...

I也建议您使用主要绑定 KeyListener ,因为它没有遭遇同样的焦点相关问题...

I would also recommend the use of Key Bindings over KeyListener, as it does not suffer from the same focus related issues...

例如

  • Problems with Java's Paint method, ridiculous refresh velocity (this is quite interesting, as it I'll accelerate the object while the key is pressed (to a maximum speed) and will decelerate the object after you release it, but that's just me)
  • How can i make multiple Key Bindings work at the same time?
  • Detecting multiple keypresses in java

这篇关于使用Java Canvas在半主要方向移动播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆