You are previewing Learning ActionScript 2.0 for Macromedia® Flash® 8.
O'Reilly logo
Learning ActionScript 2.0 for Macromedia® Flash® 8

Book Description

Powerful development and design tools require thorough and authoritative technical advice and documentation. When it comes to Macromedia Flash, no one is more authoritative than Macromedia Development and writing teams. Now their official documentation is available to you in printed book form. As you work, keep this guide by your side for ready access to valuable information on using Flash. We’ve designed it so that it’s easy to annotate as you progress.

Learning ActionScript 2.0 for Macromedia Flash 8 shows you how to write proper ActionScript syntax to create useful and interactive Flash applications. This book includes examples of object-oriented programming, and teaches you how to write custom classes for your Flash applications. Learning ActionScript 2.0 for Macromedia Flash 8 also includes numerous hands-on examples on how to apply expressive and interactive features to your files using ActionScript code, such as file upload, filter effects, scripted animation, and anti-alias text using the FlashType font rendering engine.

In Learning Actionscript 2.0 for Macromedia Flash 8, learn how to:
•    Write proper ActionScript syntax using the fundamentals of the ActionScript language.
•    Use object-oriented programming techniques to build Flash applications.
•    Use ActionScript classes to add interactivity and expressive features to your Flash applications.
•    Use best practices and coding conventions to optimize, structure, and write consistent ActionScript.

