gpt4 book ai didi

java - 使动画居中 [libGDX]

转载 作者:行者123 更新时间:2023-12-01 11:06:38 25 4
gpt4 key购买 nike

我有以下类(class):

public class AnimationDemo implements ApplicationListener {
private SpriteBatch batch;
private TextureAtlas textureAtlas;
private Animation animation;
private float elapsedTime = 0;
private OrthographicCamera camera;
private int width;
private int height;
private int texturewidth;
private int textureheight;

@Override
public void create() {
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
camera = new OrthographicCamera(width, height);
camera.position.set(width / 2, height / 2, 0);
camera.update();


batch = new SpriteBatch();
textureAtlas = new TextureAtlas(Gdx.files.internal("data/packone.atlas"));


textureAtlas.getRegions().sort(new Comparator<TextureAtlas.AtlasRegion>() {
@Override
public int compare(TextureAtlas.AtlasRegion o1, TextureAtlas.AtlasRegion o2) {
return Integer.parseInt(o1.name) > Integer.parseInt(o2.name) ? 1 : -1;
}
});

animation = new Animation(1 / 15f, textureAtlas.getRegions());

}

@Override
public void dispose() {
batch.dispose();
textureAtlas.dispose();
}

@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

batch.begin();
elapsedTime += Gdx.graphics.getDeltaTime();
batch.draw(animation.getKeyFrame(elapsedTime, true), 0, 0);
batch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
}

在上面我使用动画类来简单地从纹理图集中进行绘制。我正在遵循另一个SO问题的例子,即here但坐标不符合我的方程。我应该如何设置这些:

private int texturewidth;
private int textureheight;

任何帮助都会很棒:)

最佳答案

您需要注意适当的偏移 - 原点始终位于左下角,这就是为什么您在绘制时需要减去宽度和高度的一半。

简而言之,它应该是这样的:

    TextureRegion region = animation.getKeyFrame(elapsedTime, true);
batch.draw(region, 0 - (region.getRegionWidth()/2f), 0 - (region.getRegionHeight()/2f));

关于java - 使动画居中 [libGDX],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com