Libgdx相机控制器限制区域



我正在使用一些输入控制器。一个用来控制相机,一个用来控制放置在场景中的按钮。

我一直在努力找出如何创建我自己的自定义控制器,但到目前为止,一直没有找到任何教程如何做到这一点。

如果触摸或手势低于一定的高度,我也想停用相机控制器。

非常感谢您的帮助

创建一个类CamController implements InputProcessor(或extends InputAdapter)。
然后覆盖您需要的所有方法(我在这里使用touchDown作为示例),并执行以下操作(伪代码!!不要复制粘贴!!):

protected boolean touchDown(int screenX, int screenY, int pointer, int button) {
    boolean handled = false;
    if (screenX <= maxX && screenX >= minX && screenY <= maxY && screenY >= minY) {
         // The touch is inside the limits of the camera controller, the controller is activated
         // Move camera to the touchpoint:
         camera.position.set(screenX, screenY);
         camra.update();
         handled = true;
    }
    return handled;
}

为按钮创建控制器,控制器再次实现InputProcessor并覆盖方法。
接下来在ApplicationListenerScreencreate()show()方法中创建InputMultiplexer:

InputMultiplexer m = new InputMultiplexer(new CamController(camera, limitX, limitY), new ButtonController);

您只需要将InputMultiplexer设置为活动InputProcessor
使用CamController作为第一个是很重要的,因为它然后为此调用touchDown -method,只有当touchDown返回false时,它才为ButtonController调用touchDown

希望能有所帮助。

最新更新