Jump to content
GreenSock

junky

With timelinelite/ max is there a way to create a listener for labels?

Recommended Posts

is there a way to set up a listener and function that gets fired every time a label in the "timeline" is reached?

 

I've tried

tl.addCallback(myFunction, "label1")

 

But it only fires when I hit that one label.

Link to comment
Share on other sites

hmm. i don't think there is anything built in.

but using the getLabelAfter() method, you can literally walk through all the labels and assign callbacks to them.

 

try this:

 

import com.greensock.*;
var tl:TimelineMax = new TimelineMax({paused:true});
tl.append(TweenLite.to(mc, .2, {x:100}));
tl.addLabel("cat", tl.duration);
tl.append(TweenLite.to(mc, .2, {x:50}));
tl.addLabel("dog", tl.duration);
tl.append(TweenLite.to(mc, .2, {x:200}));
tl.append(TweenLite.to(mc, .2, {x:100}));
tl.addLabel("mouse", tl.duration);
tl.append(TweenLite.to(mc, .2, {x:300}));
tl.addLabel("elephant", tl.duration);
tl.append(TweenLite.to(mc, .2, {x:200}));
var labels:Array = [];

function setupLabelCallbacks() {
if (tl.getLabelAfter() != null) {
 var nextLabel:String = tl.getLabelAfter(tl.currentTime);
 tl.addCallback(myFunction, nextLabel);
 trace(nextLabel);
 tl.gotoAndStop(nextLabel);
 setupLabelCallbacks();
}
else {
 trace("no more labels");
 tl.restart();
}
}
function myFunction(){
trace("myFunction fired at: " + tl.currentLabel);
}
setupLabelCallbacks();

 

basically the function

finds the next label,

adds a callback at that label,

advances the timeline to the next label,

finds the next label,

adds a callback at that label,

advances the timeline to the next label... until it can't find any more labels.

Link to comment
Share on other sites

Or you could just use your own little function that accomplishes that along the way, like this:

 

var tl:TimelineMax = new TimelineMax({paused:true});
tl.append(TweenLite.to(mc, .2, {x:100}));
appendLabel(tl, "cat");
tl.append(TweenLite.to(mc, .2, {x:50}));
appendLabel(tl, "dog");
tl.append(TweenLite.to(mc, .2, {x:200}));
tl.append(TweenLite.to(mc, .2, {x:100}));
appendLabel(tl, "mouse");
tl.append(TweenLite.to(mc, .2, {x:300}));
appendLabel(tl, "elephant");
tl.append(TweenLite.to(mc, .2, {x:200}));

function appendLabel(tl:TimelineMax, label:String):void {
tl.addLabel(label, tl.duration);
tl.addCallback(myFunction, tl.duration);
}

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×