java - Clicklistener - different coordinates while resizing -
i have tiled map on set characters(every character of size of 1 tile). managed make them clickable, when screen resizes works perfect. every time click on character want button show above it. button use stage , place button in place clicked small transition , works.
my problem when try use clicklistener on button. if screen not resize, clicklistener works. problem starts when screen resized - clicks on players works well, button not work - after resize clicking space , button space not aligned. test purposes made test map shows me grids. seems stage not resized. picture reference. tried many solutions came upon on internet , still can't find solution problem. have shortened code down minimum basic example:
public class test extends applicationadapter { public static stage stage; public tiledmap tiledmap; static orthographiccamera camera; tiledmaprenderer tiledmaprenderer; public static boolean showmenu; gesturedetector gesture; inputmultiplexer myinputmultiplexer; public static int posx, posy; public static image move; public texture movemenu; @override public void create() { movemenu = new texture(gdx.files.internal("move.png")); gesture = new gesturedetector(new mygesturelistener()); myinputmultiplexer = new inputmultiplexer(); float unitscale = 1 / 32f; camera = new orthographiccamera(); camera.settoortho(true, 33, 21); stage = new stage(new screenviewport()); stage.getviewport().setcamera(camera); tiledmap = new tmxmaploader().load("test.tmx"); tiledmaprenderer = new orthogonaltiledmaprenderer(tiledmap, unitscale); myinputmultiplexer.addprocessor(stage); myinputmultiplexer.addprocessor(gesture); gdx.input.setinputprocessor(myinputmultiplexer); move = new image(movemenu); move.setwidth(2); move.setheight(2); move.addlistener(new clicklistener() { @override public void clicked(inputevent event, float x, float y) { move(); //my action, works fine showmenu = false; } }); stage.addactor(move); } @override public void render() { super.render(); stage.act(); tiledmaprenderer.setview(camera); camera.update(); tiledmaprenderer.render(); if (showmenu) { mainmenudraw(); } } public static void mainmenudraw() { move.setposition(posx, posy-2); stage.draw(); } public void resize(int width, int height) { stage.getviewport().update(width, height, true); } public static orthographiccamera getcamera() { return camera; } public static vector3 unprojectcoords(vector3 coords) { camera.unproject(coords); return coords; } }
and part of gesturelistener:
public boolean touchdown(float x, float y, int pointer, int button) { vector3 temp_coord = new vector3(x, y, 0); vector3 coords = test.unprojectcoords(temp_coord); return false; } @override public boolean tap(float x, float y, int count, int button) { vector3 temp_coord = new vector3(x, y, 0); vector3 coords = test.unprojectcoords(temp_coord); test.posx = (int) coords.x; test.posy = (int) coords.y; tap = true; test.showmenu = true; return false; }
i suggest read more re-sizing , displaying pixels.
you need recalculate pixels when render something. - player, background image, buttons, events.
actually don't need use resize method, camera width , height.
you need share more code, because depends on everything. don't see how doing rendering.
- tiledmaprenderer.render(), background rendering / layers?
- player rendering?
- menu rendering?
- buttons rendering?
example: (the same should event handler)
public gamebutton(textureregion reg, float x, float y, orthographiccamera cam) { this.reg = reg; this.x = x; this.y = y; this.cam = cam; width = reg.getregionwidth(); height = reg.getregionheight(); vec = new vector3(); texture tex = game.res.gettexture("hud"); font = new textureregion[11]; for(int = 0; < 11; i++) { font[i] = new textureregion(tex, 32 + * 9, 16, 9, 9); //use height , width here) } }
Comments
Post a Comment