开发者

embedding sources dynamically

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

[Embed(source = '../../../../assets/le开发者_如何学运维vels/test.xml')]

I could probably do something like this

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