【问题标题】:Java KeyPressed KeyRelease and holding a KeyJava KeyPressed KeyRelease 并持有一个密钥
【发布时间】:2013-12-11 14:25:42
【问题描述】:

我试图让我的角色左右移动,然后在您将手指从左箭头键或右箭头键移开时停止,但不断发生的事情是他继续前进。这是我的按键:

public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();//getting keycodes
        if (bInGame) {            
                if (key == KeyEvent.VK_LEFT) {
                    nReqdx = -1;
                    nReqdy = 0;  
                    }
                } else if (key == KeyEvent.VK_RIGHT) {
                    nReqdx = 1;
                    nReqdy = 0;
                    }
                } else if (key == KeyEvent.VK_SPACE) {//Interaction
                    //Yet to be filled
                } else if (key == KeyEvent.VK_ESCAPE && tTimer.isRunning()) {
                    bInGame = false;
                } else if (key == KeyEvent.VK_PAUSE) {
                    if (tTimer.isRunning()) {
                        tTimer.stop();
                    } else {
                        tTimer.start();
                    }
                }
        } else {
            if (key == 's' || key == 'S') {
                bInGame = true;
                GameRun();
            }
        }
    }

这是我的 KeyReleased:

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();         
            if (key == Event.LEFT || key == Event.RIGHT) {
                bMoving = false;
                if (bMoving == false) {
                nReqdx = 0;
                nReqdy = 0;
                nCurrentSpeed = 0;
            }
        }
    }

我认为,我发现它永远不会达到关键版本,但我不知道为什么。也不要让我发布我的所有代码,因为我有大约 450 行,只要让我发布变量声明的位置或类似的东西,如果您需要更多信息来找出问题所在。

我现在一直想知道我的角色是否会因为动画而继续前进,也许是他设定的图像一直导致他离开。

这是他的动画。

     public void DoAnimation() {
    nAstroImgCount--;
    if (nAstroImgCount <= 0) {
        nAstroImgCount = nASTROIMGDELAY;
        nAstroAnimPos = nAstroAnimPos + nAstroImgDir;
        if (nAstroAnimPos == (nASTROANIMIMGCOUNT - 1) || nAstroAnimPos == 0) {
            nAstroImgDir = -nAstroImgDir;
        }
    }
}

public void DrawAstronaut(Graphics2D g2d) {
    if (nViewDX == -1) {
        DrawAstronautLeft(g2d);
    } else if (nViewDX == 1) {
        DrawAstronautRight(g2d);
    } else {
        DrawAstronautStand(g2d);
    }
}

public void DrawAstronautLeft(Graphics2D g2d) {
    switch (nAstroAnimPos) {
        case 1:
            g2d.drawImage(imgAstroWalkLeft1, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 2:
            g2d.drawImage(imgAstroWalkLeft2, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 3:
            g2d.drawImage(imgAstroWalkLeft3, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 4:
            g2d.drawImage(imgAstroWalkLeft4, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 5:
            g2d.drawImage(imgAstroWalkLeft5, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 6:
            g2d.drawImage(imgAstroWalkLeft6, nAstronautX + 1, nAstronautY + 1, this);
            break;
        default://default of him standing still
            g2d.drawImage(imgAstroStandLeft, nAstronautX + 1, nAstronautY + 1, this);
            break;
    }
}

public void DrawAstronautRight(Graphics2D g2d) {//Same as the left but right
    switch (nAstroAnimPos) {
        case 1:
            g2d.drawImage(imgAstroWalkRight1, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 2:
            g2d.drawImage(imgAstroWalkRight2, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 3:
            g2d.drawImage(imgAstroWalkRight3, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 4:
            g2d.drawImage(imgAstroWalkRight4, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 5:
            g2d.drawImage(imgAstroWalkRight5, nAstronautX + 1, nAstronautY + 1, this);
            break;
        case 6:
            g2d.drawImage(imgAstroWalkRight6, nAstronautX + 1, nAstronautY + 1, this);
            break;
        default:
            g2d.drawImage(imgAstroStandRight, nAstronautX + 1, nAstronautY + 1, this);
            break;
    }
}

我的意思是他继续前进,因为他的动画从未停止。想知道是否有人可以确认。

【问题讨论】:

    标签: java swing keyboard keypress


    【解决方案1】:

    您正在访问两种不同类型的键常量。一方面您使用的是KeyEvent,另一方面是Event

    KeyEvent 类中是:

    /**
     * Constant for the non-numpad <b>left</b> arrow key.
     * @see #VK_KP_LEFT
     */
    public static final int VK_LEFT           = 0x25;   // = 37
    

    Event 类中是:

    /**
     * The Left Arrow key, a non-ASCII action key.
     */
    public static final int LEFT                = 1006;
    

    由于getKeyCode() 方法从KeyEvent 类返回常量,您必须在两种 情况下都使用KeyEvent.VK_LEFT 常量。

    【讨论】:

    • +1 用于发现问题,最好使用键绑定机制而不是KeyListenerS
    • 那么我会将事件更改为 KeyEvents 并将 LEFT 和 RIGHT 更改为 VK_,如果是这样我已经更改了它并没有帮助
    • 您是如何发现您的程序根本没有达到keyReleased 方法的?你是通过给这个方法设置断点来调试的吗?
    • 我不知道这有多专业,但我只有一个 system.out.println ,其中包含一些文本,当我释放密钥时从未打印出来。
    • 在第一行?还是在内部 if 语句中?如果是前者,你应该看看你的KeyListener,你在哪里添加了它。这是您尚未向我们展示的范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2018-08-06
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多