【问题标题】:Sprite Animation Pictures are OverlappingSprite 动画图片重叠
【发布时间】:2019-01-12 16:09:15
【问题描述】:

我正在尝试使用具有各种动作状态的 png 文件制作动画。我的问题是,图片在渲染时相互重叠。有没有解决方案,我只能显示一张图片? 我使用 LibGDX 库。

@Override public void show()
{
    batch = new SpriteBatch();
    img = new Texture("core/assets/ghosty.png");

    regions = TextureRegion.split(img, 32, 32);
    sprite = new Sprite (regions[0][0]);
    Timer.schedule(new Timer.Task(){

        @Override
        public void run(){
            frame++;
            if (frame>27){
                frame = 0;
                if (zeile ==1){
                    zeile = 0;
                }
                else
                {
                    zeile = 1;
                }
            }
            sprite.setRegion(regions[zeile][frame]);
        }

    }, 0, 1/20f);

}

@Override public void render(float delta)
{
    //stage.draw();

    batch.begin();

    sprite.draw(batch);

    batch.end();
}

【问题讨论】:

  • 不确定你所说的重叠是什么意思,但我建议重写它。您的计时器在您在不同线程中使用的变量的单独线程设置区域中运行,不要这样做。只需在另一个 render 调用中更新并绘制下一帧。

标签: java intellij-idea libgdx sprite


【解决方案1】:

您不需要额外的 Timer 任务和 Sprite,而是需要 Animation<>

这是一个如何渲染动画的小例子:

private SpriteBatch batch;
private Texture img;
private Animation<TextureRegion> animation;
private TextureRegion[][] regions;
private Array<TextureRegion> frames;

@Override
public void show() {
    batch = new SpriteBatch();
    img = new Texture(Gdx.files.internal("ghosty.png")); //Get Texture from asset folder
    regions = TextureRegion.split(img, 32, 32);
    frames = new Array<TextureRegion>();
    int rows = 5, columns = 5; //How many rows and columns the region have

    //Fill Frames array with the regions of Texture
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            frames.add(regions[i][j]);
        }
    }

    //Create Animation. 0.1f is the time how long a frame will occur,
    //is the animation to fast set this number to a higher value
    //so the single frames will stay for a longer time
    animation = new  Animation<TextureRegion>(0.1f, frames, Animation.PlayMode.LOOP);
}

private float stateTime = 0;

@Override
public void render(float delta) {
    //Clear the screen
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //update state time so animation will go further
    stateTime += delta;

    batch.begin();
    //Draw the current frame
    batch.draw(animation.getKeyFrame(stateTime), 50, 50);
    batch.end();
}

希望这会对你有所帮助。

运行动画的更有效和更简单的方法是使用TextureAtlas 而不是Texture。下面是一个使用 TextureAtlas 的例子:Libgdx Animation not working

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 1970-01-01
    • 2013-02-05
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多