[SOLVED] External swf woes. How to play and stop a swf?


(Quang Tran) #1

I have an agreement with a flash portal site to create a sitelock version of my game for them. However, their logo animation is an external swf that I believe is an AS2 swf. I’m having problems playing & stopping it. I’ve put together some hacky code to get it to play but when I remove it the sound continues to loop.

        [Embed(source = "graphics/logo.swf", mimeType = "application/octet-stream")] private var LOGO_ANIM:Class;
	private var swf_loader:Loader;
	private var child_swf:DisplayObject;
	private var preloader_ld: Loader;
	private var preloader_ld_loaded: MovieClip;
	private var frame_counter:int = 0;
	
	private function _StartUpAnim():void {
		preloader_ld = new Loader();
		preloader_ld.loadBytes(new LOGO_ANIM as ByteArray);
		preloader_ld.contentLoaderInfo.addEventListener(Event.COMPLETE, _LoadHandler);
	}
	
	private function _LoadHandler(e:Event = null):void {
                 // Note: e.loader.content is an AVM1Movie. I have not had any luck casting this to a MoveClip.
		child_swf = (e.target.content as DisplayObject);
		swf_loader = e.currentTarget.loader;
		addChild(swf_loader);
		child_swf.addEventListener(Event.ENTER_FRAME, _EnterFrame);
	}
	
	private function _EnterFrame(e:Event):void {
		frame_counter += 1;
                    // Hacky way to stop the swf. Since I am unable to cast to a MoveClip, I do not have access to totalFrames and currFrame properties.
		if (frame_counter > 200) { 
			child_swf.removeEventListener(Event.ENTER_FRAME, _EnterFrame);
			removeChild(swf_loader);
			
			// Code to go to game's main menu
			var mainClass:Class = getDefinitionByName("Main") as Class;
			parent.addChild(new mainClass as DisplayObject);
			parent.removeChild(this);
		}
	}

This hacky method does seem to work. My main problem is that after I removeChild(swfLoader), the sound for their logo continues to play & loop forever. Any idea how to clean out this swf/AVM1 movie?


(Quang Tran) #2

I was able to get the answer from another forum. Turns out all I needed was this line:

swf_loader.unloadAndStop();

Hope this is of some help to someone in the future!