获取LibGdx动画的宽度和高度

时间:2015-04-17 16:04:05

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

1 个答案:

答案 0 :(得分: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

相关问题