Behavior

Life cycle

A Behavior has a very simple lifecycle. If the Behavior is not cycling, the developer has the option of being notified on Behavior expiration by adding a listener to it. This is a simple triggering mechanism by which you will get informed of which Behavior has expired, and at exactly what time. The behavior listener can also be notified every time the behavior has been applied. A behavior is guaranteed to apply the final value it is defined for.

A behavior listener is an object of the form:

                            {
                                behaviorExpired : function( behavior, time, actor);
                                behaviorApplied : function( behavior, time, normalizedTime, actor, value);
                            }
                        
Note that any behavior can have an arbitrary number of observers.

A behavior observer will be added by calling addListener( behavior_listener_object ).

The behavior listener object function behaviorExpired parameters are:

  1. The CAAT.Behavior object that just expired.
  2. The CAAT.Scene object time the CAAT.Behavior just expired at.
  3. The CAAT.Actor the CAAT.Behavior was acting upon.

and for the function behaviorApplied parameters are:

  1. The CAAT.Behavior object that just expired.
  2. The CAAT.Scene object time the CAAT.Behavior just expired at.
  3. The behavior related time it was applied at.
  4. The CAAT.Actor the CAAT.Behavior was acting upon.
  5. The behaviors value that has been applied.

In this example, two behaviors are set for an actor. When on ebehavior expires, the other one is started and vice versa:

var director_4 = new CAAT.Foundation.Director().initialize(160, 160, "_c4");
var scene_4 = director_4.createScene();

var shape = new CAAT.Foundation.UI.ShapeActor().
        setShape(CAAT.Foundation.UI.ShapeActor.SHAPE_RECTANGLE).
        setLocation(50, 50).
        setSize(60, 60).
        setFillStyle('#f00').
        setStrokeStyle('#000');
scene_4.addChild(shape);

var scaleBehavior = new CAAT.Behavior.ScaleBehavior().
        setPingPong().
    // 50, 50 means to scale at 50% actor width and 50% actor height
    // its center.
        setValues(1, 2, 1, 2, .50, .50).
    // scale Behavior enabled by default. Start at time=2000ms, and
    // last for 3000ms.
        setFrameTime(2000, 3000);

// unless otherwise stated, Behaviors are expired by default,
// so this actor won't rotate until instrumented to do so.
var rotateBehavior = new CAAT.Behavior.RotateBehavior().
    // 50, 50 means to scale at 50% actor width and 50% actor height
    // its center.
        setValues(0, 2 * Math.PI, .50, .50);

shape.addBehavior(scaleBehavior);
shape.addBehavior(rotateBehavior);

// when scale Behavior finishes, start rotation Behavior.
scaleBehavior.addListener({
    behaviorExpired : function(behavior, time, actor) {
        rotateBehavior.setFrameTime(time, 3000);
    }});

// when rotation Behavior finishes, start scale Behavior.
rotateBehavior.addListener({
    behaviorExpired : function(behavior, time, actor) {
        scaleBehavior.setFrameTime(time, 3000);
    }});

director_4.loop(30);