views:

60

answers:

2

Is it possible to dynamically load vector files, in this case .ai files in Actionscript 3? You can import them to the stage, but I want to do this dynamically, is there a way to do this?

A: 

Of course,

you can use the URLLoader class to load it at runtime:

var _loader:URLLoader;
var request:URLRequest = new URLRequest("http://yourserver.com/graphic.ai");
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);

    private function completeHandler(e:Event):void {
        var content:DisplayObject = _e.target.content;
        addChild(content);
    }

I didn't tested it, but I think this works!

23tux
"_e.target.content" should be "e.target.data". I think you also need to typecast it before the compiler will let you put it into a DisplayObject variable. Perhaps you were thinking of Loader instead of URLLoader? The current code will not work because Flash has no idea what kind of data was just loaded (it could be XML, binary data, a PNG, etc.) and so cannot convert the AI file into a DisplayObject because it does not know that the data loaded was an AI file. I'm not certain Flash can convert AI files at runtime in the first place.
Cameron
Uh, you're right. It has to be a Loader instead of an URLLoader.But I'm not sure with the typecast, I'm gonna check it later ;-)
23tux
A: 

You could save the vector data in an XML format such as SVG (Illustrator supports this), then load it using URLLoader, parse the data and draw it into a Shape object.

There are a few AS3 SVG parsers available on the web: http://www.google.com/search?q=svg+as3

geo