I started writing a game 2 days ago. It is a simple platform game. There are 3 platforms total : a "floor" and 2 "floating ramps". On the ground, between the "floating ramps", there is a "coin". On the second floating ramp, there is another "coin". You can pick up the coins. On the first floating ramp, there is a "gun". If you pick up the "gun", you can "shoot" "bullets". Only one "bullet" by the user is allowed to be shot at one time. The user can physically control the object (cube, box, etc) with the arrow buttons. Up physically moves the object up a certain distance. Moving to the right causes the world to move left. Moving to the left causes the world to move right. You may destroy 2 "targets" with the "bullets" if you have picked up the "gun". If the "targets" are "destroyed", an AI "character" (box, cube, etc) comes running at a constant speed to the left. It spits out a stream of "bullets". If the user destroys the AI "character", the character disappears along with its bullets. The only issue is when the character moves while the AI character is still "alive" and shooting "bullets". The character's location algorithm is composed of this :
When a user pushes the left or right arrow on their keyboard, a timer starts :
These continue until the user stops holding down the key.
This is what happens when the AI "walks" (it walks if it is still "alive") :
I have marked what I believe to be the problem line above. I have tried changing the timer on the "enemyShoot" timer and it doesn't affect the problem. If a user is "running" full to the left, the "bullets" go out as far as they should. If a user is stopped, the "bullet's" path is shorter. If a user is "running" full to the right, the "bullets" start moving to the right at lower timer intervals (around 20 it is noticeably going to the left). I was wondering how to fix this problem. To me, it just seems like the lag of the repaint and the refreshing of the bullets. I would like the bullets to be consistently going to the right.
Here is the keyEvent info for left and right arrows :
Here is the release key code :
The AI's bullet display system is the same as the character's bullet display system :
EDIT : So, to restate my question... I would like a solution or help to finding a solution to fix the problem of the movement of the user causing the AI's "bullets" to not be consistent.
EDIT(2) : Please note that the AI movement isn't lagging. It is the "bullets" that the AI shoots.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
I found a solution. It was so simple. Instead of calculating it off of the previous point, which may be outdated, recalculate it based off of the AI character's position as you did in the first place, subtracting the amount of distance traveled :
EDIT : Although there is still some inconsistency when the user is running right or left (bullet stream is slower, but further when the user runs left and faster, but closer when the user runs right), it is much better than before. If anybody can give me a solution to how to make the algorithm better, that would be awesome.
It's hard to know for sure, given the fragments of code that you show. But I am surprised to see an animated game with multiple Timers involved. Most of what I've seen is done via a single timed Game Loop (which might be triggered by a Timer, or might be managed via varying Sleep amounts, to keep the refresh rate constant).