embedding sources dynamically

喜欢而已 提交于 2019-12-25 03:38:13

问题


is it possible to embed sources dynamically. instead of doing this

[Embed(source = '../../../../assets/levels/test.xml')]

I could probably do something like this

var src = '../../../../assets/levels/test.xml'
[Embed(source = src )]

回答1:


It's not possible for anything within metadata annotations to be dynamic :/. That is, you can't place variables into metadata annotations. If that was possible, there would be SO many cool possibilities. So your first option is the only way to directly embed xml.

You could, however, write a custom metadata parser that figured out how to load (not embed) your xml file. Something like:

[LoadFile]
public var source:String = "../../../../assets/levels/test.xml";

I would implement that like the code below (just wrote this right now, haven't tested it). And then you'd "process" your class via something like MyMetadataUtil.process(this). Lots of ways to do that.

public function extractMetadata(target:Object):void
{
    var description:XML = flash.utils.describeType(target);
    var tag:String = "LoadFile"
    var metadata:XMLList = description.accessor.metadata.(@name == tag);
    metadata += description.variable.metadata.(@name == tag);
    var i:int = 0;
    var n:int = metadata.length();
    // usually called a 'directive'
    // holds values from metadata annotation
    var token:Object = {};
    for (i; i < n; i++)
    {
        metadataXML = metadata[i];
        token.property = metadataXML.parent().@name;
        // token.source = myClass.source;
        token.source = target[token.property];
        var request:URLRequest = new URLRequest(token.source);
        var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, loader_completeHandler);
        loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
        loader.load(request);
    }
}

protected function loader_completeHandler(event:Event):void
{
    event.currentTarget.removeEventListener(event.type, loader_completeHandler);
    trace("SUCCESSFULLY LOADED FILE!");
}

protected function loader_ioErrorHandler(event:Event):void
{
    event.currentTarget.removeEventListener(event.type, loader_ioErrorHandler);
}

That stuff would go into some util/manager/processor class. Then anywhere in your code, you could use this:

[LoadFile]
public var source:String = "myFile.xml";

And that could be dynamic. Check out the Swiz Framework for some example source code on how to implement custom metadata processors. Or even better, Openflux's MetaUtil. Once you set that up once, you can do some hardcore stuff in your code. Makes coding fun and fast.

Hope that helps, Lance




回答2:


Your use case is basically why I created the ability to add extra frames to Flex SWFs that are treated as late-loaded modules. Instead of embedding your level, stream it in after the main application.

Documentation on -frame is sparse. Sorry! Here's some external stuff, that links back to stuff I wrote and Alex Harui wrote. Good luck!

http://www.richinternet.de/blog/index.cfm?entry=FF295F89-DAD8-CCDC-960413842BC0D478



来源:https://stackoverflow.com/questions/2215875/embedding-sources-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!