Skip to main content

FlixelAnimationController

View source

class

org.flixelgdx.animation.FlixelAnimationController

public class FlixelAnimationController implements FlixelUpdatable

Playback state and clip registration for FlixelSprite animations. Obtain a controller with FlixelSprite.ensureAnimation() (or assign one directly), then call sprite.ensureAnimation().addSparrowAtlas(...), .playAnimation(...), etc. Decouples animation timing from rendering and physics.

Optionally link a FlixelAnimationStateMachine via FlixelAnimationController.setStateMachine(FlixelAnimationStateMachine) so the machine is ticked automatically inside FlixelAnimationController.update(float) - no separate machine update call would be needed in your game loop.

Mixing in a Sparrow atlas

A character can also carry Sparrow XML clips on the same body via FlixelAnimationController.addSparrowFrames(String), allowing a single sprite to contain multiple Sparrow sheets with unique animations.

Constructors

FlixelAnimationController(FlixelSprite)

public FlixelAnimationController(FlixelSprite owner)

Creates a new animation controller for the given sprite.

Parameters:

NameDescription
ownerThe sprite that owns this animation controller.

Fields

onFrameChanged

public final FlixelSignal<FlixelAnimationFrameSignalData> onFrameChanged

Dispatched when the visible keyframe index changes.


onAnimationFinished

public final FlixelSignal<FlixelAnimationFrameSignalData> onAnimationFinished

Dispatched once when a non-looping clip reaches its end, or when looping is turned off at end.


Methods

getOwner()

public FlixelSprite getOwner()

The sprite that owns this controller. Package use includes spritemap loading helpers.


loadSpritemapFromJson(String, String, String)

public FlixelSprite loadSpritemapFromJson(String textureKey, String spritemapJsonPath, String animationJsonPath)

Adobe/CreateJS spritemap plus animation index JSON. See FlixelSpritemapJsonLoader.load(...) for file shapes.

Parameters:

NameDescription
textureKeyAsset key of the already-enqueued FlixelGraphic.
spritemapJsonPathPath resolved like other assets (internal or classpath).
animationJsonPathJSON with an animations object.

Returns: The owning sprite for chaining.


addSparrowFrames(String)

public FlixelSprite addSparrowFrames(String path)

Loads or merges a Sparrow atlas onto the owning sprite from a single base path, inferring the conventional .png and .xml file names shared by a Sparrow export. For example, passing "shared/images/foo" loads the texture from "shared/images/foo.png" and the atlas data from "shared/images/foo.xml".

When the sprite has no atlas yet, this installs the sheet as the primary atlas, sizes the sprite to the first frame, and clears any previously registered clips. When the sprite already has an atlas (either from a prior call or from an Adobe Animate rig), this appends the new sheet's frames without disturbing the existing ones, so a single sprite can carry clips from multiple sheets.

sprite.ensureAnimation().addSparrowAtlas("shared/images/foo");
sprite.animation.addAnimationByPrefix("idle", "idle", 24, true);
sprite.animation.playAnimation("idle");

Parameters:

NameDescription
pathThe base path, without a file extension, shared by the PNG and XML pair.

Returns: The owning sprite for chaining.

See Also: .addSparrowFrames


addSparrowFrames(FileHandle)

public FlixelSprite addSparrowFrames(FileHandle path)

Overload of FlixelAnimationController.addSparrowFrames(String) that accepts the base path as a FileHandle.

Parameters:

NameDescription
pathThe base path handle, without a file extension, shared by the PNG and XML pair.

Returns: The owning sprite for chaining.


addSparrowFrames(String, String)

public FlixelSprite addSparrowFrames(String textureKey, String xmlPath)

Loads or merges a Sparrow atlas onto the owning sprite from an explicit texture key and XML path. See FlixelAnimationController.addSparrowFrames(String) for the full load/merge contract.

Parameters:

NameDescription
textureKeyThe asset key of the Sparrow PNG. Must not be null.
xmlPathThe path to the Sparrow XML. Must not be null.

Returns: The owning sprite for chaining.

Throws:

TypeDescription
IllegalArgumentExceptionIf either file is missing or malformed.

addSparrowFrames(String, FileHandle)

public FlixelSprite addSparrowFrames(String textureKey, FileHandle xmlFile)

Overload of FlixelAnimationController.addSparrowFrames(String, String) that accepts the XML as a FileHandle.

Parameters:

NameDescription
textureKeyThe asset key of the Sparrow PNG. Must not be null.
xmlFileThe Sparrow XML file, read as UTF-8. Must not be null.

Returns: The owning sprite for chaining.


addSparrowFrames(String, Element)

public FlixelSprite addSparrowFrames(String textureKey, Element xmlRoot)

Overload of FlixelAnimationController.addSparrowFrames(String, String) that accepts a pre-parsed XML root.

Parameters:

NameDescription
textureKeyThe asset key of the Sparrow PNG. Must not be null.
xmlRootThe root TextureAtlas element of a Sparrow XML. Must not be null.

Returns: The owning sprite for chaining.


parseSparrowFrames(Texture, Element)

public static Array<FlixelFrame> parseSparrowFrames(Texture texture, Element xmlRoot)

Parses Sparrow SubTexture entries into a fresh frame list without installing them on any sprite.

This is the shared parsing core used by FlixelAnimationController.addSparrowFrames(String). Keeping it separate lets callers inspect or transform the frame list before passing it to FlixelSprite.applySparrowAtlas(FlixelGraphic, Array<FlixelFrame>) or FlixelSprite.mergeSparrowAtlas(FlixelGraphic, Array<FlixelFrame>).

Parameters:

NameDescription
textureThe backing texture the regions are cut from.
xmlRootThe root TextureAtlas element of a Sparrow XML.

Returns: A newly allocated list of frames, one per valid SubTexture.


clear()

public void clear()

Clears all clips, per-animation offsets, and resets playback state.


addOffset(String, float, float)

public void addOffset(String name, float x, float y)

Registers a per-animation pixel offset, applied to the owning sprite's offset every time that clip starts.

A Sparrow atlas keeps each frame planted within its own untrimmed source box, but different clips are usually authored on differently sized canvases, so their anchors do not line up when you switch between them. Nudge each clip by a hand-tuned amount so they all share a common ground line. The offset is added to the draw position, so positive x moves the graphic right and positive y moves it up (matching FlixelSprite.setOffset(float, float)).

The feature is opt-in. Until the first addOffset call the controller never touches the sprite's offset; afterwards it owns it, and playing a clip with no registered offset resets the offset to (0, 0). Avoid mixing manual FlixelSprite.setOffset(float, float) with this API.

var anim = sprite.ensureAnimation();
anim.addAnimationByPrefix("idle", "BF idle dance", 24, true);
anim.addAnimationByPrefix("singLEFT", "BF NOTE LEFT", 24, false);
anim.addOffset("idle", 0, 0);
anim.addOffset("singLEFT", -12, 6);
anim.playAnimation("idle"); // offset snaps to (0, 0)

Parameters:

NameDescription
nameThe animation name, as registered with one of the addAnimation* methods.
xHorizontal offset in pixels (positive moves the graphic right).
yVertical offset in pixels (positive moves the graphic up).

removeOffset(String)

public void removeOffset(String name)

Removes a previously registered per-animation offset.

Parameters:

NameDescription
nameThe animation name to clear the offset for.

clearOffsets()

public void clearOffsets()

Removes every registered per-animation offset without affecting the registered clips.


hasOffset(String)

public boolean hasOffset(String name)

Parameters:

NameDescription
nameThe animation name to check.

Returns: true if a per-animation offset is registered for name.


centerOffsets()

public void centerOffsets()

Equivalent to FlixelAnimationController.centerOffsets(boolean) with adjustPosition set to false.


centerOffsets(boolean)

public void centerOffsets(boolean adjustPosition)

Centers the owning sprite's hitbox within its current frame's untrimmed source box.

This is useful when a trimmed Sparrow frame draws smaller than the hitbox set by FlixelSprite.updateHitbox(float, float) (or vice versa), since the art would otherwise sit off-center inside the box.

This is a one-shot computation from the currently displayed FlixelFrame, independent of the per-animation registry; it does not read or write FlixelAnimationController.animationOffsets. Calling it after a clip with a registered offset will overwrite that offset until the next FlixelAnimationController.playAnimation(String) call reapplies it.

Parameters:

NameDescription
adjustPositionWhether to also shift the sprite's position so its origin stays anchored to the frame's center, matching HaxeFlixel's optional position correction.

pause()

public void pause()

resume()

public void resume()

isPaused()

public boolean isPaused()

getPaused()

public boolean getPaused()

Returns whether animation playback is currently paused.


reverse()

public void reverse()

Toggles playback direction. Time still advances by FlixelAnimationController.update(float) while not paused.


getPlayDirection()

public float getPlayDirection()

setPlayDirection(int)

public void setPlayDirection(int playDirection)

addAnimationByPrefix(String, String, int, boolean)

public void addAnimationByPrefix(String name, String prefix, int frameRate, boolean loop)

Adds an animation to the controller by prefixing the frame names.

Parameters:

NameDescription
nameThe name of the animation.
prefixThe prefix of the frame names.
frameRateThe frame rate of the animation.
loopWhether the animation should loop.

addAnimationFromAtlas(String, int[], float, boolean)

public void addAnimationFromAtlas(String name, int[] atlasFrameIndices, float frameDuration, boolean loop)

Adds a clip from the current atlas list FlixelSprite.getAtlasRegions() using zero-based indices into that list. Used by spritemap JSON and by games that know frame order after a Sparrow or spritemap load.

Parameters:

NameDescription
nameClip name for FlixelAnimationController.playAnimation(String).
atlasFrameIndicesIndices into the atlas list (out-of-range entries are skipped).
frameDurationlibGDX frame duration in seconds (reciprocal of FPS).
loopWhether the clip loops.

addAnimation(String, int[], float)

public void addAnimation(String name, int[] frameIndices, float frameDuration)

Adds an animation to the controller by specifying the frame indices.

Parameters:

NameDescription
nameThe name of the animation.
frameIndicesThe indices of the frames to add.
frameDurationThe duration of each frame.

playAnimation(String)

public void playAnimation(String name)

Plays the animation with the given name. Loops and restarts the animation by default.

Parameters:

NameDescription
nameThe name of the animation to play.

playAnimation(String, boolean)

public void playAnimation(String name, boolean loop)

Plays the animation with the given name. Restarts the animation by default.

Parameters:

NameDescription
nameThe name of the animation to play.
loopWhether the animation should loop.

playAnimation(String, boolean, boolean)

public void playAnimation(String name, boolean loop, boolean forceRestart)

Plays the animation with the given name.

Parameters:

NameDescription
nameThe name of the animation to play.
loopWhether the animation should loop.
forceRestartWhether the animation should restart.

isAnimationFinished()

public boolean isAnimationFinished()

getAnimationFinished()

public boolean getAnimationFinished()

Returns whether the current animation has finished playing (only meaningful for non-looping animations).


update(float)

public void update(float elapsed)

getAnimations()

public ObjectMap<String,Animation<FlixelFrame>> getAnimations()

getStateTime()

public float getStateTime()

getCurrentAnim()

public String getCurrentAnim()

getCurrentKeyframeIndex()

public int getCurrentKeyframeIndex()

Zero-based key index in the current clip. Computed from FlixelAnimationController.getStateTime() and the controller's looping flag rather than from the underlying libGDX Animation's PlayMode, so the index always matches what FlixelAnimationController.update(float) actually displays no matter how the clip was registered.

For non-looping playback that has already finished, this returns the last keyframe index (keyframeCount - 1). For looping playback, it returns the wrapped index inside [0, keyframeCount). Useful for multi-part BTA/texture-atlas characters driven outside a single-frame texture (for example FlixelAnimateSprite).

Returns: The current keyframe index. Always >= 0.


isLooping()

public boolean isLooping()

getLooping()

public boolean getLooping()

Returns whether the current animation is set to loop.


getStateMachine()

public FlixelAnimationStateMachine getStateMachine()

Returns: The linked FlixelAnimationStateMachine, or null if none is set.


setStateMachine(FlixelAnimationStateMachine)

public void setStateMachine(FlixelAnimationStateMachine stateMachine)

Links a FlixelAnimationStateMachine to this controller so it is ticked automatically by FlixelAnimationController.update(float). Pass null to detach a previously linked machine.

This is the recommended way to drive an FSM: link it once and update only the sprite each frame. The machine still works independently if you prefer to call its own FlixelAnimationStateMachine.update(float) by hand - just do not link it here.

var fsm = new FlixelAnimationStateMachine(player);
player.ensureAnimation().setStateMachine(fsm);
// From now on player.update(elapsed) advances both the controller and the FSM.

Parameters:

NameDescription
stateMachineThe machine to auto-tick, or null to clear the link.