2015-06-17 93 views
2

我有一個處理觸摸輸入的類StagelibGDX舞臺輸入處理

Screen I類設置stageInputProcessor

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); 
       } 
      } 
     }); 
    } 

} 
+0

看看這個類:'com.badlogic.gdx.input.GestureDetector'和這個wiki文章:https://github.com/libgdx/libgdx/wiki/Gesture-detection – donfuxx

+0

對不起沒查找手勢聽衆。我希望重寫像Gdx.input.InputProcessor(新的InputProcessor(){覆蓋方法})的其他類(屏幕)中的階段輸入方法。 –

回答

0

您可以實現InputProcessor(或擴展InputAdapter)在你的屏幕,並覆蓋它的方法與您的代碼。

然後使用InputMultiplexer這樣的:

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

在你需要確保的是被打的對象是應該由你的輸入處理器處理的對象,而不是從現場的一個重寫的方法。如果是這樣,從方法返回true,所以事件不會傳遞給後者。