Ok, so right now I have it on the keyPressed and keyReleased() calls a method is called that passes the key pressed and they set bools to on or off, and then call a move method which incriments my games player pos, etc, etc. But when holding down a key there is a slight pause which gets quite anoying, also it gets confused sometimes when too many keys are pressed, or you are say moving diaginal and then press an opposing key(say youre moving up and right and then hit left) it seems to kind of crap out. Its kind of hard to explain but im guessing other people have had this problem so any help would be apreciated with a more efficient way of doing it, or one that is just cleaner and better all around. Thanks alot
Something like this might work:
public class Game
extends KeyAdapter
{
private boolean goLeft = false;
private boolean goRight = false;
public void mainGameLoop()
{
while (true)
{
if (goLeft)
{
// go left
}
else if (goRight)
{
// go right
}
}
}
public void keyPressed(KeyEvent e)
{
int code = e.getCode();
if (code == KeyEvent.VK_LEFT)
{
goLeft = true;
goRight = false;
}
else if (code == KeyEvent.VK_RIGHT)
{
goRight = true;
goLeft = false;
}
}
public void keyReleased(KeyEvent e)
{
int code = e.getCode();
if (code == KeyEvent.VK_LEFT)
{
goLeft = false;
}
else if (code == KeyEvent.VK_RIGHT)
{
goRight = false;
}
}
}
I know what you’re talking about, I have the same problem. If you find something, can you please tell me?
I’m trying to make a 2-player Pong game. The first player’s pad is controlled with W and S, the second player’s with the arrow keys. The problem is that for example if you press any other key while you’re holding the S key, the pad will just stop wherever it is, until you release and repress the S key. So, that’s really annoying. I programmed this using keystrokes and actions (if that’s a very bad way of doing this, I apologize, I’m a newb).
Basically I have these lines in the main class’s constructor:
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "p1pressedDown");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "p1pressedUp");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "p2pressedDown");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "p2pressedUp");
getActionMap().put("p1pressedDown", p1downAction);
getActionMap().put("p1pressedUp", p1upAction);
getActionMap().put("p2pressedDown", p2downAction);
getActionMap().put("p2pressedUp", p2upAction);
Then I have 4 inner classes (one for each action), each with a constructor, and an actionPerformed() method that calls the method used for moving the pad.
If someone could help me, that’d be great! Thanks a lot
ok MoO_Cow, I figured it out using the code at this url: http://forum.java.sun.com/thread.jsp?forum=406&thread=369480
I don’t know if you’re writing it for an applet or like me, my class extends JPanel. Well, there isn’t much difference anyways. You just have to write an isFocusable method like that:
public boolean isFocusable() {
return true;
}
That will override the isFocusable() method for java.Awt.Component .
If you ask for it, I’ll send you my code. Hope this will help you.
yeah, that’s a good link. I see the problem as simply being that when another key is pressed, the repeat of the first key stops so the paddle stops moving. My solution to keyboard problems has always been to set a pressed flag for the key in keyPressed and set it to false in keyReleased, that way you can press anything else you want and the game is not dependant on OS repeat delays, etc.
Definitly agree about setting flags! With Java’s input model, any other way is just asking for trouble. The problem gets worse when you start using 1.4’s fullscreen api - syncing key presses to a fast running rendering loop just doesn’t work very well.
Coming from a C++ game programming background I would appreciate some way of polling the input devices manually but I’ll probably be flame grilled for saying that
[quote]Definitly agree about setting flags! With Java’s input model, any other way is just asking for trouble. The problem gets worse when you start using 1.4’s fullscreen api - syncing key presses to a fast running rendering loop just doesn’t work very well.
Coming from a C++ game programming background I would appreciate some way of polling the input devices manually but I’ll probably be flame grilled for saying that
[/quote]
U won’t get flamed for saying that here.
every1 wants that ;D
oh the joy that is LWJGL 8)
however,
Its looking brighter in that department.
The game api parts of MIDP2.0 allow you to poll the key states directly.
So u never know, we might see it in Java1.5
(now, wheres that fingers crossed smiley…)
Fingers need to be crossed pretty tight if you want to see many changes like that in 1.5 - seems mainly to be language updates (I like the look of generics - finally a typesafe way of using vectors etc.)
Also, LWJGL is pretty good - heck I learnt alot about OpenGL from it but with the hass’ of loading textures into byte buffers etc. I am always tempted to revert to C++! If they improve the memory transfer system I’m in
See JInput for controlelr discovery and polling, including the keyboard.
Ok, so in the keyPressed and keyReleased event handlers you store the current state of the “movement” keys in key state variables.
The question is: how often and where do you test these variables? (I am talking about a Java application, not a applet)
With a Model-View-Controller (MVC) architecture there’s an interrupt driven simulation module whichs handles the model and its timing (usually the only fixed timer driven event). So a TimerTask (or that like) is already running.
However you’ll hesitate to poll the key state variables from within that simulation TimerTask, because logically it doesn’t fit in there, it has to be in the controller module (of course).
So you’ve to start another TimerTask just to handle the key state variables. This looks to be quite “heavy” to me - another Thread just to scan 5 keys, oh dear.
Since you also want to use the mouse buttons as movement “keys”, too, I think you’ll have to create a second TimerTask, isn’t it?
Or do I miss the point now?
Okay, you could use Jinput. However for now it’s Win32 only (the other platforms coming). Also I hesitate to include another binary binding (next to Jogl’s) “just” to be able to scan 5 keys and a mouse.
No question, for larger Java games which support Joypads/etc. Jinput is very important.
be warned, the typical keyPressed keyReleased boolean approach will not work in Linux. It reports key events differently than other OSes do, a key held down will report as many keyReleased events as it does keyPressed events.
Until I get off my rear and look into jinput and such I’m dealing with this by disabling key repeat when in Linux (just a simple Runtime.exec(“xset r off”)). And yup, that is as much a ripe pain the ass as it sounds.