0

I have a Flash developer I'm working with. This person is building a tool in AS2 that will provide an interface that will send voting data (firstname, lastname, email address, votes (there are 100 items in categories and users will be able to choose some subset to declare "best").

All fair enough, Flash dev will POST data to a PHP app I will develop, and I will store the data in MySQL. This Flash dev has not done a great deal of work with databases or web apps.

I want to return data back to the Flash application. I want to be able to return "email address invalid" or "problem connecting to database" or "vote information accepted" messages. My instinct is to want to send back JSON or XML data. But I'm wondering if there are tools in AS2 to easily consume such responses.

I would like to see some "Hello World" type examples of AS2 code that consumes JSON or XML data so I can get the Flash app and the PHP app interacting well. My understanding is AMF is not on the table because it's AS2, but I'm open to what will work on both ends given the constraint of it being AS2.

2
  • AMF works fine with AS2, you just have to make sure you're using AMF0 rather than AMF3. Commented Jun 26, 2009 at 15:18
  • to respond to your comment below: XML is a built in class Commented Jun 26, 2009 at 18:07

1 Answer 1

1

Below should give you an example.

XML:

<alldots>
  <dotname id="bigDot" color="0xff0000" url="http://www.fletchermartin.com/" photos="8" />
  <dotname id="otherDot" color="0x000066" url="http://www.ajc.com/" photos="8" />
  <dotname id="thirdDot" color="0xCC0099" url="http://www.tiffanybbrown.com/" photos="0" />
</alldots>

AS2 Code

var dots:XML = new XML();
dots.load('bigdot.xml');

dots.onLoad = function(success:Boolean){
    if(success){
        if(dots.status == 0){
             var dotsToXMLString:String = new String(); // initializes a new string variable
             dotsToXMLString = dots.toString();         // converts dots XML object to a string and stores it in dotsToXMLString.

             var dotsXML:XML = new XML(dotsToXMLString);// creates new XML object with the string contents from above.
             dotsXML.parseXML(dotsToXMLString);         // parses the string from above.

             var dotsNodes:Object = dotsXML.firstChild; // Saves the firstChild (in this case, the outermost element) as an object
             var dotsNodesChildren:Object = dotsNodes.childNodes; // Saves the childNodes of firstChild as an object

             for(i=0;i<dotsNodesChildren.length;i++){
                var newObj:Object = dotsNodes.childNodes[i].attributes.id; // creates a new object out of the child node's id.

                var newObjColor:Color = new Color(newObj); // creates a new color object with newObj as its target
                var theColor:Number = dotsNodes.childNodes[i].attributes.color; //retrieves the hex code value (number) of the attribute color

                newObjColor.setRGB(theColor); // sets the RGB value of newObjColor.
            }

        } else {
            trace("Problem parsing XML.");
        }
    } else{
        trace("Could not load XML");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

A lot of the code above is not needed. The onLoad method automatically parses the XML and loads it. Rather than creating the dotsXML object, you can internally just refer to 'this' - since this code is part of the dots XML object.
Alas i'm not a AS2 Programmer, but found a code snipit to try and help out as there were no other answers. If i was to work with AS2 i would find the above code (personally) a good starting point to move forward and potentially improve upon. I appreciate your critique though :) it's how we all get better at this game we call programming :))
No worries! It is a good start. The big problem is just that AS2s XML class does way too much work. A single class shouldn't handle HTTP requests, loading data over HTTP, and parsing/traversing XML! Adobe has fixed the problem (and then some!) with AS3.
Is there any additional code that needs to be added for an AS2 programmer to call "new XML" constructor, or is that a built-in available inside Flash environment by default?
you can load the raw source with LoadVars, and get it in the onData callback, and then parse it into an XML ... almost as in AS3 ... i think, it's a nice shortcut ... first thing i did in AS3, when it came to loading XML, was to write myself an XMLLoader, so i don't have to write n lines, just to get an XML loaded ... :)

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.