class Eagle::Timer

Overview

Counts down and emits #timeout. Use it for spawn waves, cooldowns and delayed events that should pause with the scene tree.

class Spawner < Node2D
  def ready : Nil
    add(Timer.new(2.0, autostart: true) { spawn_enemy }) # every 2 seconds
    add(Timer.new(30.0, one_shot: true, autostart: true) { puts "boss!" })
  end

  def spawn_enemy : Nil
    add(Sprite2D.new(Texture.new(Image.circle(8, Color::RED)), v2(rand * 400, 0)))
  end
end

For a one-off delay that doesn't need a node, Tween.after is shorter.

Defined in:

eagle/nodes/timer.cr

Constructors

Instance Method Summary

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

Constructor Detail

def self.new(wait_time : Number = 1.0, one_shot : Bool = false, autostart : Bool = false, name : String = "", &block : -> ) #

Creates a timer and connects the block to #timeout.


def self.new(wait_time : Number = 1.0, one_shot : Bool = false, autostart : Bool = false, name : String = "") #

Creates a timer.


Instance Method Detail

def autostart=(autostart : Bool) #

Start automatically when the timer enters the tree.


def autostart? : Bool #

Start automatically when the timer enters the tree.


def emit_timeout(*args) : Nil #

Emitted when the countdown reaches zero. Connect with on_timeout { ... }.


def on_timeout(&block : -> Nil) : Proc(Nil) #

Emitted when the countdown reaches zero. Connect with on_timeout { ... }.


def one_shot=(one_shot : Bool) #

Stop after the first timeout instead of repeating.


def one_shot? : Bool #

Stop after the first timeout instead of repeating.


def paused=(paused : Bool) #

Freezes the countdown.


def paused? : Bool #

Freezes the countdown.


def physics=(physics : Bool) #

Count down in fixed steps (#physics_process) instead of per frame.


def physics? : Bool #

Count down in fixed steps (#physics_process) instead of per frame.


def physics_process(dt : Float32) : Nil #

Counts down each fixed step when physics is set. Called by the engine.


def process(dt : Float32) : Nil #

Counts down each frame. Called by the engine.


def progress : Float32 #

Fraction of the wait that has passed, from 0 to 1. Handy for cooldown bars.


def ready : Nil #

Starts the timer if autostart is set.


def running? : Bool #

True while counting down.


def start(time : Number | Nil = nil) : self #

Starts or restarts the countdown, optionally with a new wait time. Returns self.


def stop : Nil #

Stops the countdown.


def stopped? : Bool #

True when not counting down.


def time_left : Float32 #

Seconds until the next timeout.


def timeout : Eagle::Emitter() #

Emitted when the countdown reaches zero. Connect with on_timeout { ... }.


def wait_time : Float32 #

Seconds between timeouts.


def wait_time=(wait_time : Float32) #

Seconds between timeouts.