AS3 Stop external swf

Robin picture Robin · Mar 2, 2010 · Viewed 10.3k times · Source

Hi I'm loading an external swf into a MovieClip, and I want it to stop until I choose to play. Currently it plays upon loading immediately.

var mc:MovieClip;

var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, eventLoaded); 
var request:URLRequest;
request = new URLRequest("external.swf");
swfLoader.load (request);

function        eventLoaded(e:Event): void
{
   mc = e.target.content as MovieClip;
// does not stop the clip
   mc.Stop ();
}

So I tried adding a Event.ENTER_FRAME to the movieclip and stop it there, that will stop but it will play the first frame. Is there a way to get it to stay stopped when loaded until I choose Play?

Answer

DaveGauer picture DaveGauer · Apr 6, 2011

It's actually very close to what Jochen Hilgers suggested. However, in this instance, the event you want is actually INIT instead of COMPLETE. INIT is fired when the content is not yet fully loaded but is ready for use (and will start playing on its own).

Attach the event with

loader.contentLoaderInfo.addEventListener(Event.INIT, handleReady );

And handle it with

public function handleReady( initEvent:Event ):void{
        MovieClip(initEvent.currentTarget.content).stop();
}

You'll notice that you can cast the content property of currentTarget as a MovieClip and stop it even before it has been attached to the stage.

It is important to note that it is not safe to use the content property in a PROGRESS event (or any time prior to an INIT or COMPLETE event). You will get an error to the effect that the object is not ready.