有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java动画仅在按下键时显示,图像在未按下键时消失

当没有按下任何键时,屏幕是空白的,我希望动画在动画的最后一个图像处停止,但当没有按下任何键时,它会消失。这是我的渲染方法

public void render () {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    time += Gdx.graphics.getDeltaTime();
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
        batch.draw(right.getKeyFrame(time, true), 100, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT))batch.draw(left.getKeyFrame(time,true),100,0);
    batch.end();
}

共 (1) 个答案

  1. # 1 楼答案

    问题是,当右键或左键都没有被按下时,您没有调用batch.draw(...)

    你需要这样的东西:

    if ( Gdx.input.isKeyPressed(Input.Keys.RIGHT) )
    {
        batch.draw(right.getKeyFrame(time, true), 100, 0);
    }
    else if ( Gdx.input.isKeyPressed(Input.Keys.LEFT) )
    {
        batch.draw(left.getKeyFrame(time, true), 100, 0);
    }
    else
    {
        batch.draw(middle.getKeyFrame(time, true), 100, 0);
    }
    

    您需要将middle对象替换为不按任何键时在屏幕上看到的对象