--- name: bevy-2d-animation description: "Bevy ECS skill for 2D sprite animation. Keywords: Bevy, sprite, animation, AnimationPlayer, AnimationClip, 2D." --- # Bevy 2D animation ## When to use Load this when a 2D actor needs clip playback on a sprite entity. Pair with bevy-animation for the player and clip types. Pair with bevy-animation-graph when clips must blend. ## ECS shape - **Components:** `Sprite` (bevy::sprite) on the visible entity. `AnimationPlayer` and `AnimationTargetId` / `AnimatedBy` (bevy::animation) link a clip to that entity. - **Resources:** clip assets are `AnimationClip` handles, not a global player. - **Systems:** `advance_animations` and `animate_targets` are the engine systems named on the animation page. Gameplay code should not reimplement them. - **Schedule:** those systems are engine-owned. Gameplay that reacts to a finished clip chains after them. ## Playback choices | Need | Bevy idiom | | --- | --- | | Frame cells | `Sprite` with `texture_atlas: Some(TextureAtlas { .. })`, or an `AnimationClip` curve. | | Named cutout limb | `AnimationTargetId::from_name` / `from_names` selects which entity a curve writes. | | Loop | `ActiveAnimation::repeat` sets `RepeatAnimation::Forever`. | ## Scope - Frame sheets use `Sprite::from_atlas_image` and `TextureAtlas`. Clip curves use `animated_field!` plus `AnimatableCurve`. - A cutout limb is an entity with `AnimationTargetId` and `AnimatedBy` pointing at the entity that owns `AnimationPlayer`. ## Examples Atlas frame on a sprite. `TextureAtlas.index` is the cell. ```rust fn spawn_sheet( mut commands: Commands, assets: Res, mut layouts: ResMut>, ) { let layout = TextureAtlasLayout::from_grid(UVec2::splat(32), 4, 1, None, None); let layout = layouts.add(layout); let image = assets.load("actor.png"); commands.spawn(( Sprite::from_atlas_image(image, TextureAtlas { layout, index: 0 }), FrameClock { timer: Timer::from_seconds(0.12, TimerMode::Repeating), frames: 4, }, )); } #[derive(Component)] struct FrameClock { timer: Timer, frames: usize, } fn tick_frames(time: Res