Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 movement  (Read 2918 times)
0 Members and 1 Guest are viewing this topic.
Offline magemog

Senior Newbie




Jaded Samurai


« Posted 2005-02-01 03:26:01 »

I'm working on a tile based rpg, and I'm trying to make it look as professional as possible... I already can get smooth movement with scrolling, and I've got the character to the point where [on my system at least] if it keeps moving in one direction it will look fairly smooth, more or less thanks to you guys with the bits of code I've looked at off of here and some of the suggestions I've seen  Smiley

Anyhow so the problem is if you want to move in, let's say a different direction, it looks chopy/does't happen until the guy gets done moving. If you're at a complete stop then only try to move in one direction, it looks fine. Currently, the guy makes about 5 intervals during each directional key press, to make it look smooth. I've tried making the character just changing direction if there is a key press in between movement shifting, but this looks weird... I've tried looking at professional games, but it doesn't really help me out in seeing how they make the keyboard input/ensuing movement look so smooth.

I'd post code, but it looks a bit icky at the moment.

Ahh, and it seems i'm quite the noob with this .jar business... here is the game I'm working on. Unlike the .jars I've seen here that just work instantly, mine seems to work if you extract it, then put the jar in that folder you extracted the stuff to.  :-/    What.

www.ki-rush.com/what.jar


Oh, and sorry if I sucked at explaining what's going on, any feedback would be cool really.

Hmm... sounds vaguely familiar... yeah
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2005-02-01 03:41:36 »

Quote
Ahh, and it seems i'm quite the noob with this .jar business... here is the game I'm working on. Unlike the .jars I've seen here that just work instantly, mine seems to work if you extract it, then put the jar in that folder you extracted the stuff to.  :-/


There's really only one thing you need to make your application portable: Stop loading files with "new FileInputStream()" and start loading them with code like this:
1  
InputStream in = getClass().getResourceAsStream("/map1.dat");

That bit of code will cause the classloader to become responsible for loading the file, even if the classes were loaded from an archive, over the network, or from something that hasn't even been invented yet!

Also, most types of loaders (e.g. javax.imageio, javax.sound.sampled, etc.) take a URL as a parameter. In those cases, it's usually cleaner to pass the URL instead of creating and managing a stream. e.g.:
1  
Image myimage = ImageIO.getImage(getClass().getResource("/map1.dat"));

One important note to keep in mind is that the paths are relative to the classpath, and you should always use absolute paths! Oh, and the separator is always a forward slash.

Once you have that fixed, it will make it easier for the rest of us to help with the scrolling bug. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline magemog

Senior Newbie




Jaded Samurai


« Reply #2 - Posted 2005-02-01 18:40:42 »

Hmm, I'm not quite sure what to do... I wasn't loading files with "new FileInputStream()", I was loading them like this:

1  
BufferedReader in  = new BufferedReader(new FileReader( FileName ));


Hmm,  that might be the same thing... dunno  :-/  

In any case, is there a way to fix the problem you had that would also entail using a way to obtain file input one line at a time, rather than by bytes? I can't really figure out how to change everything to be loaded from bytes instead of just using .readLine() ...

Er.. and well currently I'm loading images like this:

1  
Image cloud      =  new ImageIcon("images\\cloud.png").getImage();


and as far as I know, I thought that does load it from the url instead of creating a stream Huh




Hmm... sounds vaguely familiar... yeah
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2005-02-01 19:15:35 »

Quote
Hmm, I'm not quite sure what to do... I wasn't loading files with "new FileInputStream()", I was loading them like this:

1  
BufferedReader in  = new BufferedReader(new FileReader( FileName ));


Hmm,  that might be the same thing... dunno  :-/


That's pretty much the same thing. FileReader is equivalent to:

1  
Reader reader = new InputStreamReader(new FileInputStream("/map1.dat"));


It's easy to convert it for classloading, however:

1  
Reader reader = new InputStreamReader(getClass.getResourceAsStream("/map1.dat"));


Quote
Er.. and well currently I'm loading images like this:

1  
Image cloud      =  new ImageIcon("images\\cloud.png").getImage();


and as far as I know, I thought that does load it from the url instead of creating a stream Huh


Erk! Quick, get this guy an IV! He's going into image loading shock! ;-)

The good news is that the above does load from a URL, but it has several things wrong with it. For one, always use forward slashes for URLs. The use of backslashes may get you in trouble on other OSes. Also, use a forward slash in front of all your paths. This will keep you from running into some nasty bugs. :-)

The "correct" way to load an image is this:

1  
2  
3  
4  
import java.awt.image.*;
import javax.imageio.*;
...
BufferedImage image = ImageIO.read(getClass().getResource("/myimage.png"));


Be aware that these images may not be accelerated. You need to get an accelerated image from the GraphicsContext and copy your image into it. There's a mini tut over on the WIKI:

http://wiki.java.net/bin/view/Games/LoadingSpritesWithImageIO

Java Game Console Project
Last Journal Entry: 12/17/04
Offline tom
« Reply #4 - Posted 2005-02-01 21:37:04 »

Quote

The "correct" way to load an image is this:

1  
2  
3  
4  
import java.awt.image.*;
import javax.imageio.*;
...
BufferedImage image = ImageIO.read(getClass().getResource("/myimage.png"));


In theory yes. But lets try to make the ImageIO bug common knowledge and encurage the use of the workaround:
1  
2  
3  
4  
import java.awt.image.*;
import javax.imageio.*;
...
BufferedImage image = ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("/myimage.png")));


I think the workaround should be a sticky post on the Java2D section.

Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #5 - Posted 2005-02-01 21:46:42 »

Tom, are you sure that happens with URL loading? I've seen it happen with InputStream loading, but not with URLs.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline tom
« Reply #6 - Posted 2005-02-01 22:15:27 »

Yes I'm sure. Have a look at the source for ImageIO.read(URL). It's the same as ImageIO.read(InputStream) only it uses URL.openStream() as the InputStream.

Offline magemog

Senior Newbie




Jaded Samurai


« Reply #7 - Posted 2005-02-02 00:15:37 »

So I tried what you said jbanes, does it work now?  :-/

www.ki-rush.com/what.jar

Hmm... sounds vaguely familiar... yeah
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #8 - Posted 2005-02-02 00:24:22 »

why not use the GTGE? Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline magemog

Senior Newbie




Jaded Samurai


« Reply #9 - Posted 2005-02-02 01:00:03 »

http://www.goldenstudios.or.id/products/GTGE/  

That? It looks like there is a ugly screen required at start up with an unsightly selection thing  there as well and... and... and... are you saying I should give up trying to handle this kind of stuff
Click to Play

Hmm... sounds vaguely familiar... yeah
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #10 - Posted 2005-02-02 01:26:42 »

Quote
So I tried what you said jbanes, does it work now?  :-/

www.ki-rush.com/what.jar


Are you sure you uploaded it? I'm still getting the same stack trace:

1  
2  
3  
4  
5  
6  
7  
8  
9  
Exception in thread "main" java.io.FileNotFoundException: map1.dat (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)
        at Map.MapChange(Map.java:105)
        at Map.<init>(Map.java:90)
        at Game.<init>(Game.java:38)
        at RunGame.main(RunGame.java:10)


Quote
why not use the GTGE? Smiley


You'll have to pardon my ignorance, but how would that help in this case? The issue includes loading maps as well as images. :-/

Java Game Console Project
Last Journal Entry: 12/17/04
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #11 - Posted 2005-02-02 01:47:53 »

Quote
and... and... and... are you saying I should give up trying to handle this kind of stuff
Click to Play


Personally, I would never encourage anyone to quit unless they were in programming for all the wrong reasons (e.g. money, fame, but not the love of creating stuff). I may come across as a jackass sometimes (I don't mean to, honest!), but my experience has been that it can spur people to find out what they're truly capable of when they put their mind to something.

Remember, you didn't learn faster when you were a kid. You just had fewer inhibitions about toying with things! :-)

BTW, I finally got some time so I extracted your game and got it running. I think the problem you may be having is that your key handling is wrong. If you only move the character on a key event, you'll end up seeing a pause followed by a constant stream of events. This is called "typematic delay" and is easy to see by opening a text editor and holding down a key. Notice that one character prints, then there's a pause until the next character prints.

The solution to this problem is to use flags that let you know when a key is down or up. Consider the following code for a moment:

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  
int x;
int y;

boolean left;
boolean right;
boolean up;
boolean down;

public void run()
{
  BufferStrategy strategy.getBufferStrategy();

  while(true)
  {
    //draw stuff

    if(left) x--;
    if(right) x++;
    if(up) y--;
    if(down) y++;
  }
}

public void keyPressed(KeyEvent evt)
{
  if(evt.getKeyCode() == evt.VK_LEFT) left = true;
  if(evt.getKeyCode() == evt.VK_RIGHT) right= true;
  if(evt.getKeyCode() == evt.VK_UP) up = true;
  if(evt.getKeyCode() == evt.VK_DOWN) down = true;
}

public void keyReleased(KeyEvent evt)
{
  if(evt.getKeyCode() == evt.VK_LEFT) left = false;
  if(evt.getKeyCode() == evt.VK_RIGHT) right= false;
  if(evt.getKeyCode() == evt.VK_UP) up = false;
  if(evt.getKeyCode() == evt.VK_DOWN) down = false;
}


Does that help clarify it? Otherwise your scroll code seems to run just fine. :-)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline magemog

Senior Newbie




Jaded Samurai


« Reply #12 - Posted 2005-02-02 03:44:07 »

Wow. My jaw just dropped at how awesome that worked. It took less than 2 minutes to fix it after I read that.  

This bug was such a headache I thought I might have to change all of my plans for the game around it. Many thanks. Smiley


Edit: nevermind, I seem to have the jar actually working now. I had the same errors as you it when it wasn't working.. so hopefully it would work now for you... does this .jar load properly?  http://www.ki-rush.com/enpitsu.jar

Hmm... sounds vaguely familiar... yeah
Offline role

Junior Devvie




Java manmoth is coming!


« Reply #13 - Posted 2005-02-05 10:52:38 »

Quote
http://www.goldenstudios.or.id/products/GTGE/

That? It looks like there is a ugly screen required at start up


Oh well an ugly screen lol..... Grin
I'm not good at drawing graphics, so forgive my graphics skill Smiley

Well for making tile based RPG, I have made a demo rpg and the source code is available to peek at, just in case you are interested of how I managing the player movement animation and other rpg stuff.
Here is the webstartable :
http://goldenstudios.or.id/products/games/bin/demorpg.jnlp
And the source code :
http://goldenstudios.or.id/products/games/bin/source/demorpg.zip

If you really don't like the ugly screen I can remove it, I'm the one and only developer you know Wink
Anyway the 'ugly screen' is only exists in the last version, it's just an exchange of my hard work by giving it freely, not too annoying isn't it Smiley
At least the 'ugly screen' has translucent effect that can impressive player before entering the real game Tongue

Golden T Game Engine - Java2D/OpenGL Game Engine
Golden T Website | Golden T Forum
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.

toopeicgaming1999 (68 views)
2014-11-26 15:22:04

toopeicgaming1999 (58 views)
2014-11-26 15:20:36

toopeicgaming1999 (13 views)
2014-11-26 15:20:08

SHC (26 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (30 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (76 views)
2014-11-13 12:34:49

digdugdiggy (54 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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