Skip to content

8. Events and Callbacks

Michael Carriere edited this page Jan 11, 2015 · 3 revisions

Introduction

GoKit has a robust amount of events that will reliably fire during the course of a Tween, TweenChain and TweenFlow to give you very strict control over your animations.

The callbacks are as follows:

  • onInit
  • onBegin
  • onIterationStart
  • onUpdate
  • onIterationEnd
  • onComplete

You may set these either on the GoTweenConfig:

GoTweenConfig config = new GoTweenConfig();
config.onBegin(SomeBeginHandler);

or you may set them directly on the Tween:

GoTween moveBlock = Go.to(someGameObject, 1f, config);
moveBlock.setOnCompleteHandler(SomeCompleteHandler);

Event Specifics

Since GoKit ensures that the start/end points of each Tween iteration are exactly the properties that you defined, we can ensure that every event will reliably fire through the normal lifespan of a Tween, TweenChan and TweenFlow.

onInit

  • Called once, before any Tween properties are setup.

Useful it you want to force starting values on your properties right before the Tween starts.

onBegin

  • Called when the Tween is at the start of the timeline.

onIterationStart

  • Called at the start of an iteration (loop).

onUpdate

  • Called every time the tween is updated. (once per frame)

onIterationEnd

  • Called at the end of an iteration (loop).

onComplete

  • Called when the Tween is complete.

When a tween is running indefinitely, this event will never be fired.

Tween Direction

Begin/Complete and Start/End events are always relative to the direction that the tween is playing in.

For illustrative purposes, assume this is the visual of a tween that has three loops, with a duration of 1s.

     | - - - - - | - - - - - | - - - - - |
 t = 0           1           2           3
    (a)         (b)         (c)         (d)

If the tween is playing forward:

  • onBegin fires at a.
  • onIterationStart fires at a, b, c.
  • onIterationEnd fires at b, c, d.
  • onComplete fires at d.

If the tween is playing in reverse:

  • onBegin fires at d.
  • onIterationStart fires at b, c, d.
  • onIterationEnd fires at a, b, c.
  • onComplete fires at a.

goto() and gotoAndPlay()

All of the aforementioned events will reliably fire when any type of Tween is created and played. If a Tween is being forced to various stages of the timeline via the goto() and gotoAndPlay() methods, there are a few things to keep in mind:

  • onBegin will fire if you goto the start of the Tween, relative to its direction.
  • onIterationStart will only fire during a goto() if onBegin will fire.
    • Using our example above with the Tween Direction, if you called someTween.goto(1), the onIterationEnd would fire immediately, and the onIterationStart would fire on the following frame.