0
\$\begingroup\$

I want a image to follow the mouse, and I use Image class of Slick 2D:

public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
    mouseImage = new Image("res/mouse.png");
}
public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics  graphics) throws SlickException {
    mouseImage.draw(0,0,0.1f);
}

But it seems that it does not have method for re-positioning a Image object. So how can I implement it (in update method)? Thanks.

\$\endgroup\$
2
  • \$\begingroup\$ I've tried remove the image and re-draw a new one, but it does not work. \$\endgroup\$
    – Renfei
    Dec 16 '13 at 9:08
  • \$\begingroup\$ Please edit your question to include that code. There is not enough information in your question to identify the problem. \$\endgroup\$ Dec 16 '13 at 13:01
0
\$\begingroup\$

You need to create variables that determine the position of your image, make your render function use those variables as the position, and change those variables when you want to move your object.

Here's how you can do it:

private float x = 0;
private float y = 0;

public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
    mouseImage = new Image("res/mouse.png");
}

public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics  graphics) throws SlickException {
    mouseImage.draw(x,y,0.1f);
}

public void moveDown(float amount) {
    y += amount;
}

public void moveRight(float amount) {
    x += amount;
}
\$\endgroup\$
0
\$\begingroup\$

Haven't used Slick2D, but I guess you should just change the coordinates, maybe when rendering?

\$\endgroup\$
0
\$\begingroup\$

If you want it to be in update, you can use the setCenterX and setCenterY methods.

mouseImage.setCenterX(Mouse.getX());
mouseImage.setCenterY(Mouse.getY());

However Y coordinates are going to be reversed. If you want them to correspond to proper ones you can do:

mouseImage.setCenter(container.getInput().getMouseY());
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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