簡體   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