/** * SWFLoader * * Instead of having to [embed] the desired .swf like other classes do, * you can now load as many .swfs as you want dynamically when you need them * and create instances of those symbols you defined an AS-linkage for. * * Also it's not required to know the definition names, they get extracted * from the .swf and put in an array you have access to by the * definition-property. * * Remember to enable Linkage and give a Class-name in the Flash IDE library for * every symbol you want to use here. * * * Original use of this class is to load runtime-defined .swfs and use them as libraries * for a tileset-map-editor which is kinda non-existent at this moment :) * * * * ----------- * NOTE: maybe only up to 256 definitions allowed - needs further testing * * * @author kris * @version 0.666 */ package de.kris.utils { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Loader; import flash.display.MovieClip; import flash.display.Sprite; import flash.errors.EOFError; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.media.Sound; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.system.LoaderContext; import flash.text.Font; import flash.utils.ByteArray; /** * Loads a .swf-file, provides the definition-names and creates instances of the defined classes. */ public class SWFLoader extends EventDispatcher { private var l:Loader; private var ul:URLLoader; private var ba:ByteArray; private var classesNames:Array; private var debug:Boolean; private var ready:Boolean=false; /** * Creates an SWFLoader-instance. * * @param url URL to the .swf file. * @param debug Debug-traces on/off. */ public function SWFLoader(url:String, debug:Boolean=false):void { super(); this.debug = debug; dtrace("------------------------------------------------"); ul = new URLLoader(); ul.dataFormat = URLLoaderDataFormat.BINARY; ul.addEventListener(Event.COMPLETE, onULComplete); ul.addEventListener(IOErrorEvent.IO_ERROR, onIOError); ul.addEventListener(ProgressEvent.PROGRESS, onULProgress); ul.load(new URLRequest(url)); } /** * An array of strings, which represent the definition names you are allowed to instantiate. */ public function get definitions():Array { return ready?classesNames:null; } /** * Returns the class. * * @param className * @return The class definition itself. (useful for fonts or other shizzle) */ public function getDefinition(className:String):Class { return (classesNames.indexOf(className)!=-1)?(l.contentLoaderInfo.applicationDomain.getDefinition(className) as Class):null; } /** * If existing and extending Sprite, an instance of the class is created and returned as Sprite. (You can cast it to a more special type (like MovieClip) after calling the function!) * * @param className The name of the class you want to instantiate, if the name is not in the definitions-array. Otherwise null! * @return The instance */ public function getSprite(className:String):Sprite { return getValidatedClassIfReady(className, "flash.display::Sprite") as Sprite; //return (classesNames.indexOf(className)!=-1)?DisplayObject(new (l.contentLoaderInfo.applicationDomain.getDefinition(className) as Class)()):null; } /** * [ Deprecated function. Kept because of old implementations in older projects of mine.. :D ] * * If existing and extending Sprite, an instance of the class is created and returned as Sprite. (You can cast it to a more special type (like MovieClip) after calling the function!) * * @param className The name of the class you want to instantiate, if the name is not in the definitions-array. Otherwise null! * @return The instance */ public function getInstance(className:String):Sprite { return getSprite(className); } /** * Creates an instance of the class represented by the index in the definitions-array. Useful for random instances. * * @param id Index of class in the definitions-array. * @return The instance... */ public function getSpriteByID(id:uint):Sprite { return (ready && classesNames && id0) { tmpID = ba.readUnsignedByte(); ba.position++; tmpName = ""; char = 0; while((char = ba.readUnsignedByte())!=0) { tmpName += String.fromCharCode(char); } // trace(tmpID + " - " + tmpName); classesNames.push(tmpName); classesNum--; } } catch ( e:EOFError ) { dtrace(e); dtrace("no symbols in library found or some unkown error"); return false; } classesNames.sort(); tmpName = ""; for (var i:int = 0; i < classesNames.length; i++ ) tmpName += i + ") " + classesNames[i] + ", "; dtrace(tmpName.slice(0,tmpName.length-2)); return true; } public function unload():void { ba = null; l = null; ul = null; } } }