2017-03-22 61 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无论坐标,因为对话框设置为吸收所有输入。