AS3 Stop external swf

2020-02-13 17:46发布

问题:

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?

回答1:

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.



回答2:

I wrote this simple TestCase and it works fine... the loaded swf is quite simple, just a tween on the main timeline.

package {
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;

    public class Test extends Sprite
    {
        private var loader:Loader = new Loader;

        public function Test()
        {
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaded );
            loader.load( new URLRequest( 'testFile.swf' ) );    
        }

        public function handleLoaded( event:Event ):void
        {
            addChild( loader.content );
            var mc:MovieClip = loader.content as MovieClip ;
            mc.stop();
        }
    }
}


回答3:

I was looking for a similar problem/solution, but my problem was little diferent. I know this was not your issue, but looks fair to share my solution. When I tried to do

    event.currentTarget.stop(); // AS1&AS2 -> BAD swf to import

with the content of a loader, my Flash IDE showed me this error:

"Property stop not found on flash.display.AVM1Movie and there is no default value."

This happened to me because the swf I imported was created using AS1, and not AS3 as the main movie ( so I decompiled the swf to a fla and recompiled using as3, it was an output from After Effects). Now I know AVM1 and AVM2 are classes that represent actionscript 1 and 2 files.