Reputation: 351
I'm trying to to get my character to move left and right, and then stop when you take your fingers off of the left or right arrow keys but what keeps happening is he keeps going. Here is my keypressed:
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();
}
}
}
And Here is my 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;
}
}
}
What I have discovered is that it never reaches the key release, I think, but I don't know why. Also don't ask me to post all of my code because I have about 450 lines, just ask me to post where a variable is declared or something like if you need more information to figure out what is going wrong.
I have now been wondering if my character keeps going because he is animated, maybe his set images keep causing him to go.
Here is where he is animated.
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;
}
}
What I mean is that he keeps going because his animation never stops. Wondering if someone could confirm that or not.
Upvotes: 0
Views: 567
Reputation: 3365
You are accessing two different types of the key constants. In one hand you are using KeyEvent
, on the other hand Event
.
In the KeyEvent
class it is:
/**
* Constant for the non-numpad <b>left</b> arrow key.
* @see #VK_KP_LEFT
*/
public static final int VK_LEFT = 0x25; // = 37
In the Event
class it is:
/**
* The Left Arrow key, a non-ASCII action key.
*/
public static final int LEFT = 1006;
Since the getKeyCode()
method is returning the constant from the KeyEvent
class, you have to use in both cases the KeyEvent.VK_LEFT
constant.
Upvotes: 2