Tweens
A Tween can be used to interpolate some state over time.
A Tween is created by instancing the Scar::Tween class and managed by a Scar::App. To start the Tween, use app#tween(t : Tween) method. The constructor works like this:
def initialize(
duration : Number,
@ease : Easing::EasingDefinition,
@on_update : Proc(Tween, Nil) = ->(t : Tween) {},
@on_completed : Proc(Tween, Nil) = ->(t : Tween) {}
)durationdenotes how long the tweening should last (in seconds)easedefines how the fraction is calculated (see below)on_updateis called every frameon_completedis called before the Tween is deleted
In on_update you should define what the Tween should modify. For example, if you want to tween a screen transition, you would modify the alpha value in on_update.
In on_completed you could for example start another tween to fade the alpha value back.
There are some instance methods to query or modify the state of a Tween:
linear_fractionreturns the linear progressionfractionreturns the fraction calculated by@easecompleted?returns if the duration has passedresetresets the elapsed time to 0 (restarts the Tween)abortcancels execution WITHOUT callingon_completedgetter and setter for
@pausedto pause/unpause the Tween
EasingDefinition
The abstract struct Scar::Easing::EasingDefinition exists to define how a Tween calculates it's fraction. You can either inherit this struct, use Scar::Easing::EaseWithFunction which can be initialized with a calculate function def initialize(@fn : Proc(Float32, Float32)); end or use the Scar::Easing#simple_easing_function(name, fn) macro where you can provide fn as a stringified mathematical expression like simple_easing_function(:EaseInQuad, "lf ** 2").
There are some builtin easing definitions, please refer to the reference for further information.
Example
app.tween(Tween.new
JUMP_TIME,
Easing::EaseOutQuad.new,
->(t : Tween) { tr.pos.y = t.fraction * JUMP_HEIGHT; nil },
->(t : Tween) { nil })Last updated
Was this helpful?