我试图让我的角色左右移动,然后在您将手指从左箭头键或右箭头键上移开时停止,但不断发生的事情是他继续前进。这是我的按键:
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:
g2d.drawImage(imgAstroStandLeft, nAstronautX + 1, nAstronautY + 1, this);
break;
}
}
public void DrawAstronautRight(Graphics2D g2d) {
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;
}
}
我的意思是他继续前进,因为他的动画永远不会停止。想知道是否有人可以证实这一点。