public void mouseMoved(int oldx, int oldy, int newx, int newy) {
  super.mouseMoved(oldx, oldy, newx, newy); 

we can get the mouse location in this method via newx, newy . i want to know is there any such way to get the location in slick2D? when we press the arrow keys and move a image of a man on the game surface(tile map)?

  • \$\begingroup\$ This looks similar to your other question. Are you asking something different and does the answer to the other question not answer this? \$\endgroup\$ Aug 19, 2011 at 18:32
  • \$\begingroup\$ yes i'm asking different thing here, other question is sounds similar though. \$\endgroup\$ Aug 19, 2011 at 19:12

1 Answer 1


I think you are confusing some concepts..... If you use your keyboard to move your player, then you usually store the location of the player's position in the player's object.

If you use your mouse to move the player, then you use the location of the mouse on the screen to set the player's position on the game.

So, there is no such thing as location of keyboard as there is for location of mouse pointer.

In any case, whichever way you update the location of your player, his position is still stored in the object. I.e.

class Sprite {
    Point position;
    Texture texture;

class Level implements MouseListener, KeyListener {
    Sprite player;
    void update(...) {
        //whether you updated it with mouse or keybaord
        Point position = player.getPosition(); 

        // Do something with this...

    // Update player position using mouse
    public void mouseMoved(int oldx, int oldy, int newx, int newy) {
        player.setPosition(new Point(newx, newy);

    // Update player position using keyboard
    public void keyPressed(int key, char c) {
        if (key == Input.KEY_W) {
            player.getPosition().X += 1;
        } else if (key == Input.KEY_S) {
            player.getPosition().X -= 1;
        } else if (key == Input.KEY_A) {
            player.getPoisition().Y -= 1;
        } else if (key == Input.KEY_D) {
            player.getPosition().Y += 1;

Hope this helped clear some things...


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .