Thursday, July 3, 2008

Assigning a Single Class to Multiple Library Symbols

So, in AS2 I used to use one single class with multiple different library items (for example, if you were creating an asteroid field and you had multiple asteroid symbols in your library, but wanted them all to behave the same using one single class). In AS3 if you try this, you get an error: "Please enter a unique class name that is not associated with other library symbols".

I finally came across a forum that had a simple elegant answer. In the Library/Linkage you simply put the name of your class into the Base Class field instead of the Class field. 

So I tried that, and I still got an error and after tinkering a bit, I realized that something also needed to be in the Class field for everything to work. If you leave the Class filed blank, you will get the same error quoted above. To fix this, just type anything into the field. When you click OK to leave the Linkage dialog, flash will warn you that the gobbldygook you typed into the Class field doesnt exist as a class - but just ignore it. 

You should now be able to have multiple different symbols using the same class. Just do the above for each item in the library.


4 comments:

Anonymous said...

Thank you for your post. Very helpful.
One thing I worth mentioning for people doing this for the first time is that the "gobbldygook" Class is what will be instantiated in your code. So it might help to name it something descriptive.

Just 2¢ worth.

Anonymous said...

Dear Sir,

I found your article, about adding multiple symbols to Stage, I don't know what's wrong with my code ...but the error message keep coming up :( , could you please describe it a little bit more details, ok here'e my situation...

I have a fla file ..let's just name it new.fla

ok ..under new.fla there are 2 Movie clip symbols, let's just name it "stars" and "moon" with the class id "star" and "moon" too, with this conditions I try to attach them randomly to stage,
ok here's the code I've put at first frame of my new.fla

//--------------------------------

var skyObjects:Array = new Array(star,moon);

var rNum:Number = Math.floor(Math.random()*skyObjects.length)
trace (rNum)
var cobj:MovieClip = skyObjects[rNum]
addChild(cobj)

//-------------------------------

ok ...I do also followed your step by providing individual external class for each MovieClip symbols ...and name it exactly the same star.as and moon.as..
and change the base class inside symbol linkage to star and moon , and left something different with the class field such as "s" for star and "m" for "moon"...

ok I'll paste you the code inside star.as
//---------------------------------
package {
import flash.display.MovieClip;
public class star extends MovieClip {
public function star() {
trace("star");
}
}
}

//-------------------------

ok ...but I already that ...and it always throw error messages ...could you please give me a hints about this issue ...any tips would be helpfull

Thanks in Advance
Ruddy

Unknown said...

Is this really the best thing to do? It seems like Adobe doesn't want you to do it this way anymore.

Anonymous said...

Thanks, mate.