2

I have a swf that I call as

/SWFUploader/upload.swf?single=true

Then in ActionScript 3 I read in the value. But it's not working. Here's my test code (the first block is taken from http://blogs.adobe.com/pdehaan/2006/07/using_flashvars_with_actionscr.html ):

var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (keyStr in paramObj) {
    valueStr = String(paramObj[keyStr]);
    trace(keyStr + " = " + valueStr);
}

var isSingle:Boolean = this.loaderInfo.parameters.single as Boolean;
var isSingle1:Boolean = this.loaderInfo.parameters['single'] as Boolean;
var isSingle2:Boolean = LoaderInfo(this.root.loaderInfo).parameters['single'] as Boolean;
var isSingle3:Boolean = LoaderInfo(this.root.loaderInfo).parameters.single as Boolean;
trace(isSingle + ", " + isSingle1 + ", " + isSingle2 + ", " + isSingle3);

And frustratingly this is the resulting two lines that are traced:

single = true
false, false, false, false

What am I doing wrong?

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • Ah, it's a casting problem. If I replace some of the above code with String in place of Boolean the traces all come out "true". So my question may well be just: How do I cast the parameter value String:"true" to Boolean:true in ActionScript (and why doesn't it happen anyway)? – dumbledad Jan 26 '12 at 11:39
  • if you want to get the type of an variable try this `trace(typeof(isSingle));` – mgraph Jan 26 '12 at 11:47
  • Thanks mgraph, that's a useful piece of ActionScript for debugging this kind of problem. – dumbledad Jan 26 '12 at 11:53

1 Answers1

6

In ActionScript not empty and not null String is casted to Boolean true.

var singleStr:String = this.loaderInfo.parameters.single;
var singleBool:Boolean = singleStr == "true";
Timofei Davydik
  • 7,244
  • 7
  • 33
  • 59