2017-03-22 32 views
1

我試圖關閉後臺觸摸一個對話框,但它總是去在其他條件的背景圖像觸摸關閉對話框libgdx

stage.addListener(new InputListener(){ 
        @Override 
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
         if(stage.hit(x,y,true).equals(bg)) { 
          System.out.println("in th if"); 
          dialog.addAction(rotateTo(90, .30f, Interpolation.smooth2)); 
          dialog.hide(); 
         } 
         else { 
          System.out.println("int the else"); 
         } 
         return true; 
        } 

       }); 
+0

「stage」是什麼對象? – azizbekian

+0

爲什麼不只是在背景上使用輸入監聽器? –

+0

@azizbekian我可能誤解了你的問題先生,但它是這個https://github.com/libgdx/libgdx/wiki/Scene2d – Lynob

回答

1

我認爲這會工作,但沒有測試。

對話框已被設置爲接收所有着陸輸入,而它的可見,即使觸摸是它的邊界之外,所以乾脆把它隱藏它的監聽器如果觸摸是它的邊界之外:

dialog.addListener(new InputListener() { 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      if (x < 0 || x > dialog.getWidth() || y < 0 || y > dialog.getHeight()){ 
       dialog.hide(); 
      } 
      return true; 
     } 
    }); 

這個假設dialog是最終的或成員字段,因此您可以從偵聽器訪問它。

我認爲你的代碼不起作用的原因是stage.hit(...)將永遠返回dialog無論座標,因爲對話框設置爲吸收所有輸入。