Flex and Flash Developer - Jesse Warden dot Kizz-ohm

A blog on software development, technology, games & movies.

About

This is the blog of Jesse Warden, a Rich Internet Application Architect. He specializes in using Flex and Flash to create Rich Internet Applications.

Not much of an engine, but I need to generate events when an animation completes, so devised this function to do so. It’s based on the start and stop frames of the animations being static variables; basically enumerated values.

You pass in what function you want to run, where it’s at (scope), what timeline your looking in for the frame position, and what the target frame number is. So, if your character attacks on frame 10, and the animation completes on frame 20, and you want to call a function when it’s done, you’d do:

static var ATTACK:Number = 10;
this.gotoAndPlay(ATTACK);
doWhenFrameReached("onDoneAttacking", this, this, 20);

Main engine function below:

function doWhenFrameReached(handler:String,
                            scope:Object,
                            timeline:MovieClip,
                            frameNumber:Number):Void
{
        if(!process_mc){
                createEmptyMovieClip("process_mc", getNextHighestDepth());
        }
        var ID = process_mc.getNextHighestDepth();
        var ref_mc = process_mc.createEmptyMovieClip("process" + ID, ID);
        ref_mc.handler = handler;
        ref_mc.scope = scope;
        ref_mc.timeline = timeline;
        ref_mc.frameNumber = frameNumber;
        arguments.shift();
        arguments.shift();
        arguments.shift();
        arguments.shift();
        ref_mc.args = arguments;
        ref_mc.onEnterFrame = function()
        {
                if(this.timeline._currentframe == this.frameNumber){
                        this.scope[this.handler].apply(this.scope, this.args);
                        this.removeMovieClip();
                }
        };
        //ref_mc.onEnterFrame(); // just in case already there
}


Leave a Reply