【问题标题】:Get width and height of LibGdx animation获取LibGdx动画的宽高
【发布时间】:2015-04-17 16:04:05
【问题描述】:

使用LibGdx Animation时如何获取单帧的宽高?

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

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    atlas.findRegion("path/goes/here") 方法返回一个 TextureRegion。 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

    【讨论】:

    • TextureAtlas 中没有 .getRegion.getTexture
    • 哎呀,当我提到 TextureAtlas 时,我的意思是 findRegion。但是,findRegion() 返回一个 TextureRegion,而 TextureRegion 确实getTexture。你有任何错误吗?如果你是,请发布它们。
    • 我不确定你的意思。你可以做findRegion.getTexture().getWidth(),但这会返回整个图集的宽度。似乎没有办法获得特定区域。你不能从那里getTexture()
    • 我试过了:return new Rectangle(posX, posY, atlas.findRegion("runner1").getTexture().getWidth(), atlas.findRegion("runner1").getTexture().getHeight());。我现在有一个临时修复,我在构造函数中设置了一个 textureRegion 并只使用它,只要帧大小相同就可以工作。但不漂亮
    • 或者如果你真的想要这样的话,来自 TextureRegion atlas.findRegion("runner1").getRegionWidth()getRegionWidth()getRegionHeight() 应该可以工作。因为当你得到纹理和它的宽度/高度时,它确实是整个图集维度。
    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多