Flixel Forums

development => help => Topic started by: Kurai on Sat, Aug 27, 2011

Title: Dynamic graphic class name from XML
Post by: Kurai on Sat, Aug 27, 2011
Hi there.
Let's say I've got an XML defining all the objects in my game. I want to put in that XML the graphic class of each object, like that:
Code: [Select]
<prodblock>
<name>Cans</name>
<graphClass>ImgBlock0</graphClass>
<capacity>5</capacity>
<!--How much the carts fill itself when taken-->
<productRate>1</productRate>
</prodblock>
Then I want to make my sprite class read the XML, choose the right element and then load the right image class (obviously all the possible graphics are embedded with a matching class name.

How can I tell loadGraphics to use a variable instead the name of the class? Is it even possible, or do I have to find a workaround?
Title: Re: Dynamic graphic class name from XML
Post by: NateTheGreatt on Sat, Aug 27, 2011
I'm still learning dynamic programming methods. How about a nice switch statement instead :D?

Code: [Select]
var image:String = xml.obj.img;
switch(image) {
case 'ImgBlock01':
loadGraphic(imgBlock01);
        break;
case 'ImgBlock02':
loadGraphic(imgBlock02);
        break;
}

Something like that?
Title: Re: Dynamic graphic class name from XML
Post by: John Hutchinson (Johntron247) on Sat, Aug 27, 2011
AS3 most likely provides a "right" way to do this, but I'm clueless as to how.  I'll be needing to do this in a few days so if I figure it out then I'll post it here.  If not then I'll probably just use a switch statement approach and then refactor the code when I figure out the real way.
Title: Re: Dynamic graphic class name from XML
Post by: John Hutchinson (Johntron247) on Sun, Aug 28, 2011
Yay!  Ok, I haven't tested this yet but this definitely looks like what we're looking for, and even includes information about using parameters along with the dynamic instantiation!

Code: [Select]
private var forCompiler:Person;

private function cc():void
{
        var obj:Object = createInstance("Person", ["bob", 30]);
}
 
public function createInstance(className:String, args:Array):Object
{
        var myClass:Class = getDefinitionByName(className) as Class;
        var instance:Object = new myClass();
        instance.initArgs.apply(null, args);
        return instance;
}

More info here... http://nwebb.co.uk/blog/?p=186 (http://nwebb.co.uk/blog/?p=186)
Title: Re: Dynamic graphic class name from XML
Post by: John Hutchinson (Johntron247) on Sun, Aug 28, 2011
That's probably too involved for what you need though.  For your particular situation you can probably use something like:

Code: [Select]
loadGraphic(getDefinitionByName(xml.prodblock.graphClass) as Class);
Again, I haven't tested this.  It's just a guess but I think it should work.  Please let me know how it goes... like I said, I'll be doing something very similar in a couple days so any useful information would be appreciated.

more info: http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/ (http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/)
Title: Re: Dynamic graphic class name from XML
Post by: NateTheGreatt on Thu, Sep 1, 2011
That's probably too involved for what you need though.  For your particular situation you can probably use something like:

Code: [Select]
loadGraphic(getDefinitionByName(xml.prodblock.graphClass) as Class);
Again, I haven't tested this.  It's just a guess but I think it should work.  Please let me know how it goes... like I said, I'll be doing something very similar in a couple days so any useful information would be appreciated.

more info: http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/ (http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/)

I attempted this. It was not successful and resulted in frozen code after the loadGraphic() call :C

Edit:

I talked to strega_ on the flixel forums and he explained a very neat way to do this!!

Define your embedded image variable as a public static variable (preferably in a registry) and then do the following:

Code: [Select]
loadGraphic(Registry["NameOfImageClass"]);
E.g. with XML:

Code: [Select]
object.loadGraphic(Registry[node.@imgName]);
Title: Re: Dynamic graphic class name from XML
Post by: John Hutchinson (Johntron247) on Fri, Sep 2, 2011
Thanks Nate!  That's a great tip!
My guess would be that the problem is due to flixel's preloader.  I tried it out in a test app without flixel and it worked great.

I had to push this loading task back a bit due to heavy school-load, but when I do get around to testing it out in flixel, likely including your tip, then I post back about how it went.  Thanks again man.  I likely would have gotten stuck on that one.