Now we will take a look at the ContactForm.as file. If you are already familiar with writing classes you can skip ahead to the Nitty Gritty section since the following few paragraphs are for the benefit of those who are scratching their heads and saying, Why the heck would you want to write all this code when it's so easy to just draw a text field on the stage?

You are right, of course. It is easy to draw a textfield, but what you will come to see is that once the initial work of creating a text field in code is accomplished, you have much more control over that text field. Here is an example of a scripted text field:

[AS]t1 = new TextField();
t1.x = xPos;
t1.y = yPos;
t1.height = 40;
t1.width = 490;
t1.multiline = true;
t1.wordWrap = true;
t1.text = "Feel free to contact us with your comments, questions and suggestions by filling out the form below.";
t1.setTextFormat(titleFormat);
addChild(t1);[/AS]

And you have created a TextFormat like so:

[AS]titleFormat = new TextFormat();
titleFormat.align = "left";
titleFormat.font = "Arial";
titleFormat.size = 10;
titleFormat.color = 0x000000;[/AS]


Now, let's say you have 10 title fields on the stage. They are using a font color of black with a font size of 10. You get everything set up just the way you want it and suddenly your client says, Gee whiz, wouldn't it be nice if the titles were blue? And maybe just a little bigger? Now you have to adjust each text field one at a time. With the code version you have set up a TextFormat and assigned it to each of the title fields. So all you have to do to change each one of those fields is change the appropriate TextFormat properties - once.

[AS]titleFormat = new TextFormat();
titleFormat.align = "left";
titleFormat.font = "Arial";
titleFormat.size = 12;
titleFormat.color = 0x0000FF;[/AS]

Isn't that a wee bit faster?

Another nice advantage to coding your assets is when you have to move them around. To make it really simple, I use these variables:

[AS]private var xPos:int;
    private var yPos:int;
[/AS]

Let's say for some reason you need to suddenly shift the entire contact form 10 pixels to the right. Simply change the starting xPos from xPos = 10; to xPos = 20; and the whole thing moves. No more shifting every text field and component one at a time.

Another reason to code your assets is the additional options that you have available to you. You will find that you have much greater control over the appearance and actions of your assets with code. In the case of a text field, the autoSize property can be very handy when you are bringing in dynamic text of uncertain lengths. Let's take a look at an example.

[AS]
t1 = new TextField();
t1.x = xPos;
t1.y = yPos;
t1.height = 40;
t1.width = 490;
t1.multiline = true;
t1.wordWrap = true;
t1.text = "This was brought in dynamically and you don't know the length of it in advance.";
t1.setTextFormat(titleFormat);
t1.autoSize = TextFieldAutoSize.LEFT;
addChild(t1);

yPos += t1.height + 10;

t2 = new TextField();
t2.x = xPos;
t2.y = yPos;
t2.height = 20;
t2.width = 150;
t2.text = "This is the text under your autoSize text field.";
t2.setTextFormat(titleFormat);
addChild(t2);
[/AS]

The t1 textfield will have it's text brought in dynamically from an XML file perhaps. No matter what length it is, the t1 text field will expand down, but you don't have to worry about the position of the t2 text field because it's y position is automatically adjusted depending on the height of t1. You can't do that on the stage.

And perhaps the biggest reason why I prefer to use this method, is that I have everything at my fingertips. No more clicking on individual fields to find out which one I called a particular instance name, or which one I used the color red on. From a single location I can survey the entire project, and change any aspect of it.

Now let's dig into the ContactForm.as and see what makes it tick.