Java键按下键释放并按住键
我试着让我的角色左右移动,然后当你把手指从左箭头键或右箭头键上拿下来的时候停止,但一直发生的是他一直在走。这是我按下的键:Java键按下键释放并按住键,java,swing,keyboard,keypress,Java,Swing,Keyboard,Keypress,我试着让我的角色左右移动,然后当你把手指从左箭头键或右箭头键上拿下来的时候停止,但一直发生的是他一直在走。这是我按下的键: public void keyPressed(KeyEvent e) { int key = e.getKeyCode();//getting keycodes if (bInGame) { if (key == KeyEvent.VK_LEFT) {
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();
}
}
}
这是我的钥匙:
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;
}
}
public void DoAnimation(){
nAstroImgCount--;
如果(nAstroImgCount您正在访问两种不同类型的键常量。一方面您正在使用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
/**
* The Left Arrow key, a non-ASCII action key.
*/
public static final int LEFT = 1006;
由于getKeyCode()
方法是从KeyEvent
类返回常量,您必须在两种情况下都使用KeyEvent.VK_LEFT
常量。+1用于捕获问题,最好使用键绑定机制,而不是keylister
SSo我会将事件更改为KeyEvents,并左右更改为VK_,如果是这样,我已经更改了它,但它没有帮助您如何发现您的程序根本没有到达keyreased
方法?您是否通过将断点设置到该方法来调试它?我不知道这有多专业,但我只是有一个system.out.println,其中包含一些在我释放密钥时从未打印出来的文本。在第一行?或在内部if语句?如果是前者,您应该查看您的KeyListener
,您在其中添加了它。这是您尚未向我们显示的范围。