The importance of casting

Casting is a term used in the film and theater world and means that an actor will play a certain character or role. When you write AS2 scripts you are automatically a casting director except that you are not casting actors but variables. Basically we already dealt with that all the time, when we give a var a certain datatype. So why do we discuss that again here? Well, there are situations where we want to convert an object from one datatype to another. If you are familiar with Flash MX and AS1 you probably know what I mean as shown in the example below.

var var1 = "1";
var var2 = "2";
trace(var1+var2);//gives 12
var var3 = parseInt(var1);
var var4 = parseInt(var2);
trace(var3+var4);//gives 3

Here we are converting strings to numbers, which of course gives quite a different result. We run into similar situations of course when we create AS2 scripts. In the following I want to show a script I created where I had this problem. I wanted to use xml to change the textformat of text within a textbox. It was all fine and worked until I tried to use true or false to manipulate a Boolean variable. Below the xml is shown.

<?xml version="1.0"?>
<menu>
<item>
<head_italic>false</head_italic>
<head_font_color>0x000000</head_font_color>
</item>
</menu>

We have a boolean statement for italic font and a number for the textcolor. Now we look at the AS2 file.

//We need the Delegate class
import mx.utils.Delegate;
class Testfile extends MovieClip {
	//declaring some class var here
	private var head_line_1:TextField;
	private var style:TextFormat;
	private var but:MovieClip;
	//declaring var for formatting the text
	private var head_italic:Boolean;
	private var head_font_color:Number;
	//Constructor
	public function Testfile() {
	   this.init();
	}
	private function init() {
	   var thisClip:MovieClip = this;
        //loading and parsing the xml file
	   var xml_value:XML = new XML();
	   xml_value.ignoreWhite = true;
	   xml_value.onLoad = Delegate.create(this, sValues);
	   xml_value.load("preferences.xml");
	   function sValues() {
	       for (var count1 = 0; count1<=xml_value.childNodes.length-1; count1++) {
	           var values:XMLNode = xml_value.childNodes[count1];
	           sValues_2(values);
	       }
	   }
	   function sValues_2(values) {
	       for (var count2 = 0; count2<=values.childNodes.length-1; count2++) {
	           var te_width:Number = 100;
	           var values_2:XMLNode = values.childNodes[count2];
	           var sub_values:XMLNode = values_2.firstChild.nextSibling;
	           /***************BOOLEAN and number from xml**********/
	           head_italic = values_2.firstChild.firstChild.nodeValue;
	           trace("1: "+head_italic);
	           thisClip.hsi = head_italic;
	           trace("2: "+thisClip.hsi);
	           head_font_color = sub_values.firstChild.nodeValue;
	           thisClip.hfc = head_font_color;
                /*********************/
	       }
	   }
	}
	//getter-setter for boolean: italic
	public function set hsi(myItem1:Boolean):Void {
	   head_italic = myItem1;
	}
	public function get hsi():Boolean {
	   return head_italic;
	}
	//getter-setter for number:  fontcolor
	public function set hfc(myItem2:Number):Void {
	   head_font_color = myItem2;
	}
	public function get hfc():Number {
	   return head_font_color;
	}
	//headline settings and creation
	public function makeHeadline() {
	   var thisClip:MovieClip = this;
	   this.but.onPress = function() {
	       thisClip.createTfield("head_line_1", 100, 0, 10, 125, 20);
	       thisClip.head_line_1.htmlText = "Text here";
	       thisClip.myHeadstyle("head_line_1");
	   };
	}
	private function createTfield(t_name:String, n_depth:Number, x_pos:Number, 
    y_pos:Number, n_width:Number, n_height:Number):Void {
	   this.createTextField(t_name, n_depth, x_pos, y_pos, n_width, n_height);
	   this[t_name].html = true;
	   this[t_name].border = true;
	}
	private function myHeadstyle(t_name:String) {
	   style = new TextFormat();
	   style.align = "center";
        /*****Here we set the textformat*****/
	   style.italic = this.hsi;
	   style.color = this.hfc;
	   trace("3: "+this.hsi);
	   trace(typeof(this.hsi));
	   trace("4: "+style.italic);
        /*************************/
	   this[t_name].setTextFormat(style);
	}
}

Now go to the continuation of this lesson.

previous        Continuation