【问题标题】:Animation appears only when key is pressed, image disappears when no key is pressed动画仅在按键时出现,图像在未按键时消失
【发布时间】:2016-11-15 13:20:03
【问题描述】:

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

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

【问题讨论】:

    标签: java android android-studio libgdx desktop-application


    【解决方案1】:

    问题是您没有在按下 RIGHT 或 LEFT 时调用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 对象替换为您希望在未按下任何键时在屏幕上看到的对象。

    【讨论】:

    • 哇,这很完美。我尝试了简单的 if else 但从未想过或 if elseif else 结构。非常感谢..
    • 很高兴它有效 - 您能否将答案标记为正确? ;)
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2017-05-13
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多