What is Software? | What is Education? | What is Technology? | What is Research? | What is Wi-Fi? | What is Communication? | What is Customer Service?

Free SEO Tutorials & Help

Tutorials:   Adobe Flash   Adobe Photoshop   CorelDRAW   SEO  

Site Map

Website Development | Cable & Broadband Magazine | Solutions for Higher Education | Auction Anything Online

Adobe Flash Help & Tutorials

 Back to Topics

 

   Using ActionScript: How to Write Scripts in Flash

ActionScript: Write Scripts

Set up your workspace

Create an instance of a symbol

Name button instances

Initialize the document

Apply ActionScript syntax

Locate ActionScript reference documentation

Add comments to ActionScript

Write a function for a button

Copy and modify a button function

Check syntax and test your application

 
ActionScript: Write Scripts 

TOP

The ActionScript language that is part of Macromedia Flash Basic 8 and Macromedia Flash Professional 8 offers designers and developers a variety of benefits. With ActionScript you can control document playback in response to events such as elapsed time and loading data; add interactivity to a document in response to user actions, such as a button click; use built-in objects, such as a button object, with built-in associated methods, properties, and events; create custom classes and objects; and create more compact and efficient applications than you could create using user interface tools, all with code that you can reuse.


ActionScript is an object-oriented scripting language that offers control over how your Flash content plays. In subsequent lessons, you'll see how ActionScript has evolved into ActionScript 2.0 to comprise a core set of language elements that make it easier to develop object-oriented programs.

 

Set up your workspace

#TOP

First, you'll open the start file for the lesson and set up your workspace to use an optimal layout for taking lessons.

  1. To open your start file, in Flash select File > Open and navigate to the file:

    • In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and Tutorials\Tutorial Assets\ActionScript\Write Scripts and double-click scripts_start.fla.

    • On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples and Tutorials/Tutorial Assets/ActionScript/Write Scripts and double-click scripts_start.fla.

    NOTE The Write Scripts folder contains completed versions of the tutorial FLA files for your reference.

  2. Select File > Save As and save the document with a new name, in the same folder, to preserve the original start file.

  3. Select Window > Workspace Layout > Default to set up your workspace for taking lessons.

 

Create an instance of a symbol

TOP

You'll drag an instance of an animated movie clip from the library to the Global Positioning System artwork on the Stage. You'll then follow the recommended practice of always naming instances--both to prompt code hinting and because in your scripts you generally refer to instance names rather than symbol names. Code hints are the tooltips that prompt you with the correct ActionScript syntax.

  1. In the Tools panel, click the Selection tool. Select the map layer in the Timeline, and click the padlock next to the map layer to unlock that layer.

  2. To place the movie clip accurately, select View > Snapping. Select Snap Align and Snap to Objects if the commands are not already selected.

  3. From the Library panel (Window > Library), drag map_skewed to the black background area of the Stage.

    Because guides don't appear when you first drag an object from the Library panel, you'll release the object, and then drag it again.

  4. Drag the map_skewed movie clip on the Stage again so that the align guides appear. Use the guides to align the movie clip to the top and left edges of the GPS screen.

    NOTE If you make an error in placement, either drag the movie clip again, or press Control+Z (Windows) or Command+Z (Macintosh) to undo your changes.

  5. With the instance of map_skewed selected on the Stage, type screen_mc in the Instance Name text box of the Property inspector (Window > Properties).

    Flash is designed to present code hints when you name your instances with the appropriate suffix:

    • When naming a movie clip instance, always give the instance a suffix of _mc, as in screen_mc.

    • When naming a button, use the _btn suffix.

    • When naming a text field, use the _txt suffix.

     

Name button instances

TOP

Using the appropriate suffix to prompt code hinting, you'll provide instance names for two button instances that are already on the Stage.

  1. In the Timeline, unlock the Buttons layer.

  2. On the Stage, select the instance of play_button (the large green button).

  3. In the Instance Name text box of the Property inspector, type onButton_btn to name the instance.

  4. On the Stage, select the instance of button_stop (the small red button).

  5. In the Instance Name text box of the Property inspector, type offButton_btn to name the instance.

 

Initialize the document  

TOP

