Currently I am working on some games for my full time job with theRedSpace. The team I am a part of decided that what would be best is that we go with Flex (now Flash Builder) rather than Flash. Now I am by no means “afraid” of Flex, but as a very visual person I prefer to program in Flash, games especially. The main reason for this of course is because in Flash I have my beloved library where I can build my Movieclips to my heart’s content.
So the problem for me was “how can I use my Flash symbol inside my Flex project easily?”. At first I was thinking about simply loading external assets, but I then would have about 50 or more asset SWFs that I would have been trying to manage, and for me, that just wasn’t an option. I thought next about using the “Embed” meta tag to embed the SWFs and then use the symbol attribute to grab the clip I wanted by simply marking my Movieclip symbol for export into ActionScript in my Flash library. I learned quickly though that this would not be an option as when I bring a clip in through use of the embed tag that my code in the timeline is stripped (I don’t like putting code in the timeline, but for my simple stop() and gotoAndPlay() commands I really don’t mind it).
So I was back to where I started. Luckily I came across a great tutorial online (can’t find the link, but when I do I will post it here) that mentionned using the getDefinition() function to grab symbols from library symbols in Flash. The code is actually very simple. You just load the clip using a normal loader, you then use the getDefinition() function to grab the library object as it’s own class and then create an instance of the class.
Here’s a very quick and dirry example to give the feel:
var loader:Loader =
new Loader
();
loader.
addEventListener(Event.
COMPLETE, _loaded
);
loader.
load("theswf.swf");
function _loaded(e:Event):void
{
var skinClass:Class = e.target.applicationDomain.getDefinition("ElememtClassExportName") as Class;
var clip:MovieClip = new skinClass() as MovieClip;
this.addChild(clip);
}
Of course, here this example assumes that the element you’re trying to load in the is being exported to ActionScript as “ElementClassExportName” which would never happen (or should never, rather).
This little bit of code though is great because it will allow you to have all of your visual assets in a single SWF file that you can load at the start of your Flex application and not have to worry about managing boat loads of SWF asset files. Hope it can help you as it helped me!