Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Best way of handling input?  (Read 2248 times)
0 Members and 1 Guest are viewing this topic.
Offline Fid

Senior Newbie

Medals: 1

« Posted 2011-03-22 14:23:45 »

Hi all,

Looking for a bit of advice here on the best way of how to handle input in my game engine. I don't have any code to show you (well, none that works completely) but I do have several ideas - but really they seem to suck/make it difficult for the end developer who is creating the game.

Idea 1.

Have some class that extends KeyListener and has 3 variables that store the keycode for each key pressed, released etc. Then, an update method in my engine class will somewhere call the method getKeyPressedKeyCode() for example on that class, and then have a switch statement to decide what to do (i.e. if left key pressed, decrease x by 5.0 on some sprite).

However, could there be any chance of being multiple key presses in one gameloop, or could input be missed?

Idea 2.

Have another class, possibly something like EventQueue that collects all the events from a key being pressed and released etc. When this happens, in another class (Keyboard) I would have a collection of all the keycodes/keyevents and depending on which was pressed, from the EventQueue it would set them to true/false.

Then, in the update method in the game engine, there would be again checks to decide what to do based on what keys are pressed.

Does anyone have any better ideas?

My main problem is, as this is a game engine and not just a game, I'm trying to make it kind of simple of the developer. Therefore I cannot set a variable in say the SpriteManager to say (moveLeft == true) because one developer may want the left arrow key to be something else, or not move left at all etc. Therefore I'm trying to be as generic as possible (which is difficult!)

So any suggestions, ideas would be gratefully received and should get me moving on with the rest of my engine!


Just a FYI: I'm using Java2D for displaying graphics on the screen, not something like LWJGL. I wish I had of done now, particularly as there seems to be classes there to handle input etc, but with 6 weeks left on this, I don't think there is time to change now!
Offline IronclawsBt

Junior Devvie

Medals: 1

« Reply #1 - Posted 2011-03-22 15:19:33 »

You could check out this tutorial I wrote. It handles input from Java2D.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Offline lhkbob

JGO Knight

Medals: 32

« Reply #2 - Posted 2011-03-22 17:41:30 »

Here's a system that I'm pretty happy with:

It tracks the entire state of the keyboard and mouse (i.e. which keys are down, which mouse buttons are down, where the mouse is). Every event gets processed in order, creating a new state to match what the event did (i.e. release a key).  This creates a list of states that then get processed on request in the main loop.  This lets processing happen in the main loop but will not miss events (which can happen if you just use a single state and frame rates are low).

When processing the states, there are triggers that take the previous and current state that can be used to build conditionals, such as "run X when 'A' is pressed the first time and the mouse is dragging".  This design has been heavily influenced by the input system in the Ardor3D engine, so I don't mean to take credit for it.

Pages: [1]
  ignore  |  Print  

DesertCoockie (33 views)
2018-05-13 18:23:11

nelsongames (75 views)
2018-04-24 18:15:36

nelsongames (70 views)
2018-04-24 18:14:32

ivj94 (752 views)
2018-03-24 14:47:39

ivj94 (82 views)
2018-03-24 14:46:31

ivj94 (622 views)
2018-03-24 14:43:53

Solater (98 views)
2018-03-17 05:04:08

nelsongames (179 views)
2018-03-05 17:56:34

Gornova (405 views)
2018-03-02 22:15:33

buddyBro (1065 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!