【问题标题】:libGDX Stage input handlinglibGDX Stage 输入处理
【发布时间】:2015-09-03 07:53:11
【问题描述】:

我有一个处理触摸输入的Stage 类。

Screen 类中,我将stage 设置为InputProcessor

stageTest = new StageTest(new ScreenViewport());
Gdx.input.setInputProcessor(stageHUD);

但现在我想向 Box2d 对象添加一个力,总是会发生手势输入。

public class ActSwipe extends Actor {

    private int tmpPointer;
    private float
            tmpX,
            tmpY,
            deltaX,
            deltaY,
            rad;
    protected float
            forceX,
            forceY;


    public ActSwipe() {
        this.setName("SwipeAction");
        this.setTouchable(Touchable.enabled);
        this.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                if(tmpPointer == 0) {
                    tmpPointer = pointer;
                    tmpX = x;
                    tmpY = y;
                }
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                if (tmpPointer == pointer) {
                    tmpPointer = 0;
                    deltaX = x - tmpX;
                    deltaY = y - tmpY;
                    rad = (float) Math.atan2(deltaY, deltaX);
                    forceX = (float) Math.cos(rad);
                    forceY = (float) Math.sin(rad);
                }
            }
        });
    }

}

【问题讨论】:

  • 看看这个课程:com.badlogic.gdx.input.GestureDetector 和这篇 wiki 文章:github.com/libgdx/libgdx/wiki/Gesture-detection
  • 抱歉,不是在寻找手势监听器。我想在 Gdx.input.InputProcessor(new InputProcessor() {override methods}) 之类的其他类(屏幕)中覆盖 Stage 输入方法。

标签: java libgdx box2d scene2d


【解决方案1】:

您可以在屏幕中实现InputProcessor(或扩展InputAdapter)并用您的代码覆盖其方法。

然后像这样使用InputMultiplexer

InputMultiplexer multiplexer = new InputMultiplexer();
Gdx.input.setInputProcessor(multiplexer);
multiplexer.addProcessor(this);
multiplexer.addProcessor(stage)

在被覆盖的方法中,您需要确保被击中的对象是应该由输入处理器处理的对象,而不是场景中的对象。如果是,则从方法中返回 true,这样事件就不会传递给后者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多