Runs at 7000FPS and I quit after 2000+ points

Holy crap really? What kind of god-rig do you have? I get 1300

Uhh an Intel Core i7 2600K overclocked to 4GHz, Nvidia GeForce GTX 580, 8GB of RAM, 60GB SSD, and 1TB HDD.........MUST..PLAY...BATTLEFIELD 3!

The only bug I found was when hitting two different arrow keys semi-quickly the player stops.
I'm working on this, debugging is my least favorite part of coding when it takes this long. Even though it's only been an hour haha.
Do you have any idea what could be making the key-reading so sloppy?
It seems like there is absolutely no point to the xxxPressed, dx, dy, and xxxWait variables, I can't see any reasonable use for them, except playing around with numbers.
The variables that control all the moving are the dirXXX variables, yet everything seems fine to me.....
I didn't do any extensive debugging because your code is way overcomplicated when you could have taken out half of it.
EDIT: Aha, I stared at it a couple more minutes and I figured it out. This only happens when you press a new key while still holding down the old key. Since my fingers are moving really fast, I couldn't really notice it but I noticed the bug in the code. I traced it down to keyReleased(int). Let's say I hold down UP, hold RIGHT, release UP, then release RIGHT: dirUp gets set to true, then dirRight gets set to true. In keyReleased(int), when I release UP: dirRight, dirLeft, and dirDown are set to false; then I release RIGHT: dirLeft, dirUp, and dirDown are set to false. In the end everything is false so nothing is moving!
I suggest using only 1 variable for direction and having 4 constants that specify what direction to move:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| private final int RIGHT = 0, LEFT = 1, UP = 2, DOWN = 3; private int currentDirection = UP;
...
public void keyPressed(int key) { switch(key) { case KeyEvent.VK_RIGHT: currentDirection = RIGHT; break; case KeyEvent.VK_LEFT: currentDirection = LEFT; break; case KeyEvent.VK_UP: currentDirection = UP; break; case KeyEvent.VK_DOWN: currentDirection = DOWN; break; } }
public void move() { if(timeToMove) { switch(currentDirection) { case RIGHT: break; case LEFT: break; case UP: break; case DOWN: break; } } } |
Phew....lots of typing
