Converting Strings To Booleans
If you've downloaded my Image Slider, you may have looked at the Main.as file and noticed that I was using a utility class to convert the loaded string values for things that are actually booleans from the XML. I was originally loading them and I couldn't figure out why when I traced out what was supposed to be a Boolean value set to "true" it was tracing out as "false". The reason is that when you load the String "true" into Flash, "true" is not a Boolean but a String, so when you test for it it returns "false". I knew it was a scoping issue but Tom Stanley explained to me why this was happening. Here is a quick rundown of how to do this.
Lets take a look at our XML:
-
<options>
-
<background>images/background.jpg</background>
-
<mainImage width="320" height="480" />
-
<name enabled="false" fontSize="20" yPosition="650" />
-
<leftArrow img="images/leftArrow.png" x="72" y="397" />
-
<rightArrow img="images/rightArrow.png" x="1150" y="397" />
-
<effects useDropShadow="false" useReflection="true" />
-
</options>
As you can see here, I wanted to pull out the value of useDropShadow and useReflection and use them as Booleans in my code. Tom passed along a handy little method my way that allowed me to do this rather quickly.
-
public static function stringToBoolean($string:String):Boolean
-
{
-
return ($string.toLowerCase() == "true" || $string.toLowerCase() == "1");
-
}
Essentially what this does is you pass it your String value that you want to test against. The method converts the string to lowercase and checks if it is "true" or if it is equal to "1", which is the numerical representation of "true" ("0" being "false"). It then returns either of these therefore returning a Boolean that is the test case in the appropriate situation.
To use the method, you just simply call it as such:
-
app.useDropShadow = StringUtils.stringToBoolean(images.options.effects.@useDropShadow);
-
app.useReflection = StringUtils.stringToBoolean(images.options.effects.@useReflection);
This, of course, implies that the stringToBoolean method is in a class called StringUtils and your XML is structured as the example was except inside of a root node called "images". Then we juse use E4X in AS3 to parse the proper node and get the value back that we are wanting to retrieve. In this case, useDropShadow and useReflection are Boolean variables inside of my Application class which is represented here by the variable "app":
-
var app:Application = Application.getInstance();
Although I have shown here an AS3 example of it, you could also use the same method in AS2 without having to change anything. The only thing that would change is how you parse the XML.


(3 votes, average: 3.67 out of 5)









Interesting. A while ago I created a context menu gallery and had my xml looking similar to yours. I had to pass in a boolean but did not have the problem you had. None the less I did not know that a boolean had a numerical representation. I am going to have to update my string util class :). Below is the xml I was using and a piece of my code so you can see how I called it in.
Thinking about it maybe it has to do that I assigned a variable to the xml element instead of just trying to plug in String( item.attribute( "enabled" )[0] ) which would probably not work since I am casting as a string.
Josh,
Your enabled/separator vars work that way because you're testing against what you are bringing in from the XML, not grabbing what you brought in from the XML. your Boolean tests an expression not just pulls the Boolean value out of the XML. Make sense?
thank you for the information. and where can i get this utility class called StringUtils?
back,
just put that method in a class called StringUtils and you should be good to go. That's really all I have in that class so I didn't bother posting it.
now it works!
one question:
is it possible to turn of the shadows over the images (who make the images in the background darker)? if you don't have images with a square shape, it looks strange with the shadows...
i have seen that with "img.overlay._alpha" it is possible to turn off the shadows at the beginning. but when i click on the arrows and the images change, the shadows are again visible.
did you mean to post this in the image slider comments?
yes, sorry!