Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Key Bindings Vs. KeyListener

  1. #1
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Key Bindings Vs. KeyListener

    People in different forums keep suggesting to me to use Key Bindings instead of KeyListener in Java 2D games.

    What are the advantages and disadvantaged of each one? Is Key Bindings really better for 2D games?

    Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Key Bindings Vs. KeyListener

    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AvivC is offline Member
    Join Date
    Dec 2013
    Posts
    26
    Rep Power
    0

    Default Key Bindings

    People keep suggesting to me to use Key Bindings in my Java 2D games instead of Key Listener.

    So I learned it and I'd like to know wether I understand correctly how to use it.

    Let's say I have a game, with two playes, each player can push 5 buttons.

    Player 1:

    - UP arrow - move forward
    - LEFT arrow - change angle of movement
    - RIGHT arrow - change angle of movement
    - SPACE key - fire missile
    - 'L' key - fire secondary missile

    Player 2:

    - 'W' key - move forward
    - 'A' key - change angle of movement
    - 'D' key - change angle of movement
    - CAPS-LOCK key - fire missile
    - 'Z' key - fire secondary missile

    If I want the program to react differently to each one of the different key-presses, than this is what I have to do: (?)

    1- Create **10** new nested AbstractAction classes inside the class that runs most of the game logic.
    2- Create an instance of every one of these **10** new classes, and bind each one to a key.

    Is this correct? Is it really logical to create **10** new classes only for pushing buttons?
    If this way is the correct way, than it seems that KeyListener might be a better choice.

    Thanks

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Key Bindings

    Yes, you'll have to create 10 instances, but nothing forces you to use nested classes. 10 instances isn't really all that much, especially if you compare that to the total number of instances Java uses behind the scenes.
    gimbal2 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Key Bindings

    Double posted - Key Bindings Vs. KeyListener

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Key Bindings

    Quote Originally Posted by jim829 View Post
    Double posted - Key Bindings Vs. KeyListener

    Regards,
    Jim
    For better or worse, I've merged the two threads. Thanks for the heads up.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Key Bindings

    Quote Originally Posted by KevinWorkman View Post
    Yes, you'll have to create 10 instances, but nothing forces you to use nested classes. 10 instances isn't really all that much, especially if you compare that to the total number of instances Java uses behind the scenes.
    Its a fart in a hurricane really :)

    But then I meet the OP halfway - having to create 10 instances just to handle some keys... I don't know. I'd have to dig out some old Java2D code at home, but I tended to have only one class that would register key input; this one class would register the state of a keypress AND release in an array (index bound to a key code) which the specific game code classes would then interrogate to see what was up (or rather: down) since the last game tick.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Key Bindings

    Quote Originally Posted by gimbal2 View Post
    Its a fart in a hurricane really :)

    But then I meet the OP halfway - having to create 10 instances just to handle some keys... I don't know. I'd have to dig out some old Java2D code at home, but I tended to have only one class that would register key input; this one class would register the state of a keypress AND release in an array (index bound to a key code) which the specific game code classes would then interrogate to see what was up (or rather: down) since the last game tick.
    Yeah, this is pretty much the way I do it (although I'm stupid so I have a separate boolean for each key). People recommend key bindings over KeyListeners mostly due to issues with focus. I've mostly stuck with KeyListeners though.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: Key Bindings Vs. KeyListener

    If it works - it works and it is a valid solution. I just program games in the old fashioned way always; not event driven but rather polling of state. So a listener wouldn't be my thing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. My key bindings don't work...
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2013, 10:28 PM
  2. Replies: 2
    Last Post: 11-01-2012, 03:04 PM
  3. Unsure of whether to use key bindings
    By kjkrum in forum AWT / Swing
    Replies: 1
    Last Post: 04-15-2012, 07:33 AM
  4. can one disable key bindings on a JComponent?
    By gib65 in forum AWT / Swing
    Replies: 2
    Last Post: 10-14-2010, 04:44 PM
  5. genesis 3.0-RC2: Swing, SWT and Thinlet bindings
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •