A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: ActionScript from XML

  1. #1
    Member
    Join Date
    Dec 2001
    Posts
    30

    ActionScript from XML

    Is there a way to get small scripts written in an XML file to run in run-time? Example, I have an XML file:


    <MyScript>
    myVar:String = "Hello World!"
    </MyScript>

    <MyComponent click="myFunction()"/>


    and I would like to run the script in run-time. Much like in FLEX, but instead of compiling the scripts into the SWF, they would be defined in run-time. I've seen following example used somewhere for text in a textfield:


    <a href="asfunction:<function-name>,<parameters>"> ... </a>


    Does anyone have any experience on such things?

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Unlike some other high-level languages, like Perl and Lua, actionscript does not have a general purpose 'exec' command that will execute a string containing arbitrary actionscript.

    However, it is possible to do what you describe in a more structured way, since functions have names and you can use strings to reference named properties of an object.

    If the set of functions you want to invoke via XML have consistent calling parameters, you can do it.

    Here's an example using functions that take two numeric arguments and return a result.
    code:

    _root.add1 = function(a,b)
    {
    return a+b;
    }

    _root.multiply1 = function(a,b)
    {
    return a*b;
    }

    // the following strings might have been loaded from XML
    myFunc = "add1"; // function i want to call
    myArg1 = Number("10"); // arguments
    myArg2 = Number("12");

    result = _root[myFunc](myArg1,myArg2);
    trace(result);



    Similarly, instead of using numeric literals for my arguments, I could have used the names of properties.

    myArg1 = _root["name_of_property"];

    But there is some rigidity here - I have to make a choice - is my argument going to be a literal or a named property?

    You can't just but a bunch of freeform actionscript code in your xml and execute it

    You can't do this:

    code = "add1(10,12);"

    And you can't make reference to built-in control structures such as 'for' and 'if'.

    You have to structure things in a well-defined way.

    Nonetheless, with a little work, you could build something that would execute simple algebraic statements. But you'd have to do all the parsing yourself.

  3. #3
    Member
    Join Date
    Dec 2001
    Posts
    30
    Thanks for the reply,

    But the problem persists, if I don't know the excact format of my scripts...

    Instead I happened to come across an ASInterpreter component by Entclosure in www.flashcomponents.net, which interpretes ActionScript from a string i.e. loaded from an XML document.

    I haven't explored the capabilities yet, but it seems promising!

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