I have a class called Assets, that basically just has all the stuff I want embedded in it and if I want to use an image I'll just go Assets.imageName. Looks like this inside Assets:
[Embed(source = "assets/girlspritesheet.png")]
public static var girlSheet:Class;
That's fine, except that I've decided to load characters from XML. I can store the string of what the asset is called there, but actually getting that asset from that string is proving difficult.
I hear good things about getDefinitionByName and getQualifiedClassName for this sort of thing, but extensive poking the code and googling hasn't helped, and I'm not sure that it's the right way after all (it creates an instance of a class from the name? Not really what I want given that they're static, I think...)
My loadCharacter method currently looks like this:
public function loadCharacter(charName:String):void
{
var charList:XMLList = charXML.children();
for each (var char:XML in charList)
{
if (char.name() == charName)
{
characters.add(new Character((getDefinitionByName("Assets."+char.attribute("asset")) as Class), int(char.attribute("x")),
int(char.attribute("y")), int(char.attribute("width")), int(char.attribute("height"))));
spritesLayer.add(characters[characters.length - 1]);
}
}
}
Which crashed messily insisting that the variable 'girlSheet' doesn't exist. It runs using getQualifiedClassName instead of getDefinitionByName, but doesn't actually load the graphic.
So, is there a smart way of doing this, or do I start on the dodgy solutions?