Search code examples
javainputlibgdxtextures

Libgdx texture on key input too fast


I want to draw a texture when I press a key. I'm using isKeyJustPressed() method and because of this , the texture appears and dissapears really fast. How can I slow it down a bit, so I can see it appear?


Solution

  • Are you drawing the texture like this?

    public void render() {
        ...
        batch.begin();
        ...
        if(isKeyJustPressed...) {
            texture.draw()...
        }
        ...
        batch.end();
        ...
    }
    

    If so your texture will be drawn only for just one frame.

    If this is the problem:

    float timeRemaining = 0f; // in seconds
    public void render() {
        ...
        batch.begin();
        ...
        if(isKeyJustPressed...) {
            timeRemaining = 5; // will show the texture for 5 seconds
        }
        if (timeRemaining>0) {
            timeRemaining -= Gdx.graphics.getDeltaTime();
            texture.draw()...
        }
        ...
        batch.end();
        ...
    }
    

    Also I would highly suggest that you go through this tutorials before jumping into making games.
    https://docs.oracle.com/javase/tutorial/