java - Moving a sprite using the input processor libgdx -
i using class called inputhandler implements inputporcessor. having problem cannot move player point choose clicking on screen.
the reason because i'm unsure how x,y coordinated on map use set player's new position.
this 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 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; } }
to point of game world (which @ through camera
), need use snippet:
vector3 screentouchposition= new vector3(touchx, touchy, 0); cam.unproject(screentouchposition); // change vector directly vector3 gameworldtouchposition = screentouchposition;
Comments
Post a Comment