Java KeyPressed KeyRelease并持有一个密钥

时间:2013-12-11 14:25:42

标签: java swing keyboard keypress

我正试图让我的角色左右移动,然后当你的手指离开左或右箭头键时停止,但发生的事情是他继续前进。这是我的关键词:

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;
    }
}

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

1 个答案:

答案 0 :(得分:2)

您正在访问两种不同类型的键常量。另一方面,您使用的是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常量。