0
\$\begingroup\$

I'm trying to create a game with libGDX in Android Studio, and I am struggling with my code to add a command to the program to do something when I press the button.

I have added an ImageButton, but now I struggle to get it to do something when I click on it.

Here's my code:

 bplay.addListener( new ClickListener(){
        public void clicked(InputEvent event, float x, float y){
            batch.begin();
            batch.draw( splash, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
            batch.end();
        }
    });
\$\endgroup\$
1
  • \$\begingroup\$ Hey, what is happening? \$\endgroup\$
    – Curio
    Commented Oct 25, 2017 at 16:02

1 Answer 1

1
\$\begingroup\$

You need to draw stuff in the rendering loop. Try doing the following:

// On the class level
boolean drawImage = false;

// When setting the listener
bplay.addListener( new ClickListener(){
    public void clicked(InputEvent event, float x, float y){
        drawImage = true;
    }
});

// In the render method
if (drawImage) {
    batch.begin();
    batch.draw( splash, 0, 0, Gdx.graphics.getWidth(), 
    Gdx.graphics.getHeight() );
    batch.end();
    drawImage = false;
}
\$\endgroup\$
8
  • \$\begingroup\$ No luck, added and no problem with the code but when i touch the imageButton it still does not do anything. \$\endgroup\$
    – Neillbar
    Commented Oct 24, 2017 at 9:48
  • \$\begingroup\$ @Neill are you sure listener gets called? Try logging something in it. \$\endgroup\$
    – Bálint
    Commented Oct 24, 2017 at 10:15
  • \$\begingroup\$ Hi, I did and still no luck. \$\endgroup\$
    – Neillbar
    Commented Oct 24, 2017 at 10:35
  • \$\begingroup\$ if(drawplay==true) { batch.begin(); batch.draw( splash, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() ); batch.end(); drawplay=true; Gdx.app.log( "Yess","yess" ); } }; \$\endgroup\$
    – Neillbar
    Commented Oct 24, 2017 at 10:36
  • \$\begingroup\$ @Neill Does that log the yess message? If it doesn't, then does it work if you put it in the listener? \$\endgroup\$
    – Bálint
    Commented Oct 24, 2017 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .