AttachMovie in AS3 ? Now possible! (also attachBitmapData ;)
I code in AS3 for quite a while now, and don’t get me wrong, I do love it and I NEVER want to go back to as2.
However; there are some things I wish remained in AS3 that were possible to do in AS2.
Things like “eval” and “attachMovie” comes to mind…
Why attachMovie ? Isn’t it perfectly possible to do addChild(new WhatewerClass()) ?
Yes and no. The problem is if you don’t develop in Flash IDE, but in more sophisticated code editors like FDT, FlashDevelop or Flex.
Let’s say you have a graphical asset sitting in your library, it needs no interactivity so you don’t create any class for it. It simply extends MovieClip and has a linkage name “Asset1″ assign to it.
Now the moment , in your code you decide to attach it to your display container via addChild(new Asset1())none of the code editors I mentioned above will know about Asset1 as that Asset1 is a class created by flash IDE only when the fla is being compiled and therefore is only available during runtime. So they will mark it as erroneous class reference. Of course , swf will compile just fine, but that’s not the point
You don’t want to have your project covered in errors of some of which are not really error and need to remember which ones are not real.. !
There are several solutions to this of course.
One I used, is to compile swc from the fla, so the every linked library assets are available to FDT/flash develop/flex for reference. The trouble with this solution is you have to keep the swc file updated which isn’t always straightforward.. For example flex sometimes doesn’t pick up that swc have been changed, you have to delete swc and then re-publish it to force it to recognise change. Similar situation happens with FDT sometimes. As well as you can into a situation where you get into a sort of a recursive error cycle , where swf doesn’t compile correctly because compiling that swf requires some assets from swc that aren’t there yet because project’s swc doesn’t have that assets yet because it can’t be compiled before the project doesn’t have those errors..
The solution I devised instead is much simpler and mimic the old as2 “attachMovie” functionality.
Taking advantage of built in “getDefinitionByName” command from flash.utils package we can turn the asset into a class by giving it it’s linkage ID . As that class is compiled into swf it will be available during runtime - so that will work fine.
During authoring time it won’t flag up as error as well as it’s typed correctly to MovieClip (so sprite will work as well).
So I wrote a wrapper utility class that has 2 functions :
attachMovie - expect a linkage name and returns a MovieClip instance
attachBitmapData - expect a linkage name and returns BitmapData
this is a code for attaching the library assets used in the demo bellow :
private function attachAsset1():void
{
var asset1:Sprite = MCUtils.attachMovie("Asset1");
asset1.x = 10;
asset1.y = 8;
this.addChild(asset1);
}
private function attachGirl():void
{
var girlBmp:BitmapData = MCUtils.attachBitmapData("Girl");
var girlBitmap:Bitmap = new Bitmap(girlBmp);
girlBitmap.x = 10;
girlBitmap.y = 85;
this.addChild(girlBitmap);
}
Demo :


November 2nd, 2009 at 3:58 pm
nice — this would make for a very handy tool.
November 3rd, 2009 at 12:26 pm
interesting, but while not typed results you can fall into runtime errors… in flex we do it this way
http://blog.yoz.sk/2009/10/embedding-assets-wisely/
November 3rd, 2009 at 1:47 pm
Yoz:
I think you’re missing a point here ( a bit)
My class isn’t supposed to be used in Flex to utilise the access to embeded assets.
It’s supposed to be used in more generic circumstances when you code in any better AS3 editor but you’re still compiling with flash IDE.
The class main purpose is to bridge the link between the Flash IDE’s 3 library embeded movie clips/bitmaps and external as3 editor that has no clue what’s in Flash’s IDE library.
So there is realy no room for type errors , the only errors you can get is if you mistype the linkage ID, or actualy forget to click on checkbox “export for actionscript” in library linkage properties.
November 3rd, 2009 at 8:24 pm
Slav: Yes, now I see your point. Anyway, wount it be better to create simple wrapper .as class file for each library symbol, map it and than use it?
November 4th, 2009 at 11:28 pm
Possible ? Yes. Simpler ? No.
What’s simpler about creating an empty class file for each bitmap or MovieClip you want to keep linked so you can add to stage later?
You have to create a file, put it in a correct package, type it in a correct package, type in correct class name and constructor.. You’ll get bunch of files that aren’t realy doing anything just taking space on your harddrive..
how is that simpler than NOT creating those files and having to write one line of code to attach them straight from library at runtime?
November 5th, 2009 at 10:22 am
I am not saying it is faster (simplier)… I think that is the correct way
“The class main purpose is to bridge the link between the Flash IDE’s 3 library embeded movie clips/bitmaps and external as3 editor that has no clue what’s in Flash’s IDE library.”
well, with correct classes for each item, your editor has clue. If the main problem is time consumption of manual creating/editing, you can always create snippet for this and new class file is one click away… Yeah, there is still thing with that 100 bytes text file on your HDD. But, resulting .swf is smaller (without MCUtils class), no runtime error possibility… You got my point?
November 5th, 2009 at 10:36 am
Yoz: I do get your point.

For me “simpler” means “faster” , “easier” and “more conventient”. I think you’re aiming for “proper”.
but .. even if I was aiming for “proper” I’d still wouln’t be creating empty MovieClip wrapper class files
I would compile my fla as swc and attach it to the classpath of those external editors (all of them can parse SWCs..)
I guess this is all matter of subjective taste, so perhaps for you it really IS easier and more convenient to create those files. If that’s the case , you’re welcommed not to use my utils class
November 8th, 2009 at 9:31 pm
ok, it is subjective matter… but anyway, creating basic wrapper class is as proper as use swc (but why to do this and recompile on every change?) because when compiling with flash ide, wrapper class is automaticaly created by ide if not found.
January 21st, 2010 at 5:32 am
Могу поискать ссылку на сайт с информацией на интересующую Вас тему.