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:

…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:

and implement it in the main class with:

Now you have a custom Star class you can do anything you want with associated with an embedded asset!
Yay! Problem solved.

About the author

Mark

View all posts

23 Comments

  • 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.

  • 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 ???

  • 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:)

  • Thanks for this tip Mark, I am having the following issue:

    My movieclip makes use of other movieclips in the library.swf (like buttons, etc), when I try to complie my “application.swf” I get an error for each movieclip that the Star class references, saying:

    1120: Access of undefined property myMask.

    My Star class has a line in the constructor that reads:

    myMask.cacheAsBitmap = true;

    Would you happen to know why this happens? Thanks in advance for your support

  • I am trying to do this but it doesnt work, I keep getting:

    “App_Picture.as(10): col: 42 Error: The definition of base class BitmapAsset was not found.”

    Any ideas?

  • Thank you, this is incredibly useful!

    @Seb: In case you never got it working: I got the same error and fixed it by declaring the movieclip as a variable, like public var myMask:MovieClip; in the class.

  • Hello and thank you for your post, of all those related to this issue, it is the one I like the most.

    Do you think you could post or email me an example including the source code and the movie to produce library.swf?

    I am asking because I have tried many different combinations and I cannot make this work.

    It would be great if you could post or email me a zip file with everything needed.

    Thanks.

  • Hi.
    I have already associtated the symbol with the class I use in the embedding swf. So when I try to export the library.swf Flash complains:exception during transcoding: lib_shell.swf (The process cannot access the file because another process uses it). That is why the embed tag is also recognized by Flash.. problem! How can I make Flash ignore the embed tag?

  • Hello! Thank you for a great post. I use this association method in all my Flash/Flex projects and it worked flawlessly until now :(.

    The concern is that I have three projects which share same classes. So I created Flex-library-project and placed classes to share into it’s packages structure. Then I created three project interested in those classes from Flex-library-project, pointed that Flex-library-project in Projects properties->ActionScript Build Path->Library path->Add project dialogue. Now I have classes visible and can create them like:

    package {
    import flash.display.Sprite;
    public class TestProject extends Sprite {
    public function TestProject() {
    var pItem:TestSprite = new TestSprite();
    addChild(pItem);
    }
    }
    }

    package {
    import flash.display.MovieClip;
    import flash.display.Sprite;

    [Embed(source=’editor.swf’, symbol=’MarkerState’)]
    public class TestSprite extends Sprite{

    public var box:MovieClip;
    public function TestSprite() {
    }
    }
    }
    The problem is that there’s no “box” movieclip in TestSprite I addChild in my main project from the library project. Everything worked fine until I moved TestSprite to that Flex Library Project.
    Haven’t found the answer even after hours of heavy goggling around.
    Please advice.

  • [Embed(source=’/images/page9.swf’, symbol=”MYPage”)]
    public static const MYPage:Class;

    I got this error while embed the swf in Flash IDE with Flex Lib included in Lib path. And using Player 10.

    definition for symbol ‘MYPage’ not found
    Unable to transcode /images/page9.swf.

Leave a Reply

Your email address will not be published. Required fields are marked *