Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

close a dialog on background image touch libgdx

I'm trying to close a dialog on background touch but it always goes in the else condition

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;
                    }

                });
like image 342
Lynob Avatar asked Nov 07 '25 06:11

Lynob


1 Answers

I think this will work, but didn't test.

Dialog is already set up to receive all touchDown input while it's visible, even if the touch is outside its bounds, so simply give it a listener that hides it if the touch is outside its bounds:

    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;
        }
    });

This assumes dialog is final or a member field so you can access it from the listener.

I think the reason your code doesn't work is that stage.hit(...) will always return the dialog regardless of coordinates since Dialogs are set up to absorb all input.

like image 95
Tenfour04 Avatar answered Nov 09 '25 04:11

Tenfour04



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!