Applications have an initial state that specifies how the content first appears to users. You initialize properties and variables in the first frame of a document. You'll specify that the map movie clip should not be visible when the SWF file first plays.

  1. Select Frame 1 of the Actions layer. If the Actions panel isn't open, select Window > Actions.

    Actions - Frame appears at the top of the panel, which indicates that you selected a frame in which to apply ActionScript. It's a good practice to verify that you're attaching ActionScript to the intended frame or object.

    The Actions panel includes a Script pane, the blank text entry area, in which you can enter text directly; an Actions toolbox, which lets you select ActionScript to add to your script; and a Script navigator, which functions like the Movie Explorer.

  2. Along the top of the Actions panel, click Insert Target path.

  3. In the Insert Target Path dialog box, verify that Relative, meaning relative path, is selected. From the hierarchical tree in the dialog box, select screen_mc. Click OK.

    A target path tells ActionScript the location of an object within the overall structure of a document.

  4. Click in the Script pane, at the end of the screen_mc text, and type a period (.).

  5. When you type the period, code hints appear for the movie clip, because you used the _mc suffix when naming the instance. Double-click _visible from the list of code hints, and type the following:

    = false;

    This line of code makes the screen_mc movie clip invisible on the Stage.

    NOTE If code hints don't appear, you don't have code hints selected as a preference in the Actions panel. You can type _visible directly in the Script pane. You can also change your preferences by clicking the pop-up menu in the upper-right corner of the Actions panel. From the pop-up menu, select Preferences, and then select Code Hints on the ActionScript tab.

Throughout authoring, remember to save your document frequently.

 

Apply ActionScript syntax

TOP

All languages, whether computer languages or written and spoken "human" languages, follow specific rules that foster comprehension. These rules are known as the language syntax.

Flash uses dot syntax, which means that the period (.) links parts of a script. Other ActionScript syntax elements include the following:

  • A semicolon (;) in an ActionScript statement, like a period in an ordinary sentence, indicates the end of a statement.

  • Parentheses () group arguments that apply to an ActionScript statement.

  • Curly braces {} group related ActionScript statements. You can use nested braces to create a hierarchy of statements.

Later in this lesson, you'll use Flash features that allow you to test your syntax.
 

 

Locate ActionScript reference documentation

TOP

During authoring, if you'd like additional information about the ActionScript that you enter, you can select the action in the Actions toolbox or Script pane and click Reference. The Help panel displays information about the selected action.

  1. In the Script pane of the Actions panel, double-click visible to select the term.

    NOTE After completing the next step, you'll change topics in the Help panel and you will no longer be on this lesson topic. In the Help panel, click the History Back icon to return to this topic.

  2. Along the top of the Actions panel, click the Help icon.

    The visible entry in the Help panel appears.

     

Add comments to ActionScript

TOP

In ActionScript, text after double slashes (//) is commented text, which Macromedia Flash Player ignores. Commented text often documents script functionality so that other developers can understand your script, but you can also use comments to deactivate sections of your script when debugging. As a best practice, always add comments that explain your scripts.

  • In the Script pane of the Actions panel, place the insertion point at the beginning of the line of code and type // Initialize document to hide screen movie clip. Press Enter (Windows) or Return (Macintosh).

  • Text in the Script pane appears as follows:

  • // Initialize document to hide screen movie clip.

  • this.screen_mc._visible = false;

NOTE If your commented text is many lines, you can use /* instead of double slashes for the beginning of the comment, and */ to mark the end of the comment.

 

Write a function for a button

TOP

A command in ActionScript is called a function. A function is a script that you can write once and use repeatedly in a document to perform a certain task. You're going to write a function that makes the screen_mc movie clip appear (visible = true) when the user releases the mouse button.

  1. In the Script pane of the Actions panel, click after the last line of code, press Enter (Windows) or Return (Macintosh) twice, and type // function to show animation

  2. Press Enter or Return and click Insert Target Path, along the top of the Actions panel. Select onButton_btn from the hierarchical tree, and click OK.

  3. In the Script pane, type a period (.) and double-click onRelease from the list of code hints that appears.

  4. In the Script pane, press the Spacebar and type the following:

    = function(){

    The line of code that you just completed should appear as follows:

    this.onButton_btn.onRelease = function(){

    You already know how to select objects in the Insert Target Path dialog box; you'll now enter the instance names directly into the Script pane.

  5. Press Enter or Return, and type the following:

    screen_mc._visible = true;

  6. Press Enter or Return and type }; to specify the end of the statement.

    The function should appear as follows:

    // function to show animation

    this.onButton_btn.onRelease = function(){

    screen_mc._visible = true;

    };
     

Copy and modify a button function

TOP

You just created one function that sets the visible property of a movie clip to true when the user releases the mouse button after a button click. You can probably guess how to create another function that hides the screen_mc movie clip: by setting the movie clip _visible property to false when the user clicks an Off button. You'll create that function now.

  1. In the Script pane, select the entire function that you just typed, including the comment, curly brackets, and semicolon. Copy the text as you normally would, using Control+C (Windows) or Command+C (Macintosh).

  2. In the Script pane, place the insertion point after the last line of code. Then press Enter (Windows) or Return (Macintosh) twice, and paste the text as you normally would, using Control+V (Windows) or Command+V (Macintosh).

  3. In the copied function, change the text in onButton_btn to read offButton_btn.

    Remember, earlier you assigned an instance name of offButton_btn to an instance.

  4. In the copied function, change the visible property of the screen_mc movie clip from true to false.

  5. In the copied function, change the commented text after the slashes to read function to hide animation.

    Your entire script should appear as follows:

    // Initialize document to hide screen movie clip.

    this.screen_mc._visible = false;

    // function to show animation

    this.onButton_btn.onRelease = function(){

    screen_mc._visible = true;

    };

    // function to hide animation

    this.offButton_btn.onRelease = function(){

    screen_mc._visible = false;

    };

     

Check syntax and test your application

TOP

As you learned earlier in this lesson, ActionScript depends on correct syntax to execute properly. Flash offers a variety of ways for you to test your syntax.

  1. To check the syntax, do one of the following:

    • Click the pop-up menu in the upper-right corner of the Actions panel title bar and select Check Syntax.

    • Click Check Syntax along the top of the Actions panel.

    If the syntax is correct, a message appears stating that the script contains no errors.

    If the syntax is incorrect, a message appears stating the script contains errors; the Output panel opens and displays information about the error.

  2. Click OK to close the syntax message.

  3. After you've verified that your ActionScript does not contain syntax errors, save the document and select Control > Test Movie.

    When the SWF file appears, the animation should not appear in the Global Positioning System screen, because its initial visible property is set to false. When you click and release the top green button, you call the function that sets the movie clip's visible property to true. Does the animation play then? Finally, click the red Off button to see if the visible property for the animation is again false.

    You can test your SWF content throughout authoring to confirm that it plays as expected.

 

Copyright ADOBE - All Rights Reserved Worldwide

 

 

More Topics:

Working with Flash Documents

How to work in Flash WorkSpace

Working with Projects in Flash

Process to Build your First Application in Flash

Using Symbols, Instances and Library Assets in Flash

How to Build Video Player in Flash

How to Work with Color, Strokes and Fills in Flash

How to Create Document in Flash

What is Vector and Bitmap Graphics in Flash

How to Create a Banner in Flash, Part 1

How to Work with Text in Flash

How to Create a Banner in Flash, Part 2

How to Use Imported Artwork in Flash

How to Create a Banner in Flash, Part 3

How to Work with Graphic Objects in Flash

How to Work with Layers in Flash

How to Use Filters and Blends

Working with Graphics in Flash

What is Accessibility Features in Flash

How to Create Motion (Shape Tween & Motion Tween) in Flash

How to Create an Application in Flash

What is Masking in Flash

How to Work with Video in Flash

How to Use Layout Tools in Flash

What are Behaviors in Flash

How to Work with Sound in Flash

How to Create Symbols and Instances in Flash

What is ActionScript in Flash

How to Write ActionScript With Script Assist in Flash

How to Add Button Animation and Navigation in Flash

What is Data Integration in Flash

How to Work with Screens

How to Create a Presentation with Screens

What is Extending Flash

How to Create Multilanguage Text in Flash

How to Create Graphics: Draw in Flash

What is Flash Lite

Ways of Data Integration

How to Create Graphics: Create a Timeline Animation in Flash

Getting Started with Flash Lite in Flash

How to Publish Flash Documents

How to Create Graphics: Making Animations with Easing

Learning Flash Lite 1.X ActionScript in Flash

How to Export Flash Content and Images from Flash

How to Create Graphics: Applying Gradients in Flash

Process of Writing and Editing ActionScript 2.0 in Flash

How to Create Accessible Content in Flash

How to Create Graphics: Apply Graphic Filters and Blends

What is Data and Data Types in Flash

Process of Printing from SWF Files in Flash

Using ActionScript: How to Use Script Assist mode in Flash

Learn Syntax and Language Fundamentals in Flash

How to Create E-learning Content in Flash

Using ActionScript: How to Write Scripts in Flash

Working with Functions and Methods in Flash

Process of Using Templates in Flash

Using ActionScript: Process of Adding Interactivity in Flash

What are Classes in Flash

Control Tag Summary of XML to UI in Flash

Using ActionScript: How to Create a Form with Conditional Logic and Send Data in Flash

What is Inheritance in Flash

What is Data Integration: Overview

Using ActionScript: How to Work with Objects and Classes in Flash

Overview on Interfaces in Flash

What is Data Integration: Using XML for a Timesheet

How to Work with Text and Strings in Flash

How to use Handling Events in Flash

What is Data Integration: Using XUpdate to Update the Timesheet

Learning Animation, Filters and Drawings in Flash

How to Work with Movie Clips in Flash

How to Create Interaction with ActionScript in Flash

How to Work with Images, Sound, and Video in Flash

How to Work with External Data in Flash

What is Security in Flash

How to Debug Applications in Flash

List of Error Messages in Flash

Using Object-Oriented Programming with ActionScript 1.0 in Flash

How to Write Scripts for Earlier Versions of Flash Player in Flash

List of all Keyboard Keys and Key Code Values for using in Flash

Terminology

Introduction to Components in Flash

What are Components in Flash

How to Create an Application with Components

How to Work with Components in Flash

How to Handle Component Events in Flash

How to Customize Components in Flash

How to Create Components in Flash

What is Collection Properties in Flash