- Flash AS3 -

Monday, August 15, 2011

Flash Game Development :: Lesson 1 - C

Lesson 1 - C :: Properties

After having completed Lesson 1 - B, you should now be able to make use of Variables, and call functions without a hitch. Continuing on the 'Hello World' example, we will proceed to modify our code.

Let us start by creating a private class variable named txt_Field of type TextField, then proceed to the constructor in order to initialize it and set it's text property to the value of the msg String variable. If we compile and run the movie, we will get the same result obtained in Lesson 1 - B. [Figure 001]

[Figure 001]

Next we will set some properties of the txt_Field object. We will set the values of the following properties: Height = 50, Width = 100, Border = true, X = 10 and Y = 10. Once the properties are set, if we compile, and run the movie we'll get a TextField who's border is visible, and height and width defined. [Figure 002]

[Figure 002]

As you can see, the TextField text is pretty plain using the default Font and Color. Next we will create a TextFormat object which we will then apply to the TextField. Create a new private class variable named txt_Format of type TextFormat, and make sure that you have the required import command set up top. [Figure 003]

[Figure 003]

Proceed to initialize the variable and set the following properties as follows:
Font = Verdana, Size = 14, Color = Green (0x00FF00), align = CENTER (TextFormatAlign.CENTER). [Figure 004]

[Figure 004]

Once the properties are set, assign the TextFormat Object to the TextField variable txt_Field, by setting the defaultTextFormat property of txt_Field.

Please note that it is of upmost importance to define this property, i.e. defaultTextFormat, BEFORE setting the Text value of the TextField, otherwise the written Text will not be Styled.

[Figure 005]

If we save, compile and run our Movie now, we should get the original TextField, containing the same text as before, but this time, it is formatted in a different manner. [Figure 006]

[Figure 006]

For those of you who are curious, there is another method that can be used to set the Text Format of the said TextField. The method in question is setTextFormat.

The primary difference between defaultTextFormat and setTextFormat is that the prior is a property while the latter is a function.

This means that we set the defaultTextFormat by assigning a value to it while we call setTextFormat by passing it values for the parameters it requires. [Figure 007]*

[Figure 007]

The second and most influential difference between the two is that, the defaultTextFormat method sets the TextFormat to be used to all sub sequent text contained within the TextField variable.

While the setTextFormat method sets and defines the text format for the text which is currently present within the TextField variable. Once the value changes, the text format is lost.

Click here to download the source code for this example.

*1 Note the difference between Lines 36 & 37 and 40 & 41. Remember that the '//' are keywords used to define the start of commenting.

Labels: , , , ,


Post a Comment

<< Home