This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello everybody, I've been working on a 2d sidescroller for awhile now and worked through some pretty difficult bumps in the road, but this one has me stumped, because it has to do with how Java keybindings work.
Instead of using keyListeners this time around, I read up on key bindings (setup through put() and InputMaps and ActionMaps). Most said they were more used for game controls. And although the process ended up being really similar, and I didn't see any advantage, I stuck with it. Only now I'm having game issues.
One of the most common inputs from the user in a sidescroller is to hold a directional key to move the character left/right, then while still holding it, jump, and continue. Using WASD for moving and SPACE for jumping, the list of keys being pressed during this time looks like this.......AAAAAAAAAAAAAAA*SPACE**SPACE RELEASED*..... The problem is that even though the user is still holding A the whole time, at the end there, after the SPACE press and release, it is no longer recognized without the user releasing A and then pressing it again. This is very impractical to have in a game. So here's the question...
I haven't done a significant amount of figuring out how keyListeners would work in situations like this, so I'm asking before I go to change anything. Would keyListeners have the same issues? Should I switch to them or try something else? Is this a common issue?
probably, but try it - it's only a few lines to test it
> Should I switch to them or try something else?
assuming this is not for an applet, try something like this,
keyPressed() to start a timer (in this example, for the 'a' key),
and keyReleased() to stop the timer. the timer gives you
control over how long between intervals,
Thanks for the quick reply. I had thought of using a timer and even tried in multiple ways. What just hit me is that I was using the java.util.Timer instead of the javax.swing.Timer (I'd noticed this before, but didn't take the time to look into it because the util Timer was getting the job done).
So I went down and looked at the javadoc for it, and I have to say, this is going to open things up quite a bit. And the method you suggested would be perfect for controlling my bindings more precisely.