Saturday, November 15, 2008

Finding the Number of External Variables.

As I've been writing my first real app with flash, I've gotten into sending and receiving data to and from flash. There is quite a bit of information out there about it. In Actionscript 3, the preferred method is to do something like this:



var loader:URLLoader = new URLLoader(new URLRequest("http://url_with_data_in_it.com"));

loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dothisfunction);

function dothisfunction(myevent:Event){
     var loader2:URLLoader = URLLoader(myevent.target);
     var variables:URLVariables = new URLVariables();
     loader2.data = variables;
}


And within the dothisfunction() you can access the variables in an object.
But often you're not just passing a single variable. You're passing quite a few. If you don't know how many items you have (and therefore not the names of all the items), you'll need a reference for the loop that you want to run through. Most places I've seen simply pass the number of variables along with the other data. If for some reason, that doesn't work, you can do the following:

     var loader2:URLLoader = URLLoader(myevent.target);

     var vargroup:String = loader2.data;
     var params:Array = vargroup.split("&");
     var paramCount = params.length;

That's it. With the paramCount you can build a for loop that'll give you the length you need.

No comments: