Game Development Reference
In-Depth Information
LibGDX provides a class called Animation that helps us define image sequences
and also helps us to pick the right (key) frame from a sequence at a specific time
which, for example, depends on the desired frame rate for playback. There is some
clever mathematics and state-keeping involved for finding the right frame in an
efficient way. Luckily, the Animation class saves us from having to go down that
mathematical road and lets us just do animations the easy way with a variety of
extra playback options.
Packing animations using TexturePacker
Before we look any further into the Animation class, let's first cover a great feature
of LibGDX's TexturePacker that should be used when working with animations.
Whenever TexturePacker builds a new texture atlas, it always scans the end of each
image filename for an underscore followed by a number like waterfall_03.png . In
this case, the number 03 is considered as the frame index of this animation, while the
animation itself will be named and referred to as waterfall in the texture atlas.
Let's imagine that we have a five-frame animation called waterfall . So, according
to the mentioned pattern for allowing animation frames to be recognized by
TexturePacker, we would name our image files as follows:
waterfall_01.png
waterfall_02.png
waterfall_03.png
waterfall_04.png
waterfall_05.png
The following short code example further illustrates the line of action to
build animations:
TextureAtlas atlas = assetManager.get("atlas.pack");
AtlasRegion firstFrame = atlas.findRegion("waterfall");
AtlasRegion thirdFrame = atlas.findRegion("waterfall", 3);
Array<AtlasRegion> allFrames = atlas.findRegions("waterfall");
 
Search WWH ::




Custom Search