FLASH AS2 OOP SLIDESHOW 2004: LESSON 11
The importance of casting: part 2
Let's first have a look at the correct and incorrect files. Although we have set italic to false we get italic text. How come?
The reason is that the text from the xml is interpreted as string, which is fine when it comes to the number for the font color but not when it comes to a boolean statement. It is also interesting that the compiler is not giving us errors. If you check the traces the false statement from the xml is interpreted as string although style.italic requires a boolean statement. In order to correct this we need to convert the string to a boolean which is done using the following syntax.
/***************BOOLEAN and number from xml**********/ head_italic = (values_2.firstChild.firstChild.nodeValue == "true"); trace("1: "+head_italic); thisClip.hsi = head_italic; trace("2: "+thisClip.hsi); head_font_color = Number(sub_values.firstChild.nodeValue); thisClip.hfc = head_font_color; /****************************************************/
Interestingly, if we just declare...
head_italic = Boolean(values_2.firstChild.firstChild.nodeValue);
we cast head_italic as Boolean but with a string value and every string value of a Boolean is automatically set to true. Although not necessary we also cast the nodevalue for the color number. So from now on we should get used to to cast all nodevalues or attributes from xml files to obtain the right datatypes.
Now you can download the files for this lesson and test everything out.