I have an engine I created a while back that loads objects into a container based on XML data. A really quick example of the XML would be like this:
<level>
<object cname="enemies.Robot">
<pos x="200" y="400" layer="mobiles" />
</object>
<object cname="Player">
<pos x="12" y="89" layer="mobiles" />
</object>
</level>
I have a class Environment that has a method loadLevel(data:XML) which I parse the XML through, then the function runs through the XML finding all object nodes and uses getDefinitionByName to determine which Object I want to create based on object.@cname.
From here, I have to manually define each property based on the XML like so;
obj.x = xml.pos.@x;
obj.y = xml.pos.@y;
etc.
I was wondering if there's an inbuilt method for setting a property based on a String. By this I mean something like so:
var mc:MovieClip = new MovieClip();
mc.someInbuiltFunctionThatSetsAProperty("alpha", 0.5);
This way I could change my XML to be more like so:
<object cname="Player">
<props>
<x>200</x>
<y>221</y>
<alpha>7834</alpha>
<health>Something</health>
<power>3</power>
</props>
</object>
And iterate through all the children of props to set all of my properties on the fly.
I know if I create an Object and set properties within it like so:
var obj:Object =
{
var1: "hello",
var2: "there",
name: "marty"
};
That you can then iterate through names/values using the for(String in Object) loop like this:
var i:String;
for(i in obj)
{
trace(i + ": " + obj[i]);
}
/**
* Output:
* var1: hello
* var2: there
* name: marty
*/
Is there maybe something even similar to that?
Surely there's a way, as here's an example of identifying a property using a String:
var ar:Array = [new MovieClip(), new MovieClip()];
ar.sortOn("alpha", Array.ASCENDING);
So just to make my question more to-the-point: I want to be able to get and set properties that I can identify using a String.
<flos.gui.GUIControl><flash.text.TextField text="hello" autoHeight="true" dockMargins="10,10,10,10" dockStyle="top" wrapStyle="stretchwidthaligntop"></flos.gui.GUIControl>XML nodes that resolve to DisplayObjectContainers allow child nodes to be processed and added, and any node can have special child nodes 'props' and 'constructor'.<flash.display.MovieClip name="Clip1" x="0" y="0"><props names="scrollRect,scale9Grid"><rect width="100" height="100"/><rect x="10" y="10" width="80 y="80"/></props></flash.display.MovieClip>such that child nodes of "props" will be instantiated and assigned in order to the list of "names" specified in "names" attribute of the props node. It supports type name aliases, so I can use "rect" instead of "flash.display.Rectangle" for example.<constructor 0="" 3=""><props names="2"><rect width="10" height="10" /></props></constructor>, which builds an array object that will passed to the constructor via getDefinitionByName + Class object, and a helper function that hard codes 20 different paths that construct the class with anywhere from 1 to 20 constructor parameters (since you can't just call "apply" on a Class object). All attributes are also intelligently processed, it checks hasOwnProperty, automatically interprets numbers and automatically parses to strong types for known properties.