2

So, I converted a game to Slick2D. The movement is broke, and I am at a loss. Before, we used KeyPressed and keyReleased methods, but now with Slick2D movement isn't working right.

Yea, nothing has gone right with converting to Slick2D. First the launcher, which I had a help topic on before, and now this. Though, the other topic was an issue with WebStart hating code.

You can only move right, using A. And you can't stop moving. Am I using the right methods? How can I fix it? Any help is greatly appreciated!

Here is a PasteBin link to the code, if it helps! http://pastebin.com/GRH86Yuw

ajwgeek
  • 21
  • 5

1 Answers1

3

I'm a fan of Slick, and I'd be happy to help.

The fundamental difference is that Slick is a polling model, not an event-driven model when it comes to input. Basically, in your logic update method you loop through the keys bound to your events, and check to see if any of the keys are currently pressed, and then trigger those events. For a number of reasons that I can go into if you like, polling tends to work better for games, especially with a large number of keys. It's just a different way of doing things, an not that complicated. The biggest upside is that you get centralized input processing a single method, instead of having it spread across multiple KeyListener instance objects.

If you want to look at Pedestrians - a simple pedestrian sim implemented in Slick - you can see an example of how to handle input in Slick.

Specifically, I handle input in this file (lines 192-295), inside the processInput method. Basically, you pass in a reference to the GameContainer object (the Slick object that contains your game), and from that you can get an instance to the Input instance that will allow you to check which keys are pressed, what mouse buttons are clicked, etc.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Thanks! So, do I just check to see if W is being pressed and if it is, run a method? – ajwgeek Dec 17 '11 at 03:35
  • Yep, pretty much. There are methods in Slick's input classes to check which keys are pressed and/or if a particular key is pressed. – jefflunt Dec 17 '11 at 05:29
  • Updated my answer to point to the specific file and method in `Pedestrians` that handles keyboard/mouse input. Good luck with Slick! I think it's awesome. – jefflunt Dec 17 '11 at 20:48