launch animation after user action

In this tutorial, we will animate a shape after a user action.
We use a timer to animate the shape and a key sensor to capture user action.

See Move shape tutorial for example about this node.
We use TimeSensor node to animate the widget.

We define a "ANIM_TIMER" timer (a TimeSensor node).
When widget is launch, timer is not activated ("isActive FALSE").
Timer is activated after a user action.
At end of time interval (after 1 seconds), timer is stopped ("loop FALSE").
Time sensor declaration:


We catch timer pulse with fraction_changed (SFFloat - value between 0.0 (animation start) and 1.0 (end of animation))
We catch end of loop with cycleTime (SFTime - current time)


KeySensor node send event when user press a key.
We use KeySensor node to catch user action and launch animation (activate the timer).

Note:
if animation is already launched, not re-launch animation.


We define a shape (a simple rectangle).
To move rectangle, we use a Transform2D node.


To create animation, we use PositionInterpolator2D node.
In first time, we create a simple animation.


Link the Interpolator with the timer:


Link the Interpolator with the shape:


Final code for the first example:


We want to move shape when user press joypad key.
We listen joypad keys with the KeySensor:


Define function to move shape when user press a key.
This function:



PositionInterpolator2D cannot be use (we cannot redefine animation...).
We use a JavaScript function to compute all positions of animation.
This function use fraction_changed field of timer and return a position (SFVec2f type).


We define in/out event for animation:


And we link animation event with the timer and the shape:


Final code for the second example: