Mister PHP
Mister PHP

Reputation: 327

How to iterate the flashvars?(Actionscript)

What construction i need to use, for in, or for?

var params = stage.loaderInfo.parameters;

    for (var i in params) 
    { 
       // trace(params[i]);
       // or
       // var test = params[i];
    } 

how to do it, to get all the values?

ps: params.length - show the number of flashvars?

Upvotes: 0

Views: 333

Answers (1)

jalbee
jalbee

Reputation: 951

You should use a for-in or for-each-in loop. For-in will iterate over the keys, while for-each-in will iterate over the values. A for-in loop is probably what you want, if you want to get both the keys and values.

For example, if your flashVars look something like this:

{ userId: 100, userName: 'Mister PHP' }

A for-in loop would look something like this:

var flashVars:Object = stage.loaderInfo.parameters;

for (var key:String in flashVars) {
    var value:String = flashVars[key] as String;
    trace(key + ' = ' + value);
}

// output:
// userId = 100;
// userName = Mister PHP

While a for-each-in loop would look like:

var flashVars:Object = stage.loaderInfo.parameters;

for each (var value:String in flashVars) {
    trace(value);
}

// output:
// 100
// Mister PHP

As far as I know, there is no easy way to get the length of an object or dictionary. You would have to iterate through the object and increment a counter on each pass.

Here's a reference on loops in ActionScript

Upvotes: 3

Related Questions