I'm working with LibGDX. How can I draw a sprite when a key is pressed? I have tried with the following, but it doesn't seem to work.
if (Gdx.input.isKeyPressed(Input.Keys.X)) {
batch.begin();
sprite.draw(batch);
batch.end();
}
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Sign up to join this communityI'm working with LibGDX. How can I draw a sprite when a key is pressed? I have tried with the following, but it doesn't seem to work.
if (Gdx.input.isKeyPressed(Input.Keys.X)) {
batch.begin();
sprite.draw(batch);
batch.end();
}
You could have a boolean that you set to true when the key is pressed in your update method:
boolean drawSprite;
if (Gdx.input.isKeyPressed(Input.Keys.X)) {
drawSprite = true;
}
And then in your render method:
batch.begin();
if(drawSprite == true) {
sprite.draw(batch);
}
batch.end();
I assume you are using Sprite
class from libgdx and the sprite you created is working fine? And the only problem is you cannot show the sprite when a key is pressed.
@Override
public void update() {
// the input is pressed once even you are still holding the button.
if(Gdx.input.isJustKeyPressed(Input.Keys.X)) {
sprite.setVisible(true);
}
batch.begin();
sprite.draw(batch);
batch.end();
}
update()
mehtod?
\$\endgroup\$
Oct 12, 2016 at 19:24