/**
* Copyright (C) 2009 Uwe Holland
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* If you want to contact the author please don't hesitate to write an e-mail
* to Uwe Holland
*
*
* @author Uwe Holland
*/
package rg.timeline
{
import rg.events.FrameEvent;
import flash.display.FrameLabel;
import flash.display.MovieClip;
public class TimelineEvents
{
private var _clip: MovieClip;
private var _dispatchOnce: Boolean;
/**
* Class constructor
*/
public function TimelineEvents(clip: MovieClip, addBeginEndEvents: Boolean = false, dispatchOnce: Boolean = false)
{
_clip = clip;
_dispatchOnce = dispatchOnce;
var labels: Array = clip.currentLabels;
var frameLabel: FrameLabel;
for each(frameLabel in labels)
{
_clip.addFrameScript(frameLabel.frame - 1, addLabelEvent);
}
if(addBeginEndEvents)
{
_clip.addFrameScript(0, addBeginEvent);
_clip.addFrameScript(_clip.totalFrames - 1, addEndEvent);
}
}
private function addBeginEvent(): void
{
_clip.dispatchEvent(new FrameEvent(FrameEvent.BEGIN, _clip.currentFrame, _clip.currentLabel));
removeEvent();
}
private function addEndEvent(): void
{
_clip.dispatchEvent(new FrameEvent(FrameEvent.END, _clip.currentFrame, _clip.currentLabel));
removeEvent();
}
private function addLabelEvent(): void
{
_clip.dispatchEvent(new FrameEvent(FrameEvent.LABEL, _clip.currentFrame, _clip.currentLabel));
removeEvent();
}
private function removeEvent(): void
{
if(!_dispatchOnce) return;
_clip.addFrameScript(_clip.currentFrame - 1, null);
}
}
}