• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Got a small keybinding bug

 
Greenhorn
Posts: 27
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks,
Caleb
 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Would keyListeners have the same issues?

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,

 
Caleb Kemper
Greenhorn
Posts: 27
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks for the help,
Caleb
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic