class Eagle::AnimatedSprite2D

Overview

A sprite that plays named frame animations such as "idle", "run" and "jump".

Add animations from texture regions (Texture#frames cuts a sheet into them), then call #play whenever the state changes. Calling #play with the animation that is already running does nothing, so it's safe to call every frame.

sheet = Texture.new(Image.checkerboard(256, 64, 32))
hero = AnimatedSprite2D.new
hero.add_animation("idle", sheet.frames(32, 32)[0..1], fps: 4)
hero.add_animation("run", sheet.frames(32, 32)[2..7], fps: 12)
hero.add_animation("die", sheet.frames(32, 32)[8..11], fps: 8, loop: false)
hero.on_animation_finished { |name| hero.queue_free if name == "die" }

moving = true
hero.play(moving ? "run" : "idle")

Defined in:

eagle/nodes/animated_sprite2d.cr

Instance Method Summary

Instance methods inherited from class Eagle::Sprite2D

centered=(centered : Bool) centered=, centered? : Bool centered?, color : Color color, color=(color : Color) color=, contains_point?(global : Vec2) : Bool contains_point?, current_region : Drawable | Nil current_region, draw(g : Graphics) : Nil draw, flip_h=(flip_h : Bool) flip_h=, flip_h? : Bool flip_h?, flip_v=(flip_v : Bool) flip_v=, flip_v? : Bool flip_v?, frame : Int32 frame, frame=(frame : Int32) frame=, global_rect : Rect global_rect, height : Float32 height, hframes : Int32 hframes, hframes=(hframes : Int32) hframes=, offset : Vec2 offset, offset=(offset : Vec2) offset=, rect : Rect rect, size : Vec2 size, texture : Drawable | Nil texture, texture=(t : Drawable | Nil)
texture=(path : String)
texture=
, vframes : Int32 vframes, vframes=(vframes : Int32) vframes=, width : Float32 width

Constructor methods inherited from class Eagle::Sprite2D

load(path : String, position : Vec2 = Vec2::ZERO, filter : GPU::Filter = Texture.default_filter) : Sprite2D load, new(name : String = "", texture : Drawable | Nil = nil, position : Vec2 = Vec2::ZERO, centered : Bool = true)
new(texture : Drawable, position : Vec2 = Vec2::ZERO, name : String = "")
new

Instance methods inherited from class Eagle::Node2D

direction_to(other : Node2D) : Vec2 direction_to, distance_to(other : Node2D) : Float32 distance_to, down : Vec2 down, forward : Vec2 forward, global_position : Vec2 global_position, global_position=(p : Vec2) global_position=, global_rotation : Float32 global_rotation, global_scale : Vec2 global_scale, global_transform : Transform2D global_transform, look_at(target : Vec2) : Nil look_at, modulate : Color modulate, modulate=(modulate : Color) modulate=, parent_2d : Node2D | Nil parent_2d, position : Vec2 position, position=(p : Vec2) position=, right : Vec2 right, rotate(rad : Number) : Nil rotate, rotation : Float32 rotation, rotation=(rotation : Float32) rotation=, rotation_degrees : Float32 rotation_degrees, rotation_degrees=(d : Number) rotation_degrees=, scale : Vec2 scale, scale=(s : Vec2)
scale=(s : Number)
scale=
, to_global(local : Vec2) : Vec2 to_global, to_local(global : Vec2) : Vec2 to_local, top_level=(top_level : Bool) top_level=, top_level? : Bool top_level?, transform : Transform2D transform, transform=(t : Transform2D) transform=, translate(x : Number, y : Number) : Nil
translate(v : Vec2) : Nil
translate
, x : Float32 x, x=(v : Number) x=, y : Float32 y, y=(v : Number) y=

Constructor methods inherited from class Eagle::Node2D

new(name : String = "", position : Eagle::Vec2 = Vec2::ZERO, rotation : Float32 = 0_f32, scale : Eagle::Vec2 = Vec2::ONE) new

Instance methods inherited from class Eagle::Node

<<(child : Node) : self <<, [](path : String) : Node [], []?(path : String) : Node | Nil []?, add(child : Node) : Node
add(*children : Node) : Nil
add
, add_child(child : Node) : Node add_child, add_to_group(group : String) : self add_to_group, ancestor_of?(node : Node) : Bool ancestor_of?, can_process? : Bool can_process?, child?(name : String) : Node | Nil child?, child_added : Eagle::Emitter(Node) child_added, child_count : Int32 child_count, child_removed : Eagle::Emitter(Node) child_removed, children : Array(Eagle::Node) children, children_of(type : T.class) : Array(T) forall T children_of, clear_children : Nil clear_children, draw(g : Graphics) : Nil draw, dump(io : IO = STDOUT, indent = 0) : Nil dump, each_ancestor(& : Node -> ) : Nil each_ancestor, each_child(& : Node -> ) : Nil each_child, each_descendant(&block : Node -> ) : Nil each_descendant, emit_child_added(*args) : Nil emit_child_added, emit_child_removed(*args) : Nil emit_child_removed, emit_tree_entered(*args) : Nil emit_tree_entered, emit_tree_exited(*args) : Nil emit_tree_exited, enter_tree : Nil enter_tree, exit_tree : Nil exit_tree, find(name : String) : Node | Nil find, find_all(type : T.class) : Array(T) forall T find_all, first_child : Node | Nil first_child, free : Nil free, get_node(path : String, type : T.class) : T forall T
get_node(path : String) : Node
get_node
, get_node?(path : String) : Node | Nil get_node?, groups : Set(String) groups, in_group?(group : String) : Bool in_group?, in_tree? : Bool in_tree?, input(event : Event) : Nil input, name : String name, name=(name : String) name=, on_child_added(&block : Node -> Nil) : Proc(Node, Nil) on_child_added, on_child_removed(&block : Node -> Nil) : Proc(Node, Nil) on_child_removed, on_tree_entered(&block : -> Nil) : Proc(Nil) on_tree_entered, on_tree_exited(&block : -> Nil) : Proc(Nil) on_tree_exited, parent : Node | Nil parent, path : String path, physics_process(dt : Float32) : Nil physics_process, process(dt : Float32) : Nil process, process_mode : Eagle::Node::ProcessMode process_mode, process_mode=(process_mode : Eagle::Node::ProcessMode) process_mode=, queue_free : Nil queue_free, queued_free? : Bool queued_free?, ready : Nil ready, ready_called? : Bool ready_called?, remove(child : Node) : Nil remove, remove_child(child : Node) : Nil remove_child, remove_from_group(group : String) : Nil remove_from_group, remove_from_parent : Nil remove_from_parent, resized(width : Int32, height : Int32) : Nil resized, root : Node root, to_s(io : IO) : Nil to_s, tree_entered : Eagle::Emitter() tree_entered, tree_exited : Eagle::Emitter() tree_exited, visible=(visible : Bool) visible=, visible? : Bool visible?, z_index : Int32 z_index, z_index=(z_index : Int32) z_index=

Constructor methods inherited from class Eagle::Node

new(name : String = "") new

Instance Method Detail

def add_animation(name : String, frames : Array(TextureRegion), fps : Number = 10, loop : Bool = true) : Animation #

Adds an animation and returns it. Replaces any animation with the same name.


def animation : String | Nil #

Name of the current animation.


def animation_finished : Eagle::Emitter(String) #

Emitted when a non-looping animation reaches its last frame, with the animation's name. Connect with on_animation_finished { |name| ... }.


def animations : Hash(String, Eagle::AnimatedSprite2D::Animation) #

Every animation by name.


def current_animation : Animation | Nil #

The current Animation, or nil.


def emit_animation_finished(*args) : Nil #

Emitted when a non-looping animation reaches its last frame, with the animation's name. Connect with on_animation_finished { |name| ... }.


def emit_frame_changed(*args) : Nil #

Emitted whenever the shown frame changes, with the frame index. Use it to sync footstep sounds or hitboxes to specific frames.


def frame_changed : Eagle::Emitter(Int32) #

Emitted whenever the shown frame changes, with the frame index. Use it to sync footstep sounds or hitboxes to specific frames.


def frame_index : Int32 #

Index of the current frame within the animation.


def frame_index=(i : Int32) #

Jumps to a frame within the current animation.


def on_animation_finished(&block : String -> Nil) : Proc(String, Nil) #

Emitted when a non-looping animation reaches its last frame, with the animation's name. Connect with on_animation_finished { |name| ... }.


def on_frame_changed(&block : Int32 -> Nil) : Proc(Int32, Nil) #

Emitted whenever the shown frame changes, with the frame index. Use it to sync footstep sounds or hitboxes to specific frames.


def pause : Nil #

Stops advancing frames. Call #play to continue.


def play(name : String | Nil = nil, restart : Bool = false) : Nil #

Plays name, or resumes the current animation when name is nil. Restarts only when the animation changes or restart is true. Raises for an unknown name.


def playing? : Bool #

True while frames are advancing.


def process(dt : Float32) : Nil #

Advances frames. Called by the engine.


def speed_scale : Float32 #

Multiplies every animation's fps. Use it to match run speed to movement speed.


def speed_scale=(speed_scale : Float32) #

Multiplies every animation's fps. Use it to match run speed to movement speed.


def stop : Nil #

Stops advancing frames.