MediaDisplay’s onUnload of Doom

The MediaDisplay component has an onUnload event to ensure the FLV or MP3 it’s playing is closed before the component goes away. If you, however, attempt to create a movie clip/component with the same name in the same frame (or maybe it’s depth), it won’t work. You must wait a frame because the onUnload keeps a reference to the clip and stays around until the next frame. This caused havoc in a speech preso I was doing for school. I implemented a quick and dirty doLater to ensure I had no issues.

function doLater(obj, meth:Function):Void
{
        if(post_mc == null)
        {
                createEmptyMovieClip("post_mc", getNextHighestDepth());
        }
        var d:Number = post_mc.getNextHighestDepth();
        var ref_mc:MovieClip = post_mc.createEmptyMovieClip("d" + d, d);
        ref_mc.obj = obj;
        ref_mc.meth = meth;
        ref_mc.onEnterFrame = function()
        {
                trace("ref_mc calling");
                this.meth.call(this.obj);
                this.removeMovieClip();
        };
}

One Reply to “MediaDisplay’s onUnload of Doom”

  1. Unfortunatey onUnLoad events are a bad thing with the current component arhcitecture.

    I blogged about this issue recently here

    It stems I think from the fact that the onUnLoad actually fires a frame after the component was unloaded.

    So when you try to remove the component and recreate it Flash cant properly remove it instantly as it is holding on to a reference awaiting the next frame.

Comments are closed.