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.

AS2 Refresher: sendAndLoad with LoadVars

Michael Becker

Michael has humbly acquired extensive design experience across multiple industry disciplines during his career in new media. In addition to his passion for web and Flash development, he has an uncanny penchant for zombies and robots. Let's be honest, who doesn't?

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 a response.

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 with LoadVars Snippet:

  1. // variables
  2. //——————————————————————————–
  3.  
  4. // set a ref to hold your URL
  5. var myURL:String = "test.html";
  6.  
  7. // set a new LoadVars object
  8. var result_lv:LoadVars = new LoadVars();
  9.  
  10. // function that fires when a result is received from the server
  11. result_lv.onLoad = function(success:Boolean) {
  12.    
  13.    if (success) {
  14.       
  15.       trace ("——————————————————-")
  16.       trace (">>>>> SUCCESS!");
  17.       trace ("——————————————————-")
  18.       
  19.    } else {
  20.       
  21.       trace ("——————————————————-")
  22.       trace (">>>>> ERROR!");
  23.       trace ("——————————————————-")
  24.    }
  25. };
  26.  
  27. // instantiate a new LoadVars object
  28. var myLoadVars:LoadVars = new LoadVars();
  29.    
  30.    // assign parameters to the object
  31.    myLoadVars.var1 = "Robots";
  32.    myLoadVars.var2 = "Aliens";
  33.    
  34.    // send our variables to the value in myURL and await the response which will be caught by ‘result_lv.onLoad’
  35.    myLoadVars.sendAndLoad(myURL, result_lv, "POST");
  36.  
  37. // make sure your URL is configured correctly by tracing out the compiled syntax
  38. 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

Category: Flash AS2 | Tags: , , ,


Leave a Reply



Back to top