Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 1338 times)
0 Members and 1 Guest are viewing this topic.
Offline Fid

Senior Newbie


Medals: 1



« Posted 2011-03-22 15: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!

Thanks!

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 Member


Medals: 1



« Reply #1 - Posted 2011-03-22 16: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 18:41:30 »

Here's a system that I'm pretty happy with: http://code.google.com/p/ferox-gl/source/browse/#svn%2Ftrunk%2Fferox-core%2Fsrc%2Fcom%2Fferox%2Finput%2Flogic

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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (76 views)
2014-04-15 18:08:23

BurntPizza (69 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (211 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!