Reputation: 148
How can I get the width and height of a single frame when using 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;
I switch between these two animations and when I check for collision I need to know if the exact width/height of the current animation frame.
How I'd normally handle this:
public Rectangle getBounds(){
return new Rectangle(positionX, positionY, sprite.getWidth(), sprite.getHeight());
}
Upvotes: 1
Views: 1353
Reputation: 710
The method atlas.findRegion("path/goes/here")
returns a TextureRegion. A TextureRegion has a region.getTexture()
method. A Texture has a tex.getWidth()
and a tex.getHeight()
method. Assuming these frames are the same width, you should be able to just say something like atlas.findRegion("path/goes/here").getTexture().getWidth()
and atlas.findRegion("path/goes/here").getTexture().getHeight()
. Otherwise you'll need some way to keep track of what frame your on and get the width and height depending on the current frame.
Your method, I'm guessing, should now look something like:
public Rectangle getBounds() {
return new Rectangle(posX, posY, atlas.findRegion("path/goes/here").getTexture().getWidth(), atlas.findRegion("path/goes/here").getTexture().getHeight())
}
I'll provide some links to each of the classes, so you can look through yourself and see what other methods they have.
TextureAtlas documentation: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureAtlas.html
TextureRegion documentation: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/TextureRegion.html
Texture documentation: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Texture.html
Upvotes: 1