Basically, I tried implementing an animation in one of my game's screens using a TextureAtlas and using .findRegions() to use for the animation object.
I tried renaming the files to their respective indices (as well as updating the .pack folder)
Sample Pack Folder:
sunglasses_1.png
format: RGBA8888
filter: Nearest,Nearest
repeat: none
tile000
rotate: false
xy: 1, 1
size: 600, 600
orig: 600, 600
offset: 0, 0
index: -1
...and so on until sunglasses_35.png
I've also read about the same issue in SAO here: Animating the sprite array created from texture atlas - however it did not help. I've also read the GitHub page including information about Animations and TextureRegions.
Here are the lines of code that were used to implement an animation
Animation setup
//animation setup with batch
batch = new SpriteBatch();
animationAtlas = new TextureAtlas(Gdx.files.internal("sunglasses.pack"));
animation = new Animation<>(0.13f, animationAtlas.findRegions("sunglasses"), Animation.PlayMode.LOOP);
Animation called in the render() function from the Screen class
@Override
public void render(float delta) {
float elapsedTime = 0f;
elapsedTime += Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
batch.begin();
batch.draw(animation.getKeyFrame(elapsedTime), 100, 100);
batch.end();
}
Error
Exception in thread "LWJGL Application" java.lang.ArithmeticException: / by zero
at com.badlogic.gdx.graphics.g2d.Animation.getKeyFrameIndex(Animation.java:138)
at com.badlogic.gdx.graphics.g2d.Animation.getKeyFrame(Animation.java:122)
at com.tantei.game.Screens.MainMenu.render(MainMenu.java:104)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.tantei.game.Game.render(Game.java:21)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Any help is appreciated!!
[EDIT] animation.getKeyFrames()
returns a size of 0 - hence the exception. I still don't get why it has no elements
Your atlas is not created correctly for an animation, so when you call animationAtlas.findRegions
, it's returning an empty array. When you call animation.getKeyFrame()
, it is dividing by zero frames of animation, hence the exception.
For animationAtlas.findRegions("sunglasses")
to work, your regions in the pack file would each need the exact same name (sunglasses), not tile000, tile001, tile003, etc. And they should each have a different index
under them, not all have an index
of -1.
Trying to create a texture atlas manually is just punishing yourself. Use TexturePacker.