aspose file tools*
The moose likes Game Development and the fly likes Movement of user causes AI to lag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Movement of user causes AI to lag" Watch "Movement of user causes AI to lag" New topic

Movement of user causes AI to lag

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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 :

John Price

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)
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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 :

John Price

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.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1511
john price wrote:...a solution to how to make the algorithm better, that would be awesome.

Will threading work?

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Could you show me what you are talking about (IE an example)?

John Price
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339

using threads is certainly something you should think about. i will post an old homework assignment that will demonstrate for you.


Visit my download page
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
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).

You can learn a bit more about Game Loops at if you are not already familiar with them.
I agree. Here's the link:
subject: Movement of user causes AI to lag
Similar Threads
Dual Program (Applet & Application) : Need to know how to improve in Applet ways only!
Suggestions to make code (Pong) better (from the code I have, not adding additional code)
Computer Pad Not Stopping!
Double Buffering Problem
Problem with writing code.