GPWiki.org
GPWiki.org
It is currently Mon May 20, 2013 8:56 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Dec 20, 2011 8:26 pm 
Rookie

Joined: Tue Dec 20, 2011 8:07 pm
Posts: 2
Hello Forum, I've always been fascinated by the concept of programming games, and I have been learning a little python, java, sometimes dabbling in to game programming a tiny bit...
But now I've been learning Java in school as well, so I have something to start with.

Anyway, I'm using Netbeans and I want to create something like this:

-----
|...|
|...|
|..x|
----- (It looks a lot better in Netbeans, lined up and stuff)

So basically a small room, maybe even more than one, where the player controls the X with the w,a,s,d keys. Very simple, I've done the exact same thing in Python.

Although the above is not relevant to the problem, I felt like telling you guys anyway. :D
Actual problem:

public static String getnextkey(){
String key = "";
Scanner keyscan = new Scanner(System.in);
System.out.print("Input:");
while (!key.equals("w") || !key.equals("a") || !key.equals("s") || !key.equals("d")) {
key = keyscan.next(); } System.out.println("yes");
return key;

I couldn't get this to work, and added the sys.out.print("yes") as a sort of debug method, and whatever I type, I can't seem to get that "yes."
Why?


Top
 Profile  
 
PostPosted: Wed Dec 21, 2011 1:58 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
I don't know what "Scanner" is, but if it is http://docs.oracle.com/javase/1.5.0/doc ... anner.html it most likely does not do what you think it does.
That class splits an input stream into tokens (by default words seperated by white space it seems). It does not seem to do keyboard scanning at all. You may want to print values of 'key' to confirm.

Another thing that you may run into is that default input streams tend to be line-oriented. That is, your program may not get anything until you hit 'enter'. This is better for efficiency (IO is very slow and CPU intensive), and it allows eg editing (with back-space) of the input line to fix typos).
I don't know enough Java to know whether the above is really the case and/or how to fix it.

I hope my reply gives you some pointers towards a working solution.


Top
 Profile  
 
PostPosted: Wed Dec 21, 2011 5:24 pm 
Rookie

Joined: Tue Dec 20, 2011 8:07 pm
Posts: 2
Thank you for your input, Alberth.
Well I was searching the interwebs looking for a way to get user input. And I found the scanner thing somewhere. And it does actually get user input, if I sys.out.print(key), than my input text gets printed. I just read the doc about it, and it does seem to do some wierd stuff. But, you can try it out yourself, it should work.
But because I'm not quite sure exactly what/how much it does, I'll use something else. Any suggestions?


Top
 Profile  
 
PostPosted: Wed Dec 21, 2011 6:23 pm 
Bytewise

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 277
Location: Here (where else?)
I did a little bit of a search, and ended up with http://docs.oracle.com/javase/6/docs/ap ... ystem.html which has a Console() method. That thing ( http://docs.oracle.com/javase/6/docs/ap ... nsole.html ) has a Reader() method, and that thing ( http://docs.oracle.com/javase/6/docs/ap ... eader.html ) has a read() :D
Yay for Java library classes :D
The docs seem to suggest it does single character input, but not exactly how or when.
I guess that is one thing you can try.

Another thing you can look into is that System.in has properties. I could not find a list of the properties, but you can query them. Perhaps if you do that, you can find a property that controls input buffering (which you want to disable).

In unix, what you want is called raw input.

I don't seem to have Java available here at all, so cannot try anything. Sorry.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group