All pastes #1059968 Raw Edit

Henke37

public text v1 · immutable
#1059968 ·published 2008-07-01 22:04 UTC
rendered paste body
package HTools {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.utils.ByteArray;
	
	import mx.core.ByteArrayAsset;

	public class SwfLoader extends Sprite {
		
		/**
		 * Swf loader class, wraps the loading of a bytearray from an embeded symbol
		 * Copyright Henke37 2008
		 * Just give credit when it's due.
		 */
		 
		 /*
		 	Here is how to use this class:
		 	# Create the fla
		 	# Set the publish settings of the fla to NOT automaticaly declare instances
		 	# Set and write the document class for the FLA. It must be declared public!
		 	# Publish the fla to get the swf
		 	# Embed the swf, but mark it as having the mimeType "application/octet-stream".
		 	# Create an asset ByteArray by calling new on the asset property
		 	# Create an instance of the Swfloader class (that's this class),
		 		passing the ByteArray from the previous step
		 	# Write an even handler for the Event.COMPLETE event that is fired from the loader.
				In that, you can access the content property of this class.
				Do not forget to cast the object to the document class
			# Add the event listener to the loader. DO NOT idle with this,
				do it ASAP, like directly after creating it.
				Otherwise you might miss the event.
				
			The gotcha list:
			* Not waiting for the Event.COMPLETE event before using the content property
			* Not declaring the document class to be public
			* Not marking the embed tag with the proper mimeType
			* Forgeting to store the content object in a variable with type of the Document class
			* Not declaring the instances explitly in the document class,
				preventing Flex from finding the properties for them
				
			Also, try to keep the documet class small since it will be embeded twice.
		 */
		
		private var loader:Loader;
		private var data:ByteArray;
		
		public function SwfLoader(data:ByteArray) {
			super();
			loader=new Loader();
			loader.loadBytes(data);
			addEventListener(Event.ENTER_FRAME,onEnterFrame,false,0,true);
		}
		
		public function get content ():MovieClip {
			if(loader.content) {
				return MovieClip(loader.content);
			} else {
				return null;
			}
		}
		
		private function onEnterFrame(e:Event):void {
			if(loader.content) {
				dispatchEvent(new Event(Event.COMPLETE));
				removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			}
		}
		
	}
}