2012-07-07 64 views
13

我正在開發一個使用LibGDX的Android遊戲。菜單屏幕上有4個按鈕,但這些按鈕的ClickListener不起作用。按鈕ClickListener不能在LibGDX遊戲中工作

// retrieve the custom skin for our 2D widgets 
Skin skin = super.getSkin(); 

// create the table actor and add it to the stage 
table = new Table(skin); 
table.width = stage.width(); 
table.height = stage.height(); 
stage.addActor(table); 

// retrieve the table's layout 
TableLayout layout = table.getTableLayout(); 

// register the button "start game" 
TextButton startGameButton = new TextButton("Start game", skin); 
startGameButton.addListener(new ClickListener() { 
    @Override 
    public void clicked(InputEvent event, float x, float y) { 
     System.out.println("hiii"); 
     Assets.load(); 
     // game.getSoundManager().play(TyrianSound.CLICK); 
     game.setScreen(new GameScreen(game)); 
    } 
}); 

layout.register("startGameButton", startGameButton); 

如何激活LibGDX中的按鈕ClickListener

回答

45

您必須將按鈕添加到舞臺,並調用

Gdx.input.setInputProcessor(stage); 
13

而不是「點擊方法」,現在它的「點擊方法」(我想!),以防萬一有人面臨着同樣的問題,我是當發現這個問題時面對:

startGameButton.addListener(new ClickListener() {    
    @Override 
    public void clicked(InputEvent event, float x, float y) { 
     game.setScreen(new GameScreen(game)); 
    }; 
}); 
+0

它現在單擊作爲方法名稱,用小寫「c」開始。 – 2013-05-14 01:42:09

+1

此外,現在它是'startGameButton.addListener' – 2014-10-29 19:02:59