0

I'm making a game with Java and Libgdx scene2d. This is my code in the constructor:

atlas = new TextureAtlas("gfx/button.pack");
skin = new Skin(atlas);

TouchpadStyle touchpadStyle = new TouchpadStyle();
touchpadStyle.background = skin.getDrawable("button.up");
touchpadStyle.knob = skin.getDrawable("button.down");
touchpadStyle.knob.setLeftWidth(20);
touchpadStyle.knob.setRightWidth(20);
touchpadStyle.knob.setBottomHeight(20);
touchpadStyle.knob.setTopHeight(20);
controller = new Touchpad(1f, touchpadStyle);
addActor(controller);

Then in resize() I do the following:

public void resize(float width, float height) {
    setViewport(width, height, true);

    //TODO setting the bounds too small breaks the touchpad?
    controller.setBounds( width*1f/16, height*1f/9, width/7f, width/7f); 
}

The problem is that when I resize the screen (I'm running the desktop version) to a small enough width, the touchpad gets somehow 'inverted'. It seems like the background is on top and is moving when I touch the touchpad. Also the direction (percentX, percentY) it gives is the negative of what it should be.

Why does this happen and how can I prevent it?

ploosu2
  • 413
  • 6
  • 15

1 Answers1

1

If you want to have a controller that is always 1/7th of the screen why are you using a pixel perfect stage? In your resize method why not do:

    virtualStage.setViewport(MyGame.VIRTUAL_WIDTH, MyGame.VIRTUAL_HEIGHT, true);
    virtualStage.getCamera().translate(-virtualStage.getGutterWidth(),-virtualStage.getGutterHeight(), 0);
    Camera c = virtualStage.getCamera();
    c.update();

You could leave out the translate if you wanted (depends on what you display on the right side). I wonder if changing the bounds of the controller on resize is what is messing with it.

Chase
  • 3,123
  • 1
  • 30
  • 35
  • I do it this way now. It certainly works allthough the hud elements get stupidly much away from the boundary when I make the screen wide in lenght and narrow in height. But I guess it's OK for mobile screen resolutions and on desktop, who would even resize the window to that kind of resolution. So now the touchpad is working but I still wonder what was it that broke it when I resized it too small... – ploosu2 Mar 13 '14 at 11:45