0

Basically, I'm working on a flash game, and I want to be able to load mods into the game. Each mod would be a .swf file with an embedded xml file that holds all the information about the mod (name, version, main class, etc.) and then instantiate that main class (which extends a class Plugin). I have a mock-up of basically how I want to do it (from my experience in Java), but I'm not too clear on things such as embedding, can someone guide me?

Rough example:

//Loads the .swf file
private var i:int = 0;
private static function onLoadComplete(e:Event) {
    //xml would (supposedly, not too sure how this works) be the embedded xml file
    var pluginMain:Class = getDefinitionByName(e.target.content.xml.Main[0]) as Class;
    var plugin = new pluginMain();
    //initiate() is a function of my class Plugin, which plugin should extend
    plugin.initiate(this);
    plugins[i] = plugin;
    i++;
}

1 Answer 1

0

You can embed an XML file in a SWF by using the Embed tag with correct MIME type. The class variable defined directly beneath will contain the embedded file's data when instantiated. Here's a sample document class that embeds an XML file, instantiates it when loaded, and dispatches a 'READY' event once it has done so:

package  {

    import flash.display.Sprite;
    import flash.events.Event;

    public class Mod extends Sprite {

        [Embed(source="../xmldir/mod_data.xml", mimeType="application/octet-stream")]
        public var ModData:Class;

        public static const READY:String = "READY";

        public var modXml:XML;
        public var ready:Boolean = false;

        public function Mod() {
            data_init();
        }

        private function data_init():void {
            var modDataXML:XML = XML(new ModData());
            ready = true;
            dispatchEvent(new Event(READY));
            //trace(modDataXML.Main[0]);
        }

    }
}

You can listen for the event from your Loader's content property, or just check the content.ready property, in case it's already fired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.