Table of Contents

  1. Copyright
    1. Dedication
  2. Introduction
    1. Intended audience
    2. System requirements
    3. Updating Flash XML files
    4. About the documentation
      1. Learning ActionScript 2.0 book overview
      2. Typographical conventions
      3. Terms used in this document
      4. Copy and paste code
    5. Additional resources
      1. About the sample files
      2. Where to find PDF files or printed documentation
      3. About LiveDocs
      4. Additional online resources
      5. Where to find documentation on other subjects
  3. 1. What’s New in Flash 8 ActionScript
    1. New in ActionScript 2.0 and Flash 8
      1. Additions to the ActionScript language
      2. About deprecated language elements
      3. ActionScript editing changes
    2. Changes to security model for locally installed SWF files
  4. 2. Writing and Editing ActionScript 2.0
    1. About ActionScript and events
      1. Mouse and keyboard events
      2. Clip events
      3. Frame events
    2. Organizing ActionScript code
      1. About writing scripts to handle events
    3. Using the Actions panel and Script window
    4. About the Actions panel
    5. About the Script window
    6. About coding in the Actions panel and Script window
      1. About the Actions panel and Script window toolbars
      2. About ActionScript editing options
      3. About ActionScript preferences
      4. About code hinting in Flash
        1. About triggering code hints
        2. Using code hints
        3. About typing objects to trigger code hints
        4. About using suffixes to trigger code hints
        5. About using comments to trigger code hints
      5. Formatting code
      6. Using syntax highlighting
      7. Using line numbers and word wrap
      8. Using Escape shortcut keys
      9. Showing hidden characters
      10. Using the Find tool
      11. Checking syntax and punctuation
      12. Importing and exporting scripts
        1. Unicode support for ActionScript
        2. Import and export preferences
    7. About Actions panel features
      1. About Script Assist
      2. Pinning scripts in the Actions panel
      3. Inserting target paths
    8. About behaviors
    9. About ActionScript publish settings
      1. Modifying ActionScript publish settings
      2. Modifying the classpath
      3. Configuration files that install with Flash 8
  5. 3. About ActionScript
    1. What is ActionScript
    2. About choosing between ActionScript 1.0 and ActionScript 2.0
    3. Understanding ActionScript and Flash Player
  6. 4. Data and Data Types
    1. About data
    2. About data types
      1. About primitive and complex data types
        1. Boolean data type
        2. MovieClip data type
        3. null data type
        4. Number data type
        5. Object data type
        6. String data type
        7. undefined data type
        8. Void data type
      2. About assigning data types and strict data typing
        1. Assigning a data type
      3. About type checking
      4. About determining data type
    3. About variables
      1. About declaring variables
      2. About default values
      3. About assigning values
      4. About operators and variables
      5. About naming variables
      6. Using variables in an application
        1. Passing a variable by reference
      7. About variables and scope
        1. Global variables
        2. Timeline variables
        3. Local variables
      8. About loading variables
        1. Using variables from the URL
        2. Using FlashVars in an application
        3. Loading variables from a server
      9. Using variables in a project
    4. Organizing data in objects
    5. About casting
      1. About casting objects
  7. 5. Syntax and Language Fundamentals
    1. About syntax, statements, and expressions
      1. Differences between ActionScript and JavaScript
      2. About case sensitivity
    2. About dot syntax and target paths
      1. About using dot syntax to target an instance
        1. Targeting an instance
        2. Targeting a nested instance
        3. Targeting dynamic instances and loaded content
        4. Setting variables using a path
      2. About scope and targeting
      3. Using the Target Path button
      4. About slash syntax
    3. About language punctuators
      1. Semicolons and colons
      2. Curly braces
      3. Parentheses
      4. About literals
      5. About comments
        1. Single-line comments
        2. Multiline comments
        3. Trailing comments
        4. Comments inside classes
    4. About constants and keywords
      1. Using constants
      2. About keywords
      3. About reserved words
    5. About statements
      1. About compound statements
      2. About conditions
        1. About writing conditions
        2. Using the if statement
        3. Using the if..else statement
        4. Using the if..else if statement
        5. Using a switch statement
        6. Using try..catch and try..catch..finally statements
        7. About the conditional operator and alternative syntax
      3. Repeating actions using loops
        1. About creating and ending loops
        2. Using for loops
        3. Using loops
        4. Using while loops
        5. About do..while loops
        6. Using nested loops in your ActionScript
    6. About arrays
      1. Using arrays
        1. About modifying arrays
        2. About referencing and finding length
        3. About adding and removing elements
      2. Creating indexed arrays
      3. Creating multidimensional arrays
      4. Creating associative arrays
    7. About operators
      1. Using operators to manipulate values
      2. About operator precedence and associativity
      3. About using operators with strings
      4. Using dot and array access operators
      5. About postfix operators
      6. About unary operators
      7. About multiplicative operators
      8. About additive operators
      9. Using numeric operators
      10. About relational operators
      11. About equality operators
      12. Using relational and equality operators
      13. About assignment operators
      14. Using assignment operators
      15. About logical operators
      16. Using logical operators
      17. About bitwise shift operators
      18. About bitwise logical operators
      19. Using bitwise operators
      20. About the conditional operator
      21. Using operators in a document
  8. 6. Functions and Methods
    1. About functions and methods
      1. About types of methods and functions
        1. About built-in and top-level functions
        2. Writing named functions
        3. Writing anonymous and callback functions
        4. About function literals
        5. About constructor functions
      2. Defining global and timeline functions
      3. Targeting and calling user-defined functions
      4. Naming functions
      5. Using functions in Flash
      6. Using variables in functions
      7. Passing parameters to a function
      8. Returning values from functions
      9. About nested functions
    2. Understanding methods
      1. Naming methods
  9. 7. Classes
    1. About object-oriented programming and Flash
      1. The benefits of using classes
      2. About packages
        1. A comparison of classes and packages
        2. Working with packages
      3. About values and data types
      4. Object-oriented programming fundamentals
        1. Objects
        2. Instances and class members
        3. Inheritance
        4. Interfaces
        5. Encapsulation
        6. Polymorphism
    2. Writing custom class files
    3. About working with custom classes in an application
      1. About importing class files
        1. About setting and modifying the classpath
          1. Using a global classpath
          2. Using a document-level classpath
      2. How the compiler resolves class references
      3. Using a class file in Flash
      4. Using methods and properties from a class file
        1. About public, private, and static methods and properties (members)
          1. Public methods and properties
          2. Private methods and properties
          3. Static methods and properties
      5. About class members
        1. Using the Singleton design pattern
        2. Using class members
      6. About getter and setter methods
        1. Using getter and setter methods
      7. About dynamic classes
        1. Creating dynamic classes
      8. About using encapsulation
      9. About using the this keyword in classes
    4. Example: Writing custom classes
      1. About general guidelines for creating a class
        1. About naming class files
      2. Creating and packaging your class files
      3. Writing the constructor function
      4. Adding methods and properties
      5. Controlling member access in your classes
      6. Documenting the classes
    5. Example: Using custom class files in Flash
      1. Importing classes and packages
      2. Creating instances of classes in an example
    6. Assigning a class to symbols in Flash
    7. Compiling and exporting classes
      1. Using ASO files
    8. Understanding classes and scope
    9. About top-level and built-in classes
      1. Other language elements
      2. Top-level classes
      3. The flash.display package
      4. The flash.external package
      5. The flash.filters package
      6. The flash.geom package
      7. The package
      8. The flash.text package
      9. The mx.lang package
      10. The System and TextField packages
    10. About working with built-in classes
      1. About creating a new instance of a built-in class
      2. Accessing built-in object properties
      3. About calling built-in object methods
      4. About class (static) members
      5. Excluding classes
      6. Preloading class files
  10. 8. Inheritance
    1. About inheritance
    2. About writing subclasses in Flash
      1. About writing a subclass
        1. Example: Extending the Widget class
      2. Overriding methods and properties
    3. Using polymorphism in an application
  11. 9. Interfaces
    1. About interfaces
      1. About the interface keyword
      2. About naming interfaces
      3. Defining and implementing interfaces
    2. Creating interfaces as data types
    3. Understanding inheritance and interfaces
    4. Example: Using interfaces
    5. Example: Creating a complex interface
  12. 10. Handling Events
    1. Using event handler methods
    2. Using event listeners
      1. Event listener model
      2. Event listener example
    3. Using event listeners with components
    4. Using button and movie clip event handlers
      1. Using on and onClipEvent with event handler methods
      2. Specifying events for on or onClipEvent methods
      3. Attaching or assigning multiple handlers to one object
    5. Broadcasting events from component instances
    6. Creating movie clips with button states
    7. Event handler scope
    8. Scope of the this keyword
    9. Using the Delegate class
  13. 11. Working with Movie Clips
    1. About controlling movie clips with ActionScript
    2. Calling multiple methods on a single movie clip
    3. Loading and unloading SWF files
      1. Specifying a root timeline for loaded SWF files
      2. Loading image files into movie clips
    4. Changing movie clip position and appearance
    5. Dragging movie clips
    6. Creating movie clips at runtime
      1. Creating an empty movie clip
      2. Duplicating or removing a movie clip
      3. Attaching a movie clip symbol to the Stage
    7. Adding parameters to dynamically created movie clips
    8. Managing movie clip depths
      1. Determining the next highest available depth
      2. Determining the instance at a particular depth
      3. Determining the depth of an instance
      4. Swapping movie clip depths
    9. About caching and scrolling movie clips with ActionScript
      1. When to enable caching
        1. When to use bitmap caching
        2. When to avoid using bitmap caching
      2. Caching a movie clip
      3. Setting the background of a movie clip
    10. Using movie clips as masks
      1. About masking device fonts
      2. About alpha channel masking
    11. Handling movie clip events
    12. Assigning a class to a movie clip symbol
    13. Initializing class properties
  14. 12. Working with Text and Strings
    1. About text fields
      1. Using the TextField class
      2. Assigning text to a text field at runtime
      3. About text field instance and variable names
      4. Creating text fields at runtime
      5. About manipulating text fields
        1. Changing a text field’s position
        2. Changing a text field’s dimensions at runtime
    2. About loading text and variables into text fields
      1. Using FlashVars to load and display text
      2. Using LoadVars to load and display text
      3. Loading variables by using LoadVars
      4. Loading and displaying text from an XML document
    3. Using fonts
      1. Embedding characters
      2. Embedding fonts
      3. Creating custom character sets
      4. Using TextField methods with embedded fonts
      5. About sharing fonts
    4. About font rendering and anti-alias text
      1. Font rendering options in Flash
      2. About continuous stroke modulation
      3. Setting anti-alias with ActionScript
      4. Setting tables for fonts
    5. About text layout and formatting
      1. About formatting anti-alias text
      2. Using a grid fit type
      3. Using the TextFormat class
      4. Default properties of new text fields
    6. Formatting text with Cascading Style Sheet styles
      1. Supported CSS properties
      2. Creating a style sheet object
      3. Loading external CSS files
      4. Creating new styles with ActionScript
      5. Applying styles to a TextField object
      6. Applying a style sheet to a TextArea component
      7. Combining styles
      8. Using style classes
      9. Styling built-in HTML tags
      10. An example of using styles with HTML
      11. Using styles to define new tags
      12. An example of using styles with XML
    7. Using HTML-formatted text
      1. Required properties and syntax for using HTML-formatted text
      2. About supported HTML tags
        1. Anchor tag
        2. Bold tag
        3. Break tag
        4. Font tag
        5. Image tag
        6. Italic tag
        7. List item tag
        8. Paragraph tag
        9. Span tag
        10. Text format tag
        11. Underline tag
      3. About supported HTML entities
      4. About embedding images, SWF files, and movie clips in text fields
        1. Embedding SWF and image files
        2. Embedding movie clip symbols
        3. About specifying height and width values
        4. Controlling embedded media with ActionScript
        5. About making hypertext links out of embedded media
    8. Example: Creating scrolling text
    9. About strings and the String class
      1. About the Strings panel
      2. Using the Locale class
      3. Using an input method editor
      4. About the String class
      5. Creating strings
      6. About the escape character
      7. Analyzing and comparing characters in strings
      8. Converting and concatenating strings
      9. Returning substrings
  15. 13. Animation, Filters, and Drawings
    1. Scripting animation with ActionScript 2.0
      1. About animation and frame rate
      2. Fading objects with code
      3. Adding color and brightness effects with code
      4. Moving objects with code
      5. Panning an image with code
    2. About bitmap caching, scrolling, and performance
    3. About the Tween and TransitionManager classes
      1. Adding tweens and transitions to a file in Flash Professional 8 (Flash Professional 8 only)
      2. Animating with the TransitionManager and Tween classes
      3. About easing classes and methods
      4. About the Tween class
      5. Using the Tween class
        1. About continuing animations using the continueTo() method
        2. Creating animations that run continuously
      6. Combining the TransitionManager and Tween classes
    4. Using filter effects
      1. Working with filter packages
      2. Working with filters, caching, and the MovieClip class
      3. About hit detection, rotating, skewing, and scaling filters
      4. Applying filters to object instances and BitmapData instances
      5. About error handling, performance, and filters
    5. Working with filters using ActionScript
      1. Using the blur filter
      2. Using the drop shadow filter
      3. Using the glow filter
      4. Creating gradient glows
      5. Using the bevel filter
      6. About the gradient bevel filter
        1. Applying a gradient bevel filter
      7. Using the color matrix filter
      8. Using the convolution filter
      9. Using the displacement map filter
    6. Manipulating filter effects with code
      1. Adjusting filter properties
      2. Animating a filter by using ActionScript
      3. Using the clone() method
    7. Creating bitmaps with the BitmapData class
    8. About blending modes
      1. Applying blending modes
    9. About operation order
    10. Drawing with ActionScript
      1. Using drawing methods to draw lines, curves, and shapes
      2. Drawing specific shapes
      3. Using complex gradient fills
      4. Using line styles
        1. Setting stroke and caps styles
        2. Setting parameters of line styles
          1. Setting line thickness
          2. Setting line color (rgb)
          3. Setting line alpha
          4. Setting line pixel hinting (pixelHinting)
          5. Setting line scale (noScale)
          6. Setting line caps (capsStyle) and joints (jointStyle)
          7. Setting line miter (miterLimit)
      5. Using Drawing API methods and scripting animation
    11. Understanding scaling and slice guides
      1. Understanding how 9-slice scaling works
      2. Working with 9-slice scaling in ActionScript
  16. 14. Creating Interaction with ActionScript
    1. About events and interaction
    2. Controlling SWF file playback
      1. Jumping to a frame or scene
      2. Playing and stopping movie clips
      3. Jumping to a different URL
    3. Creating interactivity and visual effects
      1. Creating a custom mouse pointer
      2. Getting the pointer position
      3. Capturing keypresses
      4. Setting color values
      5. Creating sound controls
      6. Detecting collisions
      7. Creating a simple line drawing tool
    4. Creating runtime data bindings using ActionScript
      1. Creating bindings between UI components using ActionScript
      2. Using components, bindings, and custom formatters
        1. Formatting data using the CustomFormatter class
      3. Adding and binding components on the Stage
    5. Deconstructing a sample script
  17. 15. Working with Images, Sound, and Video
    1. About loading and working with external media
    2. Loading external SWF and image files
      1. About loading SWF files and the root timeline
    3. About loading and using external MP3 files
      1. Loading an MP3 file
      2. Preloading MP3 files
      3. Reading ID3 tags in MP3 files
    4. Assigning linkage to assets in the library
    5. About using FLV video
      1. Creating a video object
      2. Playing back external FLV files dynamically
      3. Creating a video banner
      4. Preloading FLV files
      5. Working with cue points
        1. Tracing cue points from an FLV file
        2. Using embedded cue points with the FLVPlayback component (Flash Professional only)
        3. Creating cue points with ActionScript to use with components (Flash Professional only)
        4. Adding seek functionality with cue points (Flash Professional only)
      6. Working with metadata
      7. About configuring FLV files for hosting on a server
      8. About targeting local FLV files on Macintosh
    6. About creating progress animations for media files
      1. Creating a progress animation for loading SWF and image files
      2. Creating a progress bar for loading MP3 files with ActionScript
      3. Creating a progress bar for loading FLV files with ActionScript
  18. 16. Working with External Data
    1. Sending and loading variables
      1. Checking for loaded data
      2. Creating a progress bar to display data loading progress
    2. Using HTTP to connect to server-side scripts
      1. Using the LoadVars class
    3. About file uploading and downloading
      1. About FileReference API functionality and security
      2. Adding file upload functionality to an application
    4. About XML
      1. Using the XML class
      2. Using the XMLSocket class
    5. Sending messages to and from Flash Player
      1. Using the fscommand() function
      2. About using JavaScript to control Flash applications
      3. About Flash Player methods
    6. About the External API
      1. Creating interaction with the External API
      2. Controlling Flash Video with the External API
  19. 17. Understanding Security
    1. About compatibility with previous Flash Player security models
    2. About local file security and Flash Player
      1. Understanding local security sandboxes
        1. Local-with-file-system
        2. Local-with-networking
        3. Local-trusted
      2. About Flash Player security settings
      3. About local file security and projector files
      4. About troubleshooting legacy SWF files
      5. Fixing legacy content deployed on local computers
      6. Publishing files for local deployment
        1. Access local files only (default)
        2. Access network only
        3. Access file system and network
      7. Testing content locally with Flash 8 local file security restrictions
        1. Specifying trusted files using the Settings Manager
        2. Creating configuration files for Flash development
      8. About the sandboxType property
      9. About local-with-file-system restrictions
    3. About domains, cross-domain security, and SWF files
      1. Domain name rules for settings and local data
      2. Cross-domain and subdomain access between SWF files
      3. Allowing data access between cross-domain SWF files
    4. Server-side policy files for permitting access to data
      1. Allowing cross-domain data loading
      2. About custom policy file locations
      3. About XMLSocket policy files
    5. HTTP to HTTPS protocol access between SWF files
      1. Allowing HTTP to HTTPS protocol access between SWF files
  20. 18. Debugging Applications
    1. Debugging your scripts
      1. Debugging a SWF file from a remote location
      2. Displaying and modifying variables
      3. Using the Watch list
      4. Displaying movie clip properties and changing editable properties
      5. Setting and removing breakpoints
        1. About the breakpoints XML file
      6. About working through lines of code
    2. Using the Output panel
      1. Listing a SWF file’s objects
      2. Listing a SWF file’s variables
      3. About displaying text field properties for debugging
      4. Using the trace statement
      5. Updating Flash Player for testing
  21. 19. Best Practices and Coding Conventions for ActionScript 2.0
    1. Naming conventions
      1. General naming guidelines
      2. Avoiding reserved words or language constructs
      3. Naming variables
      4. Naming constants
      5. Naming Boolean variables
      6. Naming functions and methods
      7. Naming classes and objects
      8. Naming packages
      9. Naming interfaces
      10. Naming custom components
    2. Using comments in your code
      1. Writing good comments
      2. Adding comments to classes
    3. ActionScript coding conventions
      1. Keeping your ActionScript code in one place
      2. Attaching code to objects
      3. Handling scope
        1. Avoiding absolute targets (_root)
        2. Using _lockroot
        3. Using the this keyword
        4. About scope in classes
      4. Structuring a class file
        1. Guidelines for creating a class
        2. Using the this prefix in class files
        3. About initialization
        4. Use trace statements
        5. About the super prefix
        6. Avoid the with statement
      5. About using functions
        1. About stopping code repetition
    4. ActionScript and Flash Player optimization
      1. Optimizing your code
    5. Formatting ActionScript syntax
      1. General formatting guidelines
      2. Writing conditional statements
      3. Writing compound statements
      4. Writing a for statement
      5. Writing while and do..while statements
      6. Writing return statements
      7. Writing switch statements
      8. Writing try..catch and try..catch..finally statements
      9. About using listener syntax
  22. A. Error Messages
  23. B. Deprecated Flash 4 operators
  24. C. Keyboard Keys and Key Code Values
    1. Letters A to Z and standard numbers 0 to 9
    2. Keys on the numeric keypad
    3. Function keys
    4. Other keys
  25. D. Writing Scripts for Earlier Versions of Flash Player
    1. About targeting earlier versions of Flash Player
    2. Using Flash 8 to create content for Flash Player 4
      1. Using Flash 8 to open Flash 4 files
      2. Using slash syntax
  26. E. Object-Oriented Programming with ActionScript 1.0
    1. About ActionScript 1.0
    2. Creating a custom object in ActionScript 1.0
    3. Assigning methods to a custom object in ActionScript 1.0
    4. Defining event handler methods in ActionScript 1.0
    5. Creating inheritance in ActionScript 1.0
    6. Adding getter/setter properties to objects in ActionScript 1.0
    7. Using Function object properties in ActionScript 1.0
      1. Invoking a function using the method in ActionScript 1.0
      2. Specifying the object to which a function is applied using Function.apply() in ActionScript 1.0
  27. F. Terminology