Robots Rule | MindSculpt.net

New media has met its match.

The MindSculpt Design Syndicate focuses on code that works, from CSS, XHTML and JavaScript tips and tutorials to Flash and ActionScript how-to's. Robots not included.

Category: XML


AS2 Refresher: sendAndLoad XML

January 12th, 2010 — 11:23am

The following article assumes you have a solid understanding of Flash AS2 and the Flash interface and skips all basic Flash setup instructions and explanations in effort to provide you with quick, concise information. It also assumes you are dealing with a server-side script that is set up to receive parameters and send back XML.

The AS2 refresher series aims to consolidate basic, everyday snippets for the most common tasks used by Flash developers working with legacy files. Basic explanations are included in the comments, since after all, who wants to read through a bunch of crap when you are just after the code? Enjoy.

AS2 sendAndLoad XML Snippet:

  1. // variables
  2. //——————————————————————————–
  3.  
  4. // set a ref to hold your URL
  5. var myURL:String = "test.html";
  6.  
  7. // set a new XML object
  8. var myXML:XML = new XML();
  9.    myXML.ignoreWhite = true;     
  10.    myXML.onLoad = onMyXMLLoad;   
  11.  
  12. // functions
  13. //——————————————————————————–
  14.  
  15. // runs when the xml is successfully loaded from the url
  16. function onMyXMLLoad(success:Boolean) {
  17.    
  18.    if (success) {
  19.       
  20.       trace("XML LOADED FROM SERVER");
  21.       
  22.       // xml has been loaded, call init()
  23.       init();
  24.       
  25.    } else {
  26.       
  27.       trace("ERROR: XML NOT LOADED FROM SERVER");        
  28.    }
  29. }
  30.  
  31. // function that runs after the XML has been returned and loaded from the server
  32. function init():Void {
  33.    
  34.    trace("init() called");
  35.    trace("");
  36.    
  37.    trace("myXML: \n" + myXML);
  38. }
  39.  
  40. // load vars
  41. //——————————————————————————–
  42.  
  43. // set a new LoadVars object
  44. var myLoadVars:LoadVars = new LoadVars();
  45.  
  46.    // assign parameters to the object
  47.    myLoadVars.var1 = "Michael";
  48.    myLoadVars.var2 = "Becker";
  49.  
  50. // send our variables to the value in myURL and await the response which will be stored in myXML
  51. myLoadVars.sendAndLoad(myURL, myXML, "POST");
  52.  
  53. // make sure your URL is configured correctly by tracing out the compiled syntax
  54. trace("posting to: "+ myURL + "?var1=" + myLoadVars.var1 + "&var2=" + myLoadVars.var2);
Share this post:
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • Google Bookmarks
  • Yahoo! Buzz

Comment » | Flash AS2, XML

Back to top