【问题标题】:How to restrict touchpad knob movement along horizontal axis only如何限制触摸板旋钮仅沿水平轴移动
【发布时间】:2018-02-04 14:35:03
【问题描述】:

我希望 LibGDX 中的触摸板旋钮能够向右或向左移动,但不能向上或向下移动。这是我的代码:

Drawable touchBackground = touchpadSkin.getDrawable("touchBackground");
touchKnob = touchpadSkin.getDrawable("touchKnob");
touchpadStyle.background = touchBackground;
touchpadStyle.knob = touchKnob;
touchKnob.setMinHeight(80);
touchKnob.setMinWidth(30);

touchpad = new Touchpad(0.1f, touchpadStyle);
touchpad.setBounds(10, 100, 130, 130);
touchpad.getResetOnTouchUp();

ScrollPane scrollPane=new ScrollPane();
touchpad.setPosition(70,70);
touchpad.setOriginX(200);

stage.addActor(touchpad);
Gdx.input.setInputProcessor(stage);

【问题讨论】:

    标签: android libgdx scene2d touchpad


    【解决方案1】:

    我相信没有方便的解决方案。我只能想到一种可能的方法来实现这种行为 - 在通知其他听众之前将 InputListener 添加到 touchPad 并更正 InputEvent 协调:

    final Touchpad touchpad = ...;
    
    // insert the listener before other listeners
    // to correct InputEvent coordinates before they are notified
    touchpad.getListeners().insert(0, new InputListener() {
    
        private Vector2 tmpVec = new Vector2();
    
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            if (touchpad.isTouched()) return false;
            restrictAlongX(event);
            return true;
        }
    
        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            restrictAlongX(event);
        }
    
        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            restrictAlongX(event);
        }
    
        private void restrictAlongX(InputEvent inputEvent) {
            // convert local centerY to the stage coordinate system
            tmpVec.set(0f, touchpad.getHeight() / 2);
            touchpad.localToStageCoordinates(tmpVec);
    
            // set stageY to the touchpad centerY
            inputEvent.setStageY(tmpVec.y);
        }
    });
    

    当然这看起来不太漂亮,也许有人会提出更清洁的解决方案。您应该知道它会更改InputEvent 坐标,并且在touchpad 之后将使用相同的InputEvent 通知Actors。但我认为这在大多数情况下是可以接受的,除此之外,这应该可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多