Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411615 Posts in 69390 Topics- by 58447 Members - Latest Member: sinsofsven

May 10, 2024, 05:56:02 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Dynamically accessing embedded assets
Pages: [1]
Print
Author Topic: Dynamically accessing embedded assets  (Read 1447 times)
Aik
Level 6
*


View Profile
« on: December 09, 2009, 06:11:41 PM »

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:
Code:
		
[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:
Code:
		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?
Logged
Draknek
Level 6
*


"Alan Hazelden" for short


View Profile WWW
« Reply #1 on: December 10, 2009, 02:28:25 AM »

getDefinitionByName looks up a class from its name. girlSheet is not the name of a class, it is the name of a variable (of type Class).

You want something like this:

Code:
var className: String = char.attribute("asset");
var charClass: Class = Assets[className]
« Last Edit: December 10, 2009, 04:01:18 AM by Draknek » Logged

grapefrukt
Level 1
*



View Profile WWW
« Reply #2 on: December 12, 2009, 03:28:24 AM »

and to get an actual instance from drakneks sample you do like this:
Code:
var className: String = char.attribute("asset");
var charClass: Class = Assets[className]

var myInstance:SuitableSuperClass = new charClass;

// you can also skip the intermediate steps
var myInstance:SuitableSuperClass = new Assets[char.@asset];
Logged
grapefrukt
Level 1
*



View Profile WWW
« Reply #3 on: December 12, 2009, 03:44:13 AM »

Looking at it some more I just had to try to refactor your code a bit. This is what I came up with (untested ofcourse):
Code:
public function loadCharacter(charName:String):void
{
for each (var char:XML in charXML[charName])
{
characters.add( new Character(Assets[char.@asset] as Class),
parseInt(char.@x),
parseInt(char.@y),
parseInt(char.@width),
parseInt(char.@height)
);
spritesLayer.add(characters[characters.length - 1]);
}
}

I've replaced all your calls to attribute with the equivalent @ syntax, which does the same thing in a more succinct manner. I'm also using the super neat E4X filtering stuff to skip that first if-clause. I removed the part where you get the children() of the xml aswell, not sure if that will fly with the rest of your code, but it did look prettier Wink
I changed all your int() casts into parseInt instead since it's less picky about the input.
Logged
Aik
Level 6
*


View Profile
« Reply #4 on: December 15, 2009, 05:47:52 PM »

Thanks a lot Smiley It seems my knowledge of AS3's syntax is rather lacking.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic