Smooth walking

After programming on my game for one day, I finally need some help.
What I’m trying to do is: to make my character walk everytime each 200 - 300 ms. I tried this with a thread by letting it sleep for that amount of miliseconds. This actually did work, but not really smooth. Sometimes it didn’t really respond to my key input, only after holding it for a certain amount of miliseconds. What is the best way to solve this?
Location: https://github.com/Desmaster/Devio/blob/master/src/com/github/desmaster/Devio/entity/Player.java

Thanks for helping / reading!

You need to have better frame rate control. Try out what I said earlier today here: http://www.java-gaming.org/topics/fast-and-slow-keyadapter-keyinputhandler/27687/msg/248822/view.html#new

Don’t use sleep. It isn’t exact. :stuck_out_tongue:

DO NOT use a thread per unit. That’s a recipe for disaster.

Kk, can you tell me how I can make the delay better?

masteryoom, isn’t a thread the most efficient way? I know you can use the SWING timer or the UTIL timer. Is that better?

It’s not about delay. The delay you get is just a small side-effect compared to what’s to come. You’ll need synchronization everywhere just to handle collision detection. No, use an ArrayList, dump in all your objects into it and loop over it and update everyone in order. Don’t do it like this, ESPECIALLY not if you don’t know why. :wink:

Time tracking and a lower sleep, say 20 or 30 milliseconds.

Here’s a quick time tracking class example.


package kitt.com.swing;


/**
 * A tracker to find the time between calls.
 * 
 * @author kitten
 *
 */
public final class TimeTracker {
	private long lasttime = System.currentTimeMillis();
	
	/**
	 * 
	 */
	public TimeTracker() {
		this.lasttime = System.currentTimeMillis();
	}
	/**
	 * @return The number of milliseconds passed since last call.
	 */
	public int getTimePassed() {
		long curtime = System.currentTimeMillis();
		int millis = (int)(curtime - this.lasttime);
		this.lasttime = curtime;
		return millis;
	}
}

Just every time you pass through your loop start off by calling the getTimePassed method to get the number of milliseconds passed since the last call then interpolate your movement and animations based on that.