首页 > 解决方案 > 如何在 LibGDX 中为动画设置过滤器?

问题描述

我为动画制作了一个带有小精灵(32x32)的精灵表。我遇到了动画看起来非常模糊的问题。有人告诉我尝试添加我不太清楚如何为动画添加过滤器,所以如果有人可以帮助我,将不胜感激,谢谢。 .setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

标签: javalibgdx

解决方案


您不能TextureFilter直接将 a 设置为动画,因为 anAnimation不知道什么是动画。但是由于动画由Textures 组成,因此您可以将 a 设置为动画内TextureFilterTextures。

这如何完成取决于您如何创建动画。如果您加载纹理并创建它们的动画,您可以这样做:

Texture texture = new Texture(Gdx.files.internal("path/to/your/image.png"));
texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

// load the other textures of the animation in the same way
Texture[] allTextures = loadOtherTexturesWithFilter();

// create the animation
final float frameDuration = 0.1f;
Animation<Texture> animation = new Animation(frameDuration, allTextures);

或者,如果您Texture以不同的方式加载 s,您可以从 s 中获取TexturesAnimation并更改它们的过滤器:

//here the generic type of the Animation needs to be Texture or some subclass of texture (like TextureRegion, ...)
Animation<Texture> animation = yourAnimation;
for (Texture texture : animation.getKeyFrames()) {
  texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}

推荐阅读