有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java libGDX阶段输入处理

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

}

共 (1) 个答案

  1. # 1 楼答案

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

    然后像这样使用InputMultiplexer

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

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