I created a custom ActionScript class for converting a String value to Boolean value. Why would I need that? In this case, I have an application that imports customization parameters from an XML file. The XML parameters can accept any value such as “true” or “false”, however, they are all naturally imported as String data types, and ActionScript needs some of them as Boolean. Also, the custom XML parameters can be more user friendly if we can also accept “yes”,”no”, “on”, “off”, “1″, “o”, “y”, “n” etc (instead of clunky “true” and “false”).
Here is the class:
package support {
public class BoolConv {
// converts string to boolean value
public function BoolConv() {
}
public function convertString(boolString:String):Boolean {
switch(boolString) {
case "1":
case "true":
case "yes":
case "y":
case "on":
return true;
case "0":
case "false":
case "no":
case "n":
case "off":
return false;
default:
return Boolean(BoolConv);
}
}
}
}
And here is a sample implementation:
import support.BoolConv; // custom parameters (defaults) // note : a bunch of different data types public var startZoomCoordinates:String = "0,0,0" public var startZoomLevel:Number = 1; public var startZoomTransition:Boolean = true; public var frame:String = "normal"; public var bordersUI:Boolean = true; public var bordersActive:Boolean = false; public var zoomSliderUI:Boolean = true; public var zoomMenuUI:Boolean = true; public var listMenuUI:Boolean = true; public var listMenuActive:Boolean = false; public var legendUI:Boolean = true; public var legendActive:Boolean = false; public var infoWinUI:Boolean = false; public var linkOut:String = "_blank"; public var backgroundClr:uint = 0x000000; // ///////////////////////////////////////////////////// // ... a bunch of code that imports XML and stuff ... // ///////////////////////////////////////////////////// // create and instance of Boolean Conversion Class var bc = new BoolConv(); globalPlacemarkSize = Number(xmlPlacemark.@globalPlacemarkSize.toString()); startZoomCoordinates = xmlPlacemark.@startZoomCoordinates.toString(); startZoomLevel = Number(xmlPlacemark.@startZoomLevel.toString()); startZoomTransition = bc.convertString(xmlPlacemark.@startZoomCoordinates.toString()); frame = xmlPlacemark.@frame.toString(); var bgc = xmlPlacemark.@backgroundColor.toString(); bordersUI = bc.convertString(xmlPlacemark.@bordersUI.toString()); bordersActive = bc.convertString(xmlPlacemark.@bordersActive.toString()); zoomSliderUI = bc.convertString(xmlPlacemark.@zoomSliderUI.toString()); zoomMenuUI = bc.convertString(xmlPlacemark.@zoomMenuUI.toString()); listMenuUI = bc.convertString(xmlPlacemark.@listMenuUI.toString()); listMenuActive = bc.convertString(xmlPlacemark.@listMenuActive.toString()); legendUI = bc.convertString(xmlPlacemark.@legendUI.toString()); legendActive = bc.convertString(xmlPlacemark.@legendActive.toString()); infoWinUI = bc.convertString(xmlPlacemark.@infoWinUI.toString()); linkOut = xmlPlacemark.@linkOut.toString();


Social Links