Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am writing a Java game.

My problem is to get a really smooth input. If I hit the key W the character has to go forwards, but if I hit A and release the W key, the character doesn't go left ways :( .

It isn't smooth. Which ways are given in common game developing for a smooth input?

My current source code (Input.java): http://pastebin.com/9Z9qtXik (MainLoop.handlingInputs): http://pastebin.com/Vx8BzSpL

share|improve this question

closed as off-topic by Anko, Seth Battin, Jari Komppa, Noctrine Apr 12 '14 at 21:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Seth Battin, Jari Komppa, Noctrine
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 0 down vote accepted

In your public void keyReleased(KeyEvent arg0) method you are marking all keys as released, not just the one which actually was released. The result is that when the player releases one key while still holding another, the other key will also be considered released.

You need to check arg0 for the key which actually was released, just the way you already do in keyPressed.

Another thing which could cause problems is that you implemented both keyPressed and keyTyped. These two events behave very similar, so when you implement one, you usually have no reason to also implement the other. The main difference is that keyPressed reports modifier-keys like shift as separate events, while keyTyped reports visible characters entered. When you type a capital-A, keyPressed is called twice, first for shift, then for a. keyTyped is called for a single event A. For game input, keyPressed is usually more appropriate.

share|improve this answer

I would suggest using KeyBindings for smooth input handling! Right now it is true you have an error in your code as stated by @Phillipp, but KeyListening is not recommended to be used in games.

KeyBindings are a much newer update to Java and handle input much better that KeyListeners do. A quick Google search will provide you with more than enough sample code to learn it :)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.