2013-03-31 41 views
4

爲什麼我的TextButton,來自libgdx不響應點擊?Libgdx爲什麼我的按鈕沒有響應mouseclicks

我有一個按鈕,這個按鈕有一個監聽器,但它沒有響應。 該按鈕正在顯示,但在鼠標單擊時不響應。

public MyStage extends Stage { 
    ... 
    next.addListener(new InputListener() { 
     @Override 
     public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) 
     { 
      Gdx.app.log(ApothecaryGame.LOG, "Pressed: Next button."); 
      return true; 
     } 
     @Override 
     public void touchUp(InputEvent event, float x, float y, int pointer, int button) { 
      Gdx.app.log(ApothecaryGame.LOG, "Released: Next button."); 
      super.touchUp(event, x, y, pointer, button); 
      nextPage(); 
     } 
    }); 
    this.addActor(next); 
} 
+0

有沒有人知道爲什麼? –

回答

15

將ClickListener添加到您的按鈕。它看起來像這樣。

button.addListener(new ClickListener() { 
    public void clicked(InputEvent event, float x, float y) { 
     // Do something interesting here... 
    } 
}); 

此外,請確保您將舞臺設置爲輸入處理器,否則它將不會看到事件。

Gdx.input.setInputProcessor(stage); 
+1

確保在'public void show(){}'方法中添加'Gdx.input.setInputProcessor(stage);' – Till