I have a texture pack made of 9 images. Out of the 9 images, I am using 3 of them as animation frames. So my animation is made of 3 images. The images have a fixed resolution of 128x128.
TextureRegion sprite1 = atlas.findRegion("sprite1");
TextureRegion sprite2 = atlas.findRegion("sprite2");
TextureRegion sprite3 = atlas.findRegion("sprite3");
...
...
Animation spriteAnimation = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);
// similarly I am creating 4 more animation instance using same 3 regions
Animation spriteAnimation2 = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);
...
...
Game loop :-
{
update();
render();
}
I have following code in update()
and render()
.
update()
{
...
// because images are of fixed resolution 128x128
// I will calculate the scale factor as per the device screen
// so for each device width and height will vary..
// GameUnits is giving scaled width and height need for device
x = get new x;
y = get new y;
width = GameUnits.getWidth();
height = GameUnits.getHeight();
// similarly updating 4 more width & height, each of them are different.
...
...
}
render()
{
...
spriteBatch.begin();
spriteBatch.draw(spriteAnimation.getKeyFrame(totalTime),x,y,width1,height1);
// similarly rendering total of 5 animation
spriteBatch.draw(spriteAnimation2.getKeyFrame(totalTime),x,y,width2,height2);
...
spriteBatch.end();
...
}
Now, update()
method is taking 0.045ms on average, whereas render()
method is taking 16ms on average, giving me FPS of ~60.
At some instant render()
method takes 22ms or high or low and hence creating lags. Render method has same work load every frame, i.e to render 5 animation.
I am new to libgdx without any prior experience in gaming.
May I know what mistake I made in implementing Animations?
The update()
code is working well for me, but it looks like there are serious holes in render()
.