1

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.

3
  • Crazy. I built something similar, except mine just uses the node names as the class name and attributes as property values (e.g. <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'. Commented Jan 22, 2014 at 8:43
  • The props node requires a set of named properties, so that the child nodes will be constructed as typed objects and assigned to those properties like so: <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. Commented Jan 22, 2014 at 8:47
  • It also supports a constructor subnode <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. Commented Jan 22, 2014 at 8:50

2 Answers 2

10

Why not using ["string property"] notation :

var mc:MovieClip=new MovieClip()
mc["alpha"] = 0.5            // setter
var alpha:Number=mc["alpha"] // getter
Sign up to request clarification or add additional context in comments.

1 Comment

biggest facepalm in history. Pure genius.
1

I'm not quite clear on what it is you're looking for exactly, but I have a general sense of what you're getting at and have a few suggestions for you. First, have a look at the documentation for the Object class in the AS3 Language Reference. Look specifically at the propertyIsEnumerable() and setPropertyIsEnumerable() methods. I think that's what you're asking about.

If not, you might want to look into the behavior of dynamic classes, which let you add variables to an object on the fly.

1 Comment

Good note on dynamic classes, however the aim was to alter properties that are already in-built such as x, y, alpha. Also I have a particular hatred towards the use of dynamic classes :P +1 for the propertyIsEnumerable reference though - this will surely come in handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.