2014-10-09 98 views
0

我正在製作一個LibGdx遊戲。我有一個主菜單和一個按鈕,用於將用戶發送到播放屏幕。在玩遊戲畫面時,遊戲反應與用戶點擊按鈕位於前一屏幕中的位置時的反應。下面是定義按鈕切換屏幕代碼:LibGdx新屏幕反應從舊屏幕觸摸

button = new TextButton("Play", button_text); 
button.setWidth(Gdx.graphics.getWidth()/4); 
button.setHeight(Gdx.graphics.getHeight()/8); 
button.setPosition((Gdx.graphics.getWidth()/2) - (button.getWidth()/2), (Gdx.graphics.getHeight()/2) - (button.getHeight()/2)); 
stage.addActor(button); 
Gdx.input.setInputProcessor(stage); 
button.addListener(new InputListener(){ 
    @Override 
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){ 
     game.setScreen(new PlayScreen(game)); 
     dispose(); 
     return true; 
    } 
}); 

下面是該屏幕的Dispose方法:

@Override 
public void dispose() { 
    stage.dispose(); 
} 

那麼,如何從一個屏幕移動到下一個前清除所有輸入?

+0

你可以發佈PlayScreen類及其偵聽器方法嗎?可能是誤解觸摸事件的一個問題。 (爲了快速解決問題,我認爲你有,可以將touchDown方法更改爲touchUp以上?) – 2014-10-10 06:28:22

+0

我找到了解決方案,並將其發佈爲答案 – 2014-10-13 22:55:29

回答

0

經過進一步的研究,我發現了一個解決我的問題。

要解決此問題,在新屏幕的構造函數中,將輸入處理器設置爲GestureListener接口實現的新實例。

private MyGestureListener myGestureListener; 

public PlayScreen(Game game) { 
    this.game = game; 
    myGestureListener = new MyGestureListener(); 
    Gdx.input.setInputProcessor(new GestureDetector(myGestureListener)); 
} 

給每個屏幕自己的GestureListener實例將避免輸入從一個屏幕進行到下一個屏幕的問題。

1

添加對Dispose方法

Gdx.input.setInputProcessor(null); 

,將固定

+0

這並沒有解決問題 – 2014-10-09 23:45:23

+0

請不要將問題解決只是發佈「試試這個」風格的答案。相反,試着解釋你爲什麼要提出你的建議。 – Gary 2014-10-10 00:37:36

+0

我用電話回覆對不起。切換屏幕時,通常要做的事情就是這樣。你總是可以把它放在hide()上;或在新屏幕上設置輸入。 – Ismaw34 2014-10-10 18:01:48