I know it’s an old subject but I didn’t use the combination of AS3 and timeline animations for my projects so far.
Coming from Big Spaceship, Adobe DevNet / TimelineWatcher and Bytearray.org I wanted a clean solution to recognize FrameLabels on a timeline without using EnterFrame (which fails if you want to address newly instantiated timeline objects within the given frame) and without extending MovieClip. Mixing all together I come up with this:
FrameEvent.as
TimelineEvents.as
TimelineEvents injects dispatchEvent calls with FrameEvent (carrying currentFrame and currentLabel) using addFrameScript to the timeline on every FrameLabel.
Optional you can inject extra events for BEGIN and END on the timeline by setting addBeginEndEvents to true. By default events get dispatched every time you come across the FrameLabel. You can force it to dispatch only once by setting dispatchOnce value to true. (Be careful! Existing source code within the timeline gets overwritten by using addFrameScript for the corresponding frame. So if you got a stop(); at the end of the timeline and you set addBeginEndEvents to true it won’t stop anymore unless you do a stop within the FrameEvent.END event)
Howto use:
// clip is a MovieClip containing a few framelabels
(new TimelineEvents(clip, true, true));
clip.addEventListener(FrameEvent.LABEL, onFrameLabel);
clip.addEventListener(FrameEvent.BEGIN, onFrameLabel);
clip.addEventListener(FrameEvent.END, onFrameLabel);
private function onFrameLabel(event: FrameEvent): void
{
switch(event.type)
{
case FrameEvent.LABEL:
trace(event.currentFrame + ": " + event.currentLabel);
break;
case FrameEvent.BEGIN:
trace("you played the first frame");
break;
case FrameEvent.END:
trace("you played the last frame");
break;
}
}
2 Comments
I think you left some new operators in there:
clip.addEventListener(new FrameEvent.LABEL, onFrameLabel);
clip.addEventListener(new FrameEvent.BEGIN, onFrameLabel);
clip.addEventListener(new FrameEvent.END, onFrameLabel);
Otherwise, this is handy. Thanks!
thanks for the comment, gotta fix that!