2014-04-15 24 views
0

我在Android上使用libGDX。我設置舞臺並添加了自定義繪畫的自定義動作。它工作正常。但是,我無法通過此演員的InputListener爲touchDragged方法獲取任何日誌。即使它內部的代碼也不會運行。我在libGDX中錯過了在Actor上獲取InputEvent的內容?

這裏是需要暴露在代碼:

public class CustomActor extends Actor { 
    public CustomActor() { 
     this.setListener(new InputListener() { 
      @Override 
      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
       return true; 
      } 
      @Override 
      public void touchDragged(InputEvent event, float x, float y, int pointer) { 
       //This log doesn't print up! 
       Gdx.app.log("CustomActor","touchDragged"); 
       //This code doesn't work either 
       Vector2 v = CustomActor.this.localToParentCoordinates(new Vector2(x,y)); 
       CustomActor.this.setPosition(v.x, v.y); 
      } 
     }); 
    } 
} 

誰能幫助我在這裏對我缺少什麼?

回答

1

添加您使用的Stage作爲InputProcessor接收事件,否則Stage不能將事件轉發給演員。

Gdx.input.setInputProcessor(stage); 

應該這樣做。

+0

感謝BennX,我已經在create()方法中完成了這個工作。在舞臺上添加的小部件會在連接的InputListeners中獲取日誌,但不會自定義擴展的演員。小部件已經從演員派生出來,我只是感到驚訝。你現在可以提供任何幫助嗎? – CodenameLambda1

0
  1. 嘗試使用
    public boolean addListener (EventListener listener) {...}
    方法的Actor。你使用的是哪個版本的libgdx?我認爲演員沒有「setListener」方法。
  2. 檢查你的演員邊界。 (寬度&高度)
+0

Actor有addListener()方法。我正在使用最新的每晚構建和最新的夜間文檔。對於寬度和高度使用actor.setSize(300,300)。此外,正如我在我的描述中所說的,我可以對演員進行自定義繪製,並且可以在舞臺上正確顯示。它只是不會在其附加的偵聽器中進入touchDragged方法。你能幫我嗎 ? – CodenameLambda1