ptg 14 14 347 Using Basic ActionScripts Introduction Flash's programming language is called ActionScript. ActionScript lets you create detailed instructions on how the Flash movie will perform. Flash CS5 allows you to build applications using two different versions of ActionScript, either ActionScript 1.0 & 2.0 or ActionScript 3.0. In this chapter, you’ll learn the basics of ActionScript along with scripting as it relates to ActionScript 2.0. In the next chapter, you’ll learn about scripting as it relates to ActionScript 3.0. ActionScripts can use an event to trigger the specific action. Say you create a button, and you want the ActionScript to trigger an instruction that will stop the movie, but only when the user clicks the button object. The defined event is the user clicking his mouse, and the action would be to stop playing the movie. ActionScript is a relatively easy language to learn, but a very precise language to code, so pay close attention to the syntax. The good news is that once you master the language and the syntax, the full power of Flash is available to you. You can create ActionScripts that are triggered (the event) by specific data, or information typed in by a visitor. You can even create ActionScripts that are sensitive to variables such as date and time. Flash helps you by giving you functions (English-like script) and as your ActionScript skills grow, you can even cre- ate and call your own functions. Each new version of Flash moves the ActionScripting language closer and closer to JavaScript. The power of Flash is fully realized when you write ActionScripts and incorporate them in your Flash docu- ments. What You’ll Do View the Actions Panel Set ActionScript Preferences Understand Properties and Methods Apply Properties and Methods to an Object Use Dot Syntax Understand Data Types Use Functions Use Conditional Statements Attach a Mouse Event to a Button Work with Frame Events Work with Clip Events Attach a Clip Event to a Movie Clip Work with Loops Use For Loops Work with ActionScript Behaviors From the Library of Wow! eBook ptg 348 Chapter 14 The Actions panel is gives the Flash designer control of a Flash document by allowing him/her to create and edit actions for an object or frame. To use the Actions panel, select an object on the stage, or select a frame on the Timeline, click the Window menu, and then click Actions. Scripts can be typed directly into the Actions panel using the Script pane, or augmented by using a list of installed Actions in the Toolbox. ◆ Toolbox . Supplies a list of all installed actions, either ActionScript 1.0 & 2.0 or ActionScript 3.0 ◆ Script pane. Enter the Actions into the Script pane. ◆ Script Navigator pane. Gives reference to all the Scripts in the active movie. ◆ Current Script tag. Indicates which script is being edited. ◆ Pin Script. Adds a tab for a selected script. ◆ Options menu. Contains options that control and format the Actions panel. ◆ Add Statement. Lets you add script elements to the current action. ◆ Find, and Find and Replace. Searches the active script. ◆ Insert Target Path. Inserts a specific target clip into the action. ◆ Check Syntax. Checks the current action for syntax errors. ◆ Auto Format. Cleans up the script by auto indenting. ◆ Auto Close Bracket. When you type an open bracket {, Flash automatically adds the corresponding close bracket }. (New!) ◆ Show Code Hint. Gives you hints to the syntax of the action as you type. ◆ Debug Options. Add or remove breakpoints into the action to pause on the specified line of code. ◆ Collapse and Expand. Collapse between braces, collapse selection, or expand all. ◆ Comments. Apply a block or line comment. ◆ Code Snippet. Code Snippets provides ActionScript 3.0 code segments to use in scripts. (New!) ◆ Script Assist for 3.0. Script assist provides a visual interface for editing scripts (syntax completion and parameter descriptions). ◆ Help. Provides online help. Viewing the Actions Panel Script Tag Script Navigator pane To ol bo x Script pane Help Script Assist Options menu Pin Script Actions Panel toolbar buttons Code Snippets From the Library of Wow! eBook ptg Chapter 14 Using Basic ActionScripts 349 Since ActionScripting is so important, Flash gives you the ability to con- trol the Actions panel through preferences. ActionScript preferences give you the ability to control the font and size of the text typed into the Actions panel, as well as using syntax coloring to help you visualize the code. You can also set AutoFormat preferences to specify the auto- matic formatting you want for your ActionScript code. Setting ActionScript Preferences Set ActionScript Preferences Click the Flash (Mac) or Edit (Win) menu, and then click Preferences. Click the ActionScript category. Select from the following options: ◆ Automatic Close Brace. Select to automatically insert a close bracket. (New!) ◆ Automatic Indentation. Instructs Flash to perform syntax indentation. ◆ Tab Size. Enter a value for the number of spaces used. ◆ Code Hints. Gives you on- screen hints as you type. ◆ Delay. Delay before showing a code hint. (0 to 4 seconds) ◆ Font. Select a font and size for the ActionScript text. ◆ Open/Import and Save/Export. Select UTF-8 or Default encoding for open and import operations (UTF-8 is best). ◆ Reload Modified Files. Click to be prompted when Flash needs to reload a modified file. ◆ Class Editor. Select an editor or Ask to be prompted. (New!) ◆ Syntax Coloring. Choose the syntax-coloring scheme. ◆ Language. Click ActionScript 2.0 or ActionScript 3.0 to modify the ActionScript sub-settings. Click the Auto Format category. Select format check boxes and view the effect in preview. Click OK. 6 5 4 3 2 1 3 2 6 5 4 To re se t pr ef e re nc e se tt in g to th e default, click Reset To Defaults. Preview area From the Library of Wow! eBook ptg 350 Chapter 14 Objects in Flash are defined using two primary identifiers: properties and methods. The proper- ties of an object define its characteristics. In the real world, a house would be an object, and its properties would be things like its color, style, and number of windows and doors. In Flash, it would be written something like this: house.color = "green"; house.style = "ranch"; house.windows = "12"; house.doors = "2"; In this example, the word house is a unique instance name for the house object, and the words color , style , windows , and doors represent the properties assigned to this instance. Think of an instance as a copy of a Library item. When you create a movie clip, the object is created in Flash's Library. When you return to the Stage, you can then drag the movie clip from the Library to the Stage (technically, you're moving a Library symbol, created using the movie clip behavior). Once the movie clip is moved to the Stage, it is defined as an instance of the original Library item. When you select an instance on the Stage, Flash's Properties panel lets you give it a unique name. In the previous example, "house" is the unique name. Giving a Library symbol a unique name gives you a lot of control. For example, you could move two instances of the same movie clip onto the Stage, give each of them its own unique name (house1, house2) in the Properties panel, and then define different properties for each one. Something like this: house1.color = "green"; house2.color = "blue"; house1.style = "ranch"; house2.style = "tudor"; house1.windows = "12"; house2.windows = "8"; house1.doors = "2"; house2.doors = "4"; In Flash, most objects have properties. For example, the MovieClip object has property val- ues such as transparency, horizontal and vertical position, and visibility. You might define prop- erties loosely as the physical appearance of a movie clip, as it appears on the Flash Stage. A method instructs the object to perform some task. A method is a function that is part of a class definition. For example, if a DVD player is an object, then the methods would be something like: play, record, and stop. Flash methods are written like this: play(); record(); stop(); Some methods require parameters within the parenthesis. The following method instructs the play head to move to frame 6 on the Timeline and stop: gotoAndStop(6); Attaching the method to a specific object requires identifying the object in the ActionScript code: myDVD.gotoAndStop(6); ActionScript is a language, and just like learning any foreign language, all the words and syn- tax might seem strange at first; however, the longer you work with the language, the simpler it becomes. Understanding Properties and Methods From the Library of Wow! eBook ptg Chapter 14 Using Basic ActionScripts 351 Once you've gotten the hang of writing ActionScripts, the next step is to apply properties and methods to objects in a Flash document. You can have an object and let your visitor control its color. Changing the color is an example of changing an object’s properties. To make an object change color, you will need a Flash document that contains a MovieClip and button symbols. An easy script translation would be: Flash, when my visitor clicks (release) on the button, I want you to assign a new color to an object that I gave a unique instance name (change it), and I'm defining that property as objectColor, and change the color (setRGB) to red (0x990000). When you attach ActionScripts to buttons, you're not limited to just a single use. For example, you could drag three instances of the same button symbol on the Stage and repeat the previous code with one exception: change the SetRGB value of one to (0x990000) for red, another to (0x009900) for green, and the third one to (0x000099) for blue. Applying Properties and Methods to an Object Apply Properties and Methods Drag the movie clip onto the Stage. Enter a unique instance name in the Properties panel. Drag the button symbol onto the Stage, and then select the symbol. Click the Window menu, and then click Actions to open the Actions panel. Enter the script (ActionScript 2.0) as shown in the illustration. ◆ ActionScript 3.0 example files are available on the Web at www.perspection.com . Click the Control menu, point to Test Movie, and then click Test. Click the button to change the color of the object to red. 7 6 5 4 3 2 1 2 1 3 5 See Also See “Creating Invisible Buttons” on page 153 for information on creating invisible buttons. From the Library of Wow! eBook ptg 352 Chapter 14 ActionScript, just like any human language, has rules you should follow. However, in ActionScripts, you have to follow the rules or it won't work. One of the more important rules to follow is the use of dot syntax. The use of dots (.) in a script serves several pur- poses. First, is to specify the target path to a particular Timeline. For example, _root.amer- ica.kansas.wichita defines a movie clip on the main (_root) Timeline with the name america, containing a movie clip named kansas, which in turn contains a movie clip named wichita. Dot syntax is used to create a road map for Flash to follow. Dot syntax is a separator between two or more parts of a Flash script. Another use of dot syntax is to change the properties and methods for a specific object. Since ActionScript, by definition is an object- oriented language, Flash performs its respon- sibilities by instructing an object to do something (method), or by changing a prop- erty. For example, star._rotation = 90; instructs Flash to rotate the MovieClip instance named star , 90 degrees (property). To instruct the star MovieClip instance to play (method), you would enter the following code: star.play(); Using Dot Syntax Movie Clip with Instance Name: MovieA ActionScript 2.0 From the Library of Wow! eBook ptg Chapter 14 Using Basic ActionScripts 353 When you use ActionScripts, you have the ability to work with data. Data can be informa- tion entered in by a visitor in an input data field, or it can be computer information such as the current position of the mouse, or the date and time. When you work with data, you have 4 possible data types: ◆ String. Allows for the entering of text values. ◆ Number. Allows for the entering of numeric values. ◆ Boolean. A Boolean state has two values; typically true or false. ◆ Object. Serves as a storage device for any data type, including other objects. Use Data Types to Control Information Since data types control the type of informa- tion that can be entered into a data field, you can use them to validate the data someone is entering. For example, you want to create a calculator to convert between degrees Fahrenheit and Celsius. To do this, you will need an input field for someone to enter the current temperature in Fahrenheit, a button that would perform the calculation, and then a dynamic text field for the result, and one for an error message. Select the Input Field, and then give it a unique variable name in the Properties panel. Select the Error Dynamic Text Field and give it a unique variable name in the Properties panel. Select the Results Dynamic Text Field, and give it a unique variable name in the Properties panel. Select the button instance, and then enter a script into the Actions panel (see the example file for script details). When the movie is played, the visitor will enter a value into the data field, and it will be evaluated as to whether it's purely numeric. If it isn't, an error message will display in the dynamic error field. If the field contains num- bers, then the calculation will perform the conversion and display the results displayed in the output field. 4 3 2 1 Understanding Data Types Error Message using text Error Message using a blank field Correct data no error message From the Library of Wow! eBook . Basic ActionScripts Introduction Flash& apos;s programming language is called ActionScript. ActionScript lets you create detailed instructions on how the Flash movie will perform. Flash CS5 allows. build applications using two different versions of ActionScript, either ActionScript 1.0 & 2.0 or ActionScript 3.0. In this chapter, you’ll learn the basics of ActionScript along with scripting. Actions panel is gives the Flash designer control of a Flash document by allowing him/her to create and edit actions for an object or frame. To use the Actions panel, select an object on the