Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Smooth walking  (Read 767 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2012-11-03 21:29:57 »

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!
Offline Agro
« Reply #1 - Posted 2012-11-03 21:36:30 »

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

Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #2 - Posted 2012-11-04 03:02:28 »

Don't use sleep. It isn't exact.  Tongue

Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-11-04 03:45:27 »

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

Myomyomyo.
Offline tdegroot96

Junior Member


Projects: 1



« Reply #4 - Posted 2012-11-04 14:43:23 »

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?
Offline Agro
« Reply #5 - Posted 2012-11-04 14:45:24 »

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

Offline theagentd
« Reply #6 - Posted 2012-11-04 15:01:36 »

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?
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

Myomyomyo.
Offline KittenKoder

Senior Member


Medals: 7



« Reply #7 - Posted 2012-11-04 15:47:51 »

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

Here's a quick time tracking class example.

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  
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.

I am no one else.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (39 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (36 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!