Associating Custom AS3 Classes with Embedded Assets
Keith Peters had a couple of posts a little while ago about embedding assets in as3 (1 and 2). One thing that came up in both of them that could not be resolved was how to associate a custom class with an embedded symbol. The example given was:
...create a movie clip in the Flash 9 IDE, give it a class of “Star”, and you have an actual class written, called “Star” that has some functionality that is really cool. Now, you embed that star symbol in your AS3 application, but you can only type it as Sprite or MovieClip. How to get it to be a “Star”? I finally got to dig in to this a bit more, and sadly, I don’t think there is a way to do this. When you embed a Sprite asset, it comes in as an instance of SpriteAsset
I was trying to do the same thing and found out that there actually is a way to do this. Instead of adding the embed tag above a variable like so:
package
{
import flash.display.Sprite;
public class Application extends Sprite
{
[Embed(source="library.swf", symbol="Star")]
private var Star:Class;
public function Application()
{
var star:Sprite = new Star();
addChild(star);
}
}
}
...which only allows you to type it as Sprite or MovieClip. You can instead create the Star class and add the embed tag directly above the class declaration:
package
{
import flash.display.*;
[Embed(source="library.swf", symbol="Star")]
public class Star extends Sprite
{
public function Star()
{
}
}
}
and implement it in the main class with:
package
{
import flash.display.Sprite;
public class Application extends Sprite
{
private var star:Star;
public function Application()
{
star = new Star();
addChild(star);
}
}
}
Now you have a custom Star class you can do anything you want with associated with an embedded asset!
Yay! Problem solved.
Here's an example that uses a few Flash symbol assets this way. Source code at the bottom, don't need to read.
http://www.jessewarden.com/archives/2006/12/integrating_a_f.html
Nice. Now, I wonder is there a way to make a regular movieclip already placed on the timeline into a Class? Like say I decide that a movieclip I have placed already now should be an instance of a class, can I do that in actionscript at runtime?
Am i missing what the advantage of this
var star:Sprite = new Star();
addChild(star);
over this
star = new Star();
addChild(star);
would be? Unless you have some code in the Star class i can't see any advantage, as it is just a Sprite or MovieClip?
Tink:
That is the advantage. I didn't add anything above in the Star class, but I was just showing that it is possible. It's much more advantageous and flexible to be able to associate a custom class with a library asset then just Sprite or MovieClip. Let your mind run free. :)
Thanks for pointing out this method but how do you handle ActionScript tht would be on the timeline of Star? For example commands like gotoAndPlay() or Stop() seem to be ignored?
Hey, great find. helped me allot in figuring out this embed tag. One question if anyone can help, can i do the same thing with an xml document? thanks for your help.
Updated link:
http://jessewarden.com/2006/12/integrating-a-flash-interface-into-flex-2.html
I get the following error
C:\AS3_fun\classes\Star.as(5): col: 4: Error: unable to resolve 'library.swf' for transcoding
Im not sure if I was to set the linkage identifier in flash IDE and the name of the symbol in the library ???
Awesome. Thank you.
still get same error usgn flex compiler different project now
Error: unable to resolve 'graphics_library.swf' for transcoding
[Embed(source="graphics_library.swf", symbol="Clip0")]
[Embed(source = '../assets/graphics_library.swf', symbol = 'Clip0')]
that fixed it. Also not sure if its good to call the instance the same a s the class.
Hi, thanx for the help, I was having trouble with this embed tag...
But is it possible to prevent flash from compressing the embedded images, just as I would in the properties panel of my flash library item. an undocumented embed parameter, perhaps?
thanks again:)