dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: taking xml literally

  1. #1
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707

    taking xml literally

    let's say i load in some XML:
    <root>
    <pos>
    <x>stage.stageWidth</x>
    </pos>
    </root>

    And let's say i have a movieclip i would like to position by way of the xml value. This is simple if my value is numeric. But in this case, it's a relative position based on an unknown. Is there a way to import this value from xml, and later in my code , state:

    mc.x=xml.pos.x

    ??

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    stage.stageWidth is an expression. You would need some way to evaluate that expression since coming from XML it is only a string once it reaches Flash Player. In JavaScript and AS2 you could use eval (eval being limited in AS2 but I think would still work in this case). AS3, however, has no eval. What you would need to do then is create a parser that can read that string and determine the value it expects. This would mean spliting the string by "." and getting the value of each part from the object resolved by the prior. Depending on what other values you have like this in your XML, you may need to do even more work than that. And actually, in this case, since you're using stage, you have to make sure you reference the stage off an object on the active display list or stage will be inaccessible.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Not easily. This is where "eval" would come in handy.

    If you know that the first part of the value is going to refer to a property of the current "this" object, you could split your value into parts based on ".". Then, you could use the [] access to get the properties corresponding to those names.
    Code:
    var path:String = "stage.stageWidth"; //populated from xml, in your case
    var pathParts:Array = path.split("."); //["stage", "stageWidth"]
    var obj:Object = this;
    for (var i:int = 0; i < pathParts.length; i++){
      obj = obj[pathParts[i]];
    }
    //obj should be a Number representing stageWidth
    This may or may not work without tweaking. It also assumes you're never going to typo anything.

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    I was just thinking of something similar to Flax's method - my alternate idea (somewhat more hacky, but easier to implement), would be a simple lookup array something like:

    PHP Code:
    var lookup:Array = new Array();
    lookup['stage.stageWidth'] = stage.stageWidth;

    //  ...

    mc.lookup[xml.pos.x]; 
    Beware that putting a primitive into the array will just give you a copy of the value instead of a link to the property (eg. if stageWidth changes, you'd need to update the array).

  5. #5
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    thanks guys...these out of the box ideas are good enough

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center