java - 获取LibGdx动画的宽度和高度

标签 java libgdx

使用LibGdx动画时如何获取单帧的宽度和高度?

tallAnim = new Animation(1/10f, atlas.findRegion("runner1"), atlas.findRegion("runner2"), atlas.findRegion("runner3"), atlas.findRegion("runner4"));
shortAnim = new Animation(1/10f, atlas.findRegion("short_runner1"), atlas.findRegion("short_runner2"), atlas.findRegion("short_runner3"), atlas.findRegion("short_runner4"));
animation = tallAnim;

我在这两个动画之间切换,当我检查碰撞时,我需要知道当前动画帧的确切宽度/高度。

我通常会如何处理这个问题:

public Rectangle getBounds(){
return new Rectangle(positionX, positionY, sprite.getWidth(), sprite.getHeight());
}

最佳答案

方法atlas.findRegion("path/goes/here")返回一个纹理区域。一个TextureRegion有一个region.getTexture()方法。纹理有 tex.getWidth()和一个 tex.getHeight()方法。假设这些框架具有相同的宽度,您应该可以直接说出类似 atlas.findRegion("path/goes/here").getTexture().getWidth() 的内容。和atlas.findRegion("path/goes/here").getTexture().getHeight() 。否则,您将需要某种方法来跟踪您所在的帧并根据当前帧获取宽度和高度。

我猜你的方法现在应该类似于:

public Rectangle getBounds() {
    return new Rectangle(posX, posY, atlas.findRegion("path/goes/here").getTexture().getWidth(), atlas.findRegion("path/goes/here").getTexture().getHeight())
}

我将提供每个类的一些链接,以便您可以自己查看并了解它们还有哪些其他方法。

TextureAtlas 文档:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureAtlas.html

TextureRegion 文档:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureRegion.html

纹理文档:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html

关于java - 获取LibGdx动画的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29704193/

相关文章:

java - beanshell 新手-运行简单脚本时出错

Java Regex 在大行上抛出 StackOverflow

java - 循环不能与 float 一起使用?

java - Libgdx spine2d 骨骼动画

java - libgdx的gradlew在Fedora中不起作用

java - 如何在Java中对SQL查询处理的数据进行处理和分组?

Java Servlet/Jsp/CanvasJs 从 Servlet 获取数据到 Jsp

java - 测试的 Gradle 依赖项与编译的不同

java - 从二维数组中获取一行

java - Vector 在另外两个之间的计算位置