ajwgeek
ajwgeek

Reputation: 21

Slick2D Movement Issue

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

Upvotes: 2

Views: 1586

Answers (1)

jefflunt
jefflunt

Reputation: 33954

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.

Upvotes: 3

Related Questions