Now that you know what objects are and how to operate the Actions panel, you can begin to script with objects and call their methods or evaluate and assign new properties.
Flash provides existing classes that it calls objects. The Array object, the Boolean object, the Color object, and all the other objects that Flash provides in the Objects category of the Actions toolbox are really predefined classes. The Array object, for example, should really be called the Array class because you create named individual objects from it. This book refers to the Flash predefined classes as objects with capitalized names, because that's the way they are categorized in the Actions panel's toolbox, but it will continue to use the term classes in general discussions for clarity's sake.
These Flash classes have methods and properties that control different elements of your Flash movie, such as graphics, sound, data, time, and mathematical calculations. You can also build your own classes from scratch by combining some of the existing classes and actions with functions (see Chapter 11).
Flash's predefined classes reside in the Objects category in the Actions panel (Figure 3.33). Before you can use one of these classes, you must create an instance of the class by giving it a name. The process is similar to creating an instance of, or instantiating, a symbol. You need to create an instance of the class to use it in ActionScript, and you do so by naming it. Use the keyword new to assign a unique name to a new instance of the class.
Figure 3.33 Flash's predefined objects control different kinds of information. The Objects category in the Actions toolbox is divided in more categories. Here, you see the Sound object and the contents of its folder.
Adam = new Human (); myColor = new Color ();
Where do you find the keyword new? In the Objects category of the Actions toolbox, the classes that require you to make new instances include the command new (Figure 3.34).
Figure 3.34 In the Array category, the action new Array will create a new instance from the Array object. In the Date category, the action new Date will create a new instance from the Date object.
The two examples in this section create new objects that you can use and manipulate. The first is a hypothetical statement that makes a new human object called Adam from the Human object. The second is an actual ActionScript statement that makes a new color object called myColor from the Color object. The statement that contains the new operator in front of the class is called a constructor function. Constructor functions are specialized functions that create new instances from classes.
The following task demonstrates how to instantiate the Date object. In Normal mode, you can make a new instance of an object in two ways. The first way uses the action set variable; the second way uses the action new. The first way is more menu-driven and is slower. The second way requires more typing but is quicker.
To create an object by using set variable:
Open the Actions panel in Normal mode.
In the Actions toolbox or from the plus button's pull-down menu, choose Actions > Variables > set variable.
A new statement appears in the Script pane, with empty Variable and Value fields in the Parameters pane (Figure 3.35).
Figure 3.35 The set variable action requires a variable and a value.
In the Variable field, enter the name you want to give your object (Figure 3.36).
Figure 3.36 Enter myDate as the name of your new Date object.
Place your pointer in the Value field, and choose Objects > Core > Date > new Date.
The constructor function new Date appears in the Value field (Figure 3.37).
Figure 3.37 The constructor function new Date will make a new instance and assign it to the name in the Variable text box.
Check the Expression checkbox next to the Value field (Figure 3.38).
Figure 3.38 The new Date() statement is an expression, so check the Expression checkbox.
The quotation marks around the new Date constructor function in the Script pane disappear. This step makes sure that Flash recognizes that the name of your instance evaluates a new Date object and is not assigned to the actual characters in the Value field.
The Script pane shows the full statement, which assigns the new Date object to the name you entered. Your Date object is instantiated and ready to use (Figure 3.39).
Figure 3.39 The finished statement creates an object called myDate from the Date object.
To create an object using the keyword new:
Open the Actions panel in Normal mode.
In the Actions toolbox or from the plus button's pull-down menu, choose Objects > Core > Date > new Date.
A new statement appears in the Script pane, with new Date() in the Expression field in the Parameters pane (Figure 3.40).
Figure 3.40 Choose new Date in the Actions toolbox.
Place your pointer before the new Date () statement, and enter the name you want to call your object, followed by an equal sign (Figure 3.41).
Figure 3.41 Complete the statement in the Expression text box.
The Script pane shows the full statement, which assigns the new Date object to the name you entered. Your Date object is instantiated and ready to use (Figure 3.42).
Figure 3.42 The finished statement in the Script pane.
Some of the Flash objectssuch as Key, Math, and Mousedo not need a constructor function to instantiate an object. You can use their methods and properties immediately without having a named object. This practice makes sense because only one unique instance can exist for these special objects. (The Mouse object, for example, can have only one instance because there is only one mouse per computer.) You can tell which of the Flash objects don't require a constructor function by looking in the Objects category in the Actions toolbox. Objects that don't require a constructor function won't list the command new among their methods and properties.
About the Expression Box
When you are using the action set variable, Expression checkboxes appear for your parameters. Checking the Expression checkbox next to the Value field removes the quotation marks from the actual script, changing a string literal to an expression. A string literal, which is always contained within quotation marks, represents the actual collection of characters: numbers, letters, or symbols. An expression, on the other hand, is a formula that may contain variables, or placeholders, that Flash needs to evaluate before knowing what the entire expression represents. The string literal "3+2" is just the three characters 3+2. The expression 3+2, however, is 5. You'll work more with expressions and variables in Part V.
The next step after creating a new object involves calling an object's methods. Recall that you can call a method by using an object's name, followed by a period and then the method with its parameters within parentheses. In the Actions panel in Normal mode, you call a method simply by selecting the method in the particular Objects category (Figure 3.43). Depending on the kind of method you choose, the Actions panel displays methods in one of three ways:
Figure 3.43 Below every object category, a Methods folder lists the methods you can choose to use. The methods of the Color object are shown here.
The method appears as part of an Expression field in the Parameters pane (Figure 3.44). You must provide the object name before the period. Sometimes, you have to create a new line in the Script pane to accommodate this method. Do so by using the evaluate action (Actions > Miscellaneous Actions > evaluate) before you choose your method.
Figure 3.44 getDate() is an example of a method that appears in an Expression field.
The method appears in the Script pane with an empty Object field in the Parameters pane (Figure 3.45). You must provide an object name in the Parameters pane.
Figure 3.45 stop()is an example of a method that requires an object.
The method appears in the Script pane with empty Object and Parameters field in the Parameters pane (Figure 3.46). You must provide an object name and the required parameters in the Parameters pane.
Figure 3.46 setMask()is an example of a method that requires an object and parameters.
Ultimately, your final ActionScript in the Script pane will look something like this:
This statement calls the method setRGB() to change the color associated with the myColor object. The parameter is the hex number 0x00CC33.
Your ActionScript could also look something like this:
currentDate = myDate.getDate ()
This statement calls the method getDate() from the myDate object and puts the information it retrieves into the variable called currentDate.
The following example continues the preceding task and calls a method of the Date object. Later chapters introduce specific objects, provide more information on the Date object, and show you how to use objects' methods to control your Flash movie.
To call a method of an object:
Continuing with the preceding task, in the Actions panel, choose Actions > Miscellaneous Actions > evaluate.
A new statement appears below the first. The semicolon in the Script pane marks the end of the yet-to-be-written statement (Figure 3.47).
Figure 3.47 The evaluate action creates a new line in the Script pane.
Click inside the Expression field in the Script pane, and choose Object > Core > Date > Methods > getDate.
A dot and the method appear in the Expression field and in the Script pane. The script is highlighted in red, warning you that the syntax is incorrect and not complete (Figure 3.48).
Figure 3.48 A dot and the getDate() method appear in the Expression field.
Place your pointer before the .getDate () statement; then enter the name of a variable, an equal sign, and the name of the Date object you created in the preceding task (myDate, in this example) (Figure 3.49).
Figure 3.49 Enter the remaining statement to call the method, and put its returned value in a variable called myDisplay.
The completed statement appears in the Script pane, and the red highlighting is removed. The statement gets the current date and puts that information in the variable called mydisplay. The information that this method gets is called the returned value.
Select the Text tool, and drag out an empty selection on the Stage.
In the Property inspector, choose Dynamic Text from the pull-down menu, and in the Var text box, enter the name of the variable that holds the current date (Figure 3.50).
Figure 3.50 The text box at the top is set to Dynamic Text with the variable myDisplay. Do not confuse the Instance Name and the Var field in the Property Inspector.
The dynamic field on the Stage displays the value of its variable during playback of the movie. You learn more about dynamic text in Chapter 10.
Test your movie by choosing Control > Test movie.
Flash instantiates a Date object and then makes the object call the getDate() method. The returned value (the date) is put in the variable called mydisplay. You see the value of this variable in the text box on the Stage.
Just as you have multiple ways to call a method, you have two ways to assign properties in Normal mode of the Actions panel. The first way is to use the set variable action (Actions > Variables > set variable). This method gives you a Variable field and a Value field in the Parameters pane. The second way is to use the evaluate action (Actions > Miscellaneous Actions > evaluate). This method gives you an Expression field in which you can enter an entire statement, assigning a value to a property or the value of a property to a variable. No matter which way you go, in the end, keep in mind that you want the Script pane to display your object name, followed by a dot and then the property name. If you want to assign a value to a property, that value goes on the right side of an equal sign.
myTextField.text = "hello";
This statement assigns the word hello to the text property of the object called myTextField.
If you want to read the value of a property and put it in a variable to use later, your objectdotproperty construction goes on the right side of an equal sign.
myCurrentText = myTextField.text;
This statement puts the value of the text property of the object called myTextField in the variable called myCurrentText.
When you're working with movie clips, you can use two more actions: getProperty and setProperty. Chapter 7 explains movie-clip properties.
To assign a value to a property by using set variable:
Choose set variable (Actions > Variables > set variable).
A new line appears in the Script pane, with a Variable field and a Value field in the Parameters pane (Figure 3.51).
Figure 3.51 Choose the set variable action.
In the Variable field, enter the name of your object.
Choose the property in the Actions toolbox.
Properties are located inside each object's folder in the Object category.
The property appears after the name of your object, separated from it by a dot (Figure 3.52).
Figure 3.52 The properties of each object are listed in a Properties category. Here, the borderColor property of a TextField object is selected.
In the Value field, enter a value.
If the value you entered in step 4 is an expression, check the Expression checkbox next to it.
The completed statement appears in the Script pane (Figure 3.53).
Figure 3.53 The borderColor property takes a hex number as its value. The final statement makes the border color of the text box called myTextField black.
To assign a value to a property by using evaluate:
Choose evaluate (Actions > Miscellaneous Actions > evaluate).
A new line appears in the Script pane, with an Expression field in the Parameters pane (Figure 3.54).
Figure 3.54 Choose the evaluate action.
In the Expression field, enter the name of your object.
Choose a property from the Actions toolbox.
Properties are located inside each object's folder in the Object category.
The property appears after the name of your object, separated from it by a dot (Figure 3.55).
Figure 3.55 Enter the object name, and choose its property.
Enter an equal sign, followed by a value.
The completed statement appears in the Script pane (Figure 3.56).
Figure 3.56 The final statement is exactly like the one you created with set variable, but it uses a single Expression field rather than Variable and Value fields.
Symbols and Classes
Symbols are not classes. Symbols are not even objects. It's true that movie clips and buttons are both symbols and objects, but these elements are exceptionsand perhaps the source of some confusion. Graphics, sounds, bitmaps, and video clips are all symbols that appear in the Library, but they are not objects or classes because they do not have methods and properties that you can control with ActionScript.
Some parallels exist between classes and symbols. Symbols are reusable assets created in or imported to the Library. With these symbols, you create instances, or copies of the symbols, to use in your movie. Classes, as you have seen, also have instances made from them to be used in your movie.