我正在使用 Java 和 Libgdx scene2d 制作游戏。这是我在构造函数中的代码:
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);
然后在 resize() 中我执行以下操作:
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);
}
问题是,当我将屏幕大小(我正在运行桌面版本)调整到足够小的宽度时,触摸板会以某种方式“反转”。当我触摸触摸板时,背景似乎在顶部并且正在移动。而且它给出的方向(percentX,percentY)是它应该的方向的负数。
为什么会发生这种情况以及如何预防?
最佳答案
如果您希望 Controller 始终占屏幕的 1/7,为什么要使用像素完美的舞台?在你的调整大小方法中为什么不这样做:
virtualStage.setViewport(MyGame.VIRTUAL_WIDTH, MyGame.VIRTUAL_HEIGHT, true);
virtualStage.getCamera().translate(-virtualStage.getGutterWidth(),-virtualStage.getGutterHeight(), 0);
Camera c = virtualStage.getCamera();
c.update();
如果您愿意,您可以省略翻译(取决于右侧显示的内容)。我想知道在调整大小时更改 Controller 的边界是否会造成困惑。
关于java - 当设置边界太小时,Libgdx scene2d 触摸板获取 'inverted',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22362541/