Could anyone advise the best way to trigger a functiont when a movieclip animation finishes? I figure an eventlistener could handle this, but not sure the best way to go about it. Thanks Paul
There are a few ways to go about this:
In reference to point 3, I would create a base class for your object. This way you can apply the same logic to multiple elements being animated.
Something like this:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class AnimatingObject extends MovieClip
{
// constants
public const ANIMATION_COMPLETE:String = "animation_complete";
/**
* Constructor
*/
public function AnimatingObject()
{
addEventListener(Event.ENTER_FRAME, _handle);
}
/**
* Called on dispatch of Event.ENTER_FRAME
*/
private function _handle(e:Event):void
{
if(currentFrame == totalFrames)
{
var evt:Event = new Event(ANIMATION_COMPLETE);
dispatchEvent(evt);
}
}
}
}
Now we can listen for "animation_complete" and do stuff accordingly.
package
{
import flash.events.Event;
public class MyAnimatingObject extends AnimatingObject
{
/**
* Constructor
*/
public function MyAnimatingObject()
{
addEventListener(ANIMATION_COMPLETE, _lastFrame);
}
/**
* Called on dispatch of AnimatingObject.ANIMATION_COMPLETE
*/
private function _lastFrame(e:Event):void
{
trace("i'm done");
}
}
}
It's been awhile since I played with flash. I mostly do flex now, but this should work.
Using the enterFrame Event would be a huge waste of resources, and creating a custom event class is not nessesary.
On the last frame put this
dispatchEvent(new Event("INSERTSTUPIDEVENTNAMEHERE"));
And in your code on your "root"
movieInstanceName.addEventListener( "INSERTSTUPIDEVENTNAMEHERE", someCallBackFunction );
function someCallBackFunction ( e:Event ):void{
trace( "Last frame hit");
}
By making use of an ENTER_FRAME listener, you can tell if a MovieClip has readed the end of playback; you can then take this one step further by wrapping it up in a Wrapper class that will perform the monitoring for you:
public class EndOfMovieClipEventDispatcher extends EventDispatcher
{
private var target : MovieClip;
private var endReachedEvent : String;
public function EndOfMovieClipEventDispatcher(target : MovieClip, endReachedEvent : String = "complete") {
this.target = target;
this.endReachedEvent = endReachedEvent;
target.addEventListener(Event.ENTER_FRAME, onEnterFrameEvent, false, 0, true);
}
public function destroy() : void {
target.removeEventListener(Event.ENTER_FRAME, onEnterFrameEvent);
}
private function onEnterFrameEvent(event : Event) : void
{
if (target.currentFrame == target.totalFrames) {
dispatchEvent(new Event(endReachedEvent));
}
}
}
Usage is pretty straight forward; the call to destroy() is optional thanks to the weak event listener; but recommended if you are finished :)
new EndOfMovieClipEventDispatcher(myMovieClip).addEventListener(Event.COMPLETE, onMovieClipCompleteEvent);
myMovieClip.play();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With