Moving a sprite using the input processor libgdx
I am using a class called InputHandler which implements a InputPorcessor.
However I am having a problem where I cannot move the player to a point I
choose by clicking on the screen.
The reason is because I'm unsure as to how to get the x,y coordinated on
the map which I would then use to set the player's new position.
This is my InputHandler class
public class InputHandler implements InputProcessor {
private OrthographicCamera cam;
private boolean dragged = false;
private Player player;
private TiledMap map;
private Vector2 oPos;
public InputHandler(OrthographicCamera camera, Player player,
TiledMap map) {
this.cam = camera;
this.player = player;
this.map = map;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer,
int button) {
oPos = new Vector2(Gdx.input.getX(), Gdx.input.getY());
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer,
int button) {
if (!dragged) {
// move the player
} else
dragged = false;
return false;
}
public boolean touchDragged(int x, int y, int pointer) {
dragged = true;
moveCamera(x, y);
return false;
}
private void moveCamera(int touchX, int touchY) {
Vector2 nPos = getNewCameraPosition(touchX, touchY);
cam.translate(nPos.sub(cam.position.x, cam.position.y));
Gdx.app.log(PArena.LOG, "Moved Camera");
oPos.set(touchX, touchY);
}
private Vector2 getNewCameraPosition(int x, int y) {
Vector2 nPos = oPos;
nPos.sub(x, y);
nPos.y = -nPos.y;
nPos.add(cam.position.x, cam.position.y);
return nPos;
}
}
No comments:
Post a Comment