0

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

What I mean is that he keeps going because his animation never stops. Wondering if someone could confirm that or not.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
BlueBarren
  • 321
  • 7
  • 24
  • you have to use KeyBindings, better will be match together with used Swing Timer, no idea (based on reduced fragment ...) without an [SSCCE](http://sscce.org/), can [be based on](http://stackoverflow.com/a/7940227/714968) – mKorbel Dec 17 '13 at 13:43
  • Thank Sir, I'll look into KeyBindings. It would still be nice to see if anyone else has anything to say on this, maybe they can fix it still. – BlueBarren Dec 17 '13 at 13:54
  • yes there will be suggestion to use paintComponent instead of whatever woodoo with Graphics (nothing cleaver without your SSCCE) – mKorbel Dec 17 '13 at 13:56
  • Why can't my code just be fixable? I don't want to have to rewrite a large portion of my code with KeyBindings, I'll try it, I'm just complaining – BlueBarren Dec 17 '13 at 14:12
  • I't talking about paintComponent, search here (use tag paintComponent) – mKorbel Dec 17 '13 at 14:34
  • Um... what @mKorbel ? – BlueBarren Dec 18 '13 at 13:16

0 Answers0