API AnimatedSprite - shmellyorc/Box GitHub Wiki
AnimatedSprite
Namespace: Box.Entities.Container
Description
Represents an animated sprite entity that manages multiple named animations (frame sequences) and handles playback control, looping, and frame updates via coroutines. Supports customizable color tint, alignment, and surface effects, and emits signals on animation start and finish.
Constructor
public AnimatedSprite();
Initializes default properties: white tint, top-left alignment, no loaded animations until Add
is called.
Properties
Property | Description |
---|---|
BoxColor Color |
Tint color applied when rendering the sprite. |
VAlign VAlign |
Vertical alignment of each frame within the sprite’s bounding box. |
HAlign HAlign |
Horizontal alignment of each frame within the sprite’s bounding box. |
SurfaceEffects Effect |
Rendering effects (e.g., flip, rotate) applied when drawing. |
AnimationState State |
Current playback state (Playing , Stopped ). |
bool IsPlaying |
True if an animation is currently playing. |
bool IsStopped |
True if no animation is playing or the current animation has finished. |
new Vect2 Size |
Size of the sprite’s draw area; assigning a new value marks the sprite and parent entities dirty for layout updates. |
Methods
Method Signature | Description | Returns |
---|---|---|
AnimatedSprite Add(string name, Surface surface, Vect2 cellSize, int[] frames, float speed, bool looped) |
Adds an animation defined by an array of frame indices. | AnimatedSprite |
AnimatedSprite Add(Enum name, Surface surface, Vect2 cellSize, int[] frames, float speed, bool looped) |
Adds an animation using an enum as the name. | AnimatedSprite |
AnimatedSprite Add(string name, Surface surface, Vect2 cellSize, int start, int end, float speed, bool looped) |
Adds an animation defined by a range of frame indices. | AnimatedSprite |
AnimatedSprite Add(Enum name, Surface surface, Vect2 cellSize, int start, int end, float speed, bool looped) |
Adds an animation range using enum as name. | AnimatedSprite |
AnimatedSprite Add(string name, Surface surface, Vect2 cellSize, int frame, float speed, bool looped) |
Adds a single-frame animation. | AnimatedSprite |
AnimatedSprite Add(Enum name, Surface surface, Vect2 cellSize, int frame, float speed, bool looped) |
Adds a single-frame animation using enum name. | AnimatedSprite |
bool Remove(string name) |
Removes the named animation and stops any running coroutine. | bool |
bool Remove(Enum name) |
Removes the named animation using enum. | bool |
bool Exists(string name) |
Checks if the sprite contains an animation by name. | bool |
bool Exists(Enum name) |
Checks for an animation using enum name. | bool |
Animation Get(string name) |
Retrieves the animation object by name, or default if not found. |
Animation |
Animation Get(Enum name) |
Retrieves the animation by enum name, or default . |
Animation |
bool TryGet(string name, out Animation animation) |
Attempts to get an animation by name; returns true if found. | bool |
bool TryGet(Enum name, out Animation animation) |
Attempts to get an animation by enum name; returns true if found. | bool |
AnimatedSprite Play(string name, bool reset = true) |
Plays the specified animation, optionally resetting to the first frame. | AnimatedSprite |
AnimatedSprite Play(Enum name, bool reset = true) |
Plays the specified enum-named animation. | AnimatedSprite |
Examples
// Create and configure an AnimatedSprite
var animated = new AnimatedSprite
{
Size = new Vect2(64, 64),
Color = Color.AllShades.White
};
animated
.Add("Run", runSurface, new Vect2(32, 32), new[]{0,1,2,3}, 12f, true)
.Add("Jump", jumpSurface, new Vect2(32, 32), new[]{4}, 8f, false);
// Start playback
animated.Play("Run");
// Add to the game screen
mainScreen.AddEntity(animated);