android - Libgdx : Clicklistener doesnt work on resize -
my code functional if take out screen resize concern. have tried on multiple devices including tablet , resolution bad. trying keep same aspect ratio of viewport. using stage add buttons , images. clicklistener responding part in gutter , part on play button. stackoverflow not letting me post image because of new account. please me in understanding doing wrong , make sure piece of code works on phones , desktop.
private static final int virtual_width = 480; private static final int virtual_height = 800; public void resize(int width, int height) { // calculate new viewport vector2 size = scaling.fit.apply(virtual_width, virtual_height, width, height); int viewportx = (int)(width - size.x) / 2; int viewporty = (int)(height - size.y) / 2; int viewportwidth = (int)size.x; int viewportheight = (int)size.y; gdx.gl.glviewport(viewportx, viewporty, viewportwidth, viewportheight); } @override public void show() { batch = new spritebatch(); stage = new stage(); camera = new orthographiccamera(virtual_width, virtual_height); camera.settoortho(true, virtual_width, virtual_height); worldcoords = new vector3( virtual_width, virtual_height, 0); camera.unproject(worldcoords); camera.update(); white = new bitmapfont(gdx.files.internal("font/white.fnt"), true); white.setscale(1f, -1f); skin = new skin(); atlas = new textureatlas("button.pack.pack"); skin.addregions(atlas); buttonstyle = new textbuttonstyle(); buttonstyle.up = skin.getdrawable("button"); buttonstyle.down = skin.getdrawable("buttonpressed"); buttonstyle.font = white; buttonstyle.fontcolor = color.black; buttonplay =new textbutton("play", buttonstyle); buttonplay.setwidth(virtual_width/3); buttonplay.setheight(virtual_height/12); buttonplay.setposition(stage.getviewport().getrightgutterwidth(), stage.getviewport().getworldheight()/2); buttonplay.addlistener(new clicklistener(){ @override public void clicked(inputevent event, float x, float y){ ((game) gdx.app.getapplicationlistener()).setscreen(new mainflow(g)); } }); gdx.input.setinputprocessor(stage); image img = new image(new texture(gdx.files.internal("optimized-bg_2.png"))); img.setfillparent(true); stage.addactor(img); stage.addactor(buttonplay); } @override public void render(float delta) { camera.update(); batch.setprojectionmatrix(camera.combined); gdx.gl.glclearcolor(0, 0, 0, 1); gdx.gl.glclear(gl20.gl_color_buffer_bit); stage.act(); stage.draw(); }
i don't understand want, @ first try update stage, in resize method, , read libgdx docs
stage.getviewport().update(width, height, true);
Comments
Post a Comment