繁体   English   中英

libGDX阶段输入处理

[英]libGDX Stage input handling

我有一个处理触摸输入的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);
                }
            }
        });
    }

}

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

然后像这样使用InputMultiplexer

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM