A Tip on the Boolean() Function (Casting as Boolean)
When you load data from XML or text files, or retrieve values from the TextField.text property, the information you get is a string. Even if the incoming value is, say, the numeral 3 (without quotes), it’s a string when evaluated by ActionScript. Even if the value is “false” (with or without quotes), it’s a string — not a Boolean — and may appear to ActionScript as true! Whoa! That could cause a few problems. In the case of numbers, there’s an easy way to tell ActionScript what the datatype should be, and it’s called casting. You may cast a string numeral into an actual Number datatype by using the Number() function. With a text field whose instance name is money …
var looseChange:Number = 0;
looseChange = Number(money.text);
That converts the value of the money.text property to a true number, which you can verify with trace(typeof(looseChange)); — but the Boolean() function handles things differently.
In ActionScript, false is synonymous with 0 and true is synonymous with every other number. If you cast the number 0 as Boolean, you’ll see false in the Output panel.
var num:Number = 0;
trace(Boolean(num));
Same goes for any other number, except the output will be true.
var numA:Number = 1;
var numB:Number = 2;
var numC:Number = 1000;
trace(Boolean(numA));
trace(Boolean(numB));
trace(Boolean(numC));
But strings … those are different. Since Flash Player 7, when the Boolean() function sees a string, it counts the number of characters in that string. If the string is empty (zero characters), the cast resolves to false. A count of one or more characters resolves to true.
var strA:String = "0";
var strB:String = "1";
trace(Boolean(strA));
// outputs true, even though the string was 0
trace(Boolean(strB));
// outputs true, but not because the string was 1
Even the strings “true” and “false” both resolve to true, because they contain one or more characters.
So, is how can text values be cast as true Booleans? Well, you can either write your code to abide by the rules as they are — that is, make sure your XML file contains empty strings for false and non-empty strings for true — or use a conditional statement.
var str:String = "false";
var boo:Boolean;
if (str == "false") {
boo = false;
} else {
boo = true;
}
Or, to be more compact about it, use the conditional operator:
var str:String = "false";
var boo:Boolean = (str == "false") ? false : true ;
January 10th, 2007 at 3:33 pm
You can further simplify the compacted code to the following :
var str:String = “false”;
var boo1:Boolean = str == “true”; //everything but “true” is false
var boo2:Boolean = str != “false”; //everything but “false” is true
The above shows two different ways of considering it, if the string is “true”, or the string is not “false”. So by the first method (boo1), “happiness” would go to false, and by the second (boo2), it would go to true.
January 10th, 2007 at 3:42 pm
Nice! I always like the variety of your compact approaches, NSurveyor.
March 18th, 2007 at 9:52 am
Thank you so much for this post. I was nearly going mad trying to make a graph that should scale by the highest Y and X values (with XML-data). Its strange, it would draw the graphs correctly although they were based on stringdata, but as soon as i tried sort functions or >, it would compare strings. Anyway, thank you and have a good day.
March 18th, 2007 at 6:57 pm
Gaute,
Sure thing! Glad this was helpful to you.
September 6th, 2007 at 9:35 am
hei,, i dont know if you have the time to answer me this questions, but i’m a little bit deseperated….
i have this script:
var arreglo3:String =_root.btn3admintitle;
var arreglo4:String =_root.btn4minsidetitle;
var arreglo5:String =_root.btn5systemtitle;
if(arreglo3 == “falso”){
_root.btn3.titu.text = arreglo3;
}
else{
_root.mainMenu.peliMovie.attachMovie(”btn3admin”, “btn3adminvideo”, this.getNextHighestDepth(), {_x:5, _y:18});
}
if(arreglo4 == “falso”){
this.btn4.titu.text = arreglo4;
}
else{
this.mainMenu.peliMovie.attachMovie(”btn4minside”, “btn4minsidevideo”, this.getNextHighestDepth(), {_x:5, _y:54});
}
if(arreglo5 == “falso”){
this.btn5.titu.text = arreglo5;
}
else{
this.mainMenu.peliMovie.attachMovie(”btn5system”, “btn5systemvideo”, this.getNextHighestDepth(), {_x:5, _y:92});
}
what i want to do is to evaluate several times different variables, wheter if they are = to “falso” or to ather string,
The problem is that when the script runs it only read the last script… the last “if”… how can i make it to read all the 3 “if’s”…?? or more?….
thanks
alejandro
September 18th, 2007 at 9:27 am
alejandro,
Well, this is interesting. Certainly, three
ifstatements in a row will execute as well as one (or five, or ten). I wonder if yourattachMovie()references are inadvertently bringing new ActionScript into the mix?In a case like this, where the code seems to be behaving in an illogical manner, it helps to break things down into smaller pieces, to troubleshoot each step in turn. So for the time being, for example, drop all the
ifs but the first one (arreglo3) and make sure that one performs the way you expect.Incidentally, you probably don’t need the new
arreglo3variable, since you can reference_root.btn3admintitleright there in theifstatement:You may also want to put a
trace()in either clause, just to make doubly sure you know how theifstatement’s condition is being interpreted:Often by breaking down a complex goal (three
ifs) into a simpler one, you can discover mistaken assumptions on your part, or otherwise see something you missed previously.