Structuring ActionScript
ActionScript has a particular syntax, which you learned a lot about in the fundamental language concepts covered in Chapter 6. For example, you use dot notation, brackets, parentheses, methods, and properties to construct statements and make things happen in your projects. There are some additional concepts to cover in this chapter while you build a fully scripted animation that includes random movement and animated text.Chapter 6, you created and used variables and data to make your project work, and found out that the data you used had a type (such as Number or String). ActionScript lets you define the data type when you declare variables, using a colon and then the data type. This means that you specify what type of data the variable can contain; specifically, which helps reduce errors in your code. For example, if you create a variable to hold numerical values, you would write the following:
var myNumber:Number;

However, if you write the following:
myNumber = 6;
you then see an error in the Output panel after you publish the file (Figure 7.1), notifying you of the type mismatch error in your code. Strict data typing also helps ensure that you use methods and properties only for the object's type.
myNumber = "dog";
Figure 7.1. When you mismatch data types, you see an error in the Output panel.
[View full size image]

this code creates a new instance of the XML class, but does not use strict data typing. You wouldn't see the code completion menu when you type myXML followed by a dot (.) later in your code. However, when you use strict data typing such as the following code, you see a code completion menu after typing a dot (.).
var myXML = new XML();
You see a menu with all the methods and properties for the XML class listed. Because of the advantages outlined in this section, you use strict data typing for this chapter's project, and in future projects in this book.
var myXML:XML = new XML();
myXML.
About loops
In Chapter 6, you used conditionals in your project to see whether instances intersected; if that condition were TRue, you executed code. In this project, you use loops to add many objects to the Stage if a condition is true. Loops are similar to conditionals because they execute a statement when it evaluates to true. However, loops execute repeatedly while the condition you specify evaluates to TRue. There are several different kinds of loops, but they generally follow these criteria.

The loop starts with the initialization statement (an initial value), which it executes one time. It then executes statements in the body while the condition is TRue. After each pass (or iteration) through the loop, the update statement executes. So, the following code displays numbers 10 through 59 in the Output panel:
for (initialization; condition; update){
//statements
}
The loop starts with an initial value of 10, and then while i (the variable) is less than 60, it increments the value by 1 and updates the variable. The loop ends when the value of i reaches 60 (or greater).Loops are useful for a variety of purposes, particularly repetitive actions. For example, if you want to draw or add a bunch of graphics to the Stage, you might use a loop to do so.
for (var i = 10; i<60; i++) {
trace(i);
}
Creating a random number generator
To make animations look a bit more interesting and less scripted, you can use Flash's Math.random() method to help generate random numbers. You use the Math.random() method to return a "pseudo" random number (it's not truly random, but close enough for many animations). Therefore, instead of having each rectangle move the exact same amount of space, you can have it slide a random number of pixels vertically as well as have it move either randomly up or down. The first step is to create a custom function, which generates a random number between a specified minimum and maximum number.The randRange() function can be seen in the following code example, and you'll use it later in this chapter's project:
The previous function takes two Numeric parameters (min and max) and returns a random integer between the two numbers. Although the code looks a bit daunting, it is fairly basic math.Applying basic mathematical principles, the code starts by calculating the inner brackets and working outward. The first step is to find the difference between the min and max parameters. For example, if you call the function with a min value of 3 and a max value of 7, the difference is 4. You then multiply the difference that returns by a random floating-point number (a number that can have a fractional part, such as 3 or 1/2) between 0 and 1. The result of the multiplication is rounded to an integer value using the Math.round() method. Finally, the random integer is added to the smallest parameter value. The smallest parameter value is determined by using the Math class' Math.min() method.
function randRange(min:Number, max:Number):Number {
var randNum:Number = Math.round(Math.abs(max-min)*Math.random())+Math.min(min, max);
return randNum;
}
Returning Values
You know how to use methods and properties in your code, and you wrote functions using them. You can use functions and methods to return a value to your ActionScript. When you're working with dates in Flash, certain methods return values back to your scripts. For example, if you have a Date object called my_date, you can get the current month for that date object when you trace the value of my_date.getMonth(). The getMonth() method returns a numeric value between 0 (January) and 11 (December), which represents the month of the my_date object. Functions and methods aren't limited to returning numbers, or even simple values. The TextField class contains a method called getFontList(), which returns an array of fonts installed on the computer system that the SWF file plays on. Other functions or methods can return various different structured objects.