ActionScript 3.0 Cookbook

book cover
ActionScript 3.0 Cookbook
By Joey Lott, Darron Schall, Keith Peters
Publisher: O'Reilly
Pub Date: October 01, 2006
ISBN: 0-596-52695-4
Pages: 592

Table of Contents  | Index

Well before Ajax and Microsoft's Windows Presentation Foundation hit the scene, Macromedia offered the first method for building web pages with the responsiveness and functionality of desktop programs with its Flash-based "Rich Internet Applications". Now, new owner Adobe is taking Flash and its powerful capabilities beyond the Web and making it a full-fledged development environment.

Rather than focus on theory, the ActionScript 3.0 Cookbook concentrates on the practical application of ActionScript, with more than 300 solutions you can use to solve a wide range of common coding dilemmas. You'll find recipes that show you how to:

  • Detect the user's Flash Player version or their operating system

  • Build custom classes

  • Format dates and currency types

  • Work with strings

  • Build user interface components

  • Work with audio and video

  • Make remote procedure calls using Flash Remoting and web services

  • Load, send, and search XML data

  • And much, much more ...

Each code recipe presents the Problem, Solution, and Discussion of how you can use it in other ways or personalize it for your own needs, and why it works. You can quickly locate the recipe that most closely matches your situation and get the solution without reading the whole book to understand the underlying code. Solutions progress from short recipes for small problems to more complex scripts for thornier riddles, and the discussions offer a deeper analysis for resolving similar issues in the future, along with possible design choices and ramifications. You'll even learn how to link modular ActionScript pieces together to create rock-solid solutions for Flex 2 and Flash applications.

When you're not sure how ActionScript 3.0 works or how to approach a specific programming dilemma, you can simply pick up the book, flip to the relevant recipe(s), and quickly find the solution you're looking for.

book cover
ActionScript 3.0 Cookbook
By Joey Lott, Darron Schall, Keith Peters
Publisher: O'Reilly
Pub Date: October 01, 2006
ISBN: 0-596-52695-4
Pages: 592

Table of Contents  | Index

    Chapter 1.  ActionScript Basics
      Section 1.0.  Introduction
      Recipe 1.1.  Creating an ActionScript Project
      Recipe 1.2.  Customizing the Properties of an Application
      Recipe 1.3.  Where to Place ActionScript Code
      Recipe 1.4.  How to Trace a Message
      Recipe 1.5.  Handling Events
      Recipe 1.6.  Responding to Mouse and Key Events
      Recipe 1.7.  Using Mathematical Operators
      Recipe 1.8.  Checking Equality or Comparing Values
      Recipe 1.9.  Performing Actions Conditionally
      Recipe 1.10.  Performing Complex Conditional Testing
      Recipe 1.11.  Repeating an Operation Many Times
      Recipe 1.12.  Repeating a Task over Time
      Recipe 1.13.  Creating Reusable Code
      Recipe 1.14.  Generalizing a Method to Enhance Reusability
      Recipe 1.15.  Exiting a Method
      Recipe 1.16.  Obtaining the Result of a Method
      Recipe 1.17.  Handling Errors
    Chapter 2.  Custom Classes
      Section 2.0.  Introduction
      Recipe 2.1.  Creating a Custom Class
      Recipe 2.2.  Determining Where to Save a Class
      Recipe 2.3.  Creating Properties That Behave As Methods
      Recipe 2.4.  Creating Static Methods and Properties
      Recipe 2.5.  Creating Subclasses
      Recipe 2.6.  Implementing Subclass Versions of Superclass Methods
      Recipe 2.7.  Creating Constants
      Recipe 2.8.  Dispatching Events
    Chapter 3.  Runtime Environment
      Section 3.0.  Introduction
      Recipe 3.1.  Detecting the Player Version
      Recipe 3.2.  Detecting the Operating System
      Recipe 3.3.  Checking the Player Type
      Recipe 3.4.  Checking the System Language
      Recipe 3.5.  Detecting Display Settings
      Recipe 3.6.  Scaling the Movie
      Recipe 3.7.  Changing the Alignment
      Recipe 3.8.  Hiding the Flash Player's Menu Items
      Recipe 3.9.  Detecting the Device's Audio Capabilities
      Recipe 3.10.  Detecting the Device's Video Capabilities
      Recipe 3.11.  Prompting the User to Change Player Settings
      Recipe 3.12.  Dealing with System Security
    Chapter 4.  Numbers and Math
      Section 4.0.  Introduction
      Recipe 4.1.  Representing Numbers in Different Bases
      Recipe 4.2.  Converting Between Different Number Systems
      Recipe 4.3.  Rounding Numbers
      Recipe 4.4.  Inserting Leading or Trailing Zeros or Spaces
      Recipe 4.5.  Formatting Numbers for Display Without a Mask
      Recipe 4.6.  Formatting Currency Amounts
      Recipe 4.7.  Generating a Random Number
      Recipe 4.8.  Simulating a Coin Toss
      Recipe 4.9.  Simulating Dice
      Recipe 4.10.  Simulating Playing Cards
      Recipe 4.11.  Generating a Unique Number
      Recipe 4.12.  Converting Angle Measurements
      Recipe 4.13.  Calculating the Distance Between Two Points
      Recipe 4.14.  Determining Points Along a Circle
      Recipe 4.15.  Converting Between Units of Measurement
    Chapter 5.  Arrays
      Section 5.0.  Introduction
      Recipe 5.1.  Adding Elements to the Start or End of an Array
      Recipe 5.2.  Looping Through an Array
      Recipe 5.3.  Searching for Matching Elements in an Array
      Recipe 5.4.  Removing Elements
      Recipe 5.5.  Inserting Elements in the Middle of an Array
      Recipe 5.6.  Converting a String to an Array
      Recipe 5.7.  Converting an Array to a String
      Recipe 5.8.  Creating a Separate Copy of an Array
      Recipe 5.9.  Storing Complex or Multidimensional Data
      Recipe 5.10.  Sorting or Reversing an Array
      Recipe 5.11.  Implementing a Custom Sort
      Recipe 5.12.  Randomizing the Elements of an Array
      Recipe 5.13.  Getting the Minimum or Maximum Element
      Recipe 5.14.  Comparing Arrays
      Recipe 5.15.  Creating an Associative Array
      Recipe 5.16.  Reading Elements of an Associative Array
    Chapter 6.  Display List
      Section 6.0.  Introduction
      Recipe 6.1.  Adding an Item to the Display List
      Recipe 6.2.  Removing an Item from the Display List
      Recipe 6.3.  Moving Objects Forward and Backward
      Recipe 6.4.  Creating Custom Visual Classes
      Recipe 6.5.  Creating Simple Buttons
      Recipe 6.6.  Loading External Images at Runtime
      Recipe 6.7.  Loading and Interacting with External Movies
      Recipe 6.8.  Creating Mouse Interactions
      Recipe 6.9.  Dragging and Dropping Objects with the Mouse
    Chapter 7.  Drawing and Masking
      Section 7.0.  Introduction
      Recipe 7.1.  Setting a Line Style
      Recipe 7.2.  Setting Gradient Line Styles
      Recipe 7.3.  Drawing a Line
      Recipe 7.4.  Drawing a Curve
      Recipe 7.5.  Drawing an Arc
      Recipe 7.6.  Drawing a Rectangle
      Recipe 7.7.  Drawing a Circle
      Recipe 7.8.  Drawing an Ellipse
      Recipe 7.9.  Drawing a Triangle
      Recipe 7.10.  Drawing Regular Polygons
      Recipe 7.11.  Drawing a Star
      Recipe 7.12.  Filling a Shape with a Solid or Translucent Color
      Recipe 7.13.  Filling a Shape with a Gradient
      Recipe 7.14.  Filling a Shape with a Bitmap
      Recipe 7.15.  Scripting Masks
    Chapter 8.  Bitmaps
      Section 8.0.  Introduction
      Recipe 8.1.  Creating a BitmapData Object
      Recipe 8.2.  Adding a Bitmap to the Display List
      Recipe 8.3.  Drawing a Display Object to a Bitmap
      Recipe 8.4.  Loading an External Image into a Bitmap
      Recipe 8.5.  Manipulating Pixels
      Recipe 8.6.  Creating Rectangular Fills
      Recipe 8.7.  Creating a Flood Fill
      Recipe 8.8.  Copying Pixels
      Recipe 8.9.  Copying Channels
      Recipe 8.10.  Creating Noise
      Recipe 8.11.  Creating Perlin Noise
      Recipe 8.12.  Using Threshold
      Recipe 8.13.  Applying a Filter to a Bitmap
      Recipe 8.14.  Dissolving Between Two Bitmaps
      Recipe 8.15.  Scrolling a Bitmap
    Chapter 9.  Text
      Section 9.0.  Introduction
      Recipe 9.1.  Creating an Outline Around a Text Field
      Recipe 9.2.  Creating a Background for a Text Field
      Recipe 9.3.  Making a User Input Field
      Recipe 9.4.  Making a Password Input Field
      Recipe 9.5.  Filtering Text Input
      Recipe 9.6.  Setting a Field's Maximum Length
      Recipe 9.7.  Displaying Text
      Recipe 9.8.  Displaying HTML-Formatted Text
      Recipe 9.9.  Condensing Whitespace
      Recipe 9.10.  Sizing Text Fields to Fit Contents
      Recipe 9.11.  Scrolling Text Programmatically
      Recipe 9.12.  Responding to Scroll Events
      Recipe 9.13.  Formatting Text
      Recipe 9.14.  Formatting User-Input Text
      Recipe 9.15.  Formatting a Portion of Existing Text
      Recipe 9.16.  Setting a Text Field's Font
      Recipe 9.17.  Embedding Fonts
      Recipe 9.18.  Creating Text that Can Be Rotated
      Recipe 9.19.  Displaying Unicode Text
      Recipe 9.20.  Assigning Focus to a Text Field
      Recipe 9.21.  Selecting Text with ActionScript
      Recipe 9.22.  Setting the Insertion Point in a Text Field
      Recipe 9.23.  Responding When Text Is Selected or Deselected
      Recipe 9.24.  Responding to User Text Entry
      Recipe 9.25.  Adding a Hyperlink to Text
      Recipe 9.26.  Calling ActionScript from Hyperlinks
      Recipe 9.27.  Working with Advanced Text Layout
      Recipe 9.28.  Applying Advanced Anti-Aliasing
      Recipe 9.29.  Replacing Text
      Recipe 9.30.  Retrieving a List of System Fonts
    Chapter 10.  Filters and Transforms
      Section 10.0.  Introduction
      Recipe 10.1.  Applying Color Changes
      Recipe 10.2.  Applying Color Tints
      Recipe 10.3.  Resetting Color
      Recipe 10.4.  Shearing
      Recipe 10.5.  Applying Basic Filters
      Recipe 10.6.  Applying Advanced Filter Effects (Emboss, etc.)
      Recipe 10.7.  Embossing
      Recipe 10.8.  Detecting Edges
      Recipe 10.9.  Sharpening
      Recipe 10.10.  Making a Digital Negative
      Recipe 10.11.  Applying Grayscale
      Recipe 10.12.  Changing Saturation
      Recipe 10.13.  Changing Brightness
      Recipe 10.14.  Changing Contrast
    Chapter 11.  Programmatic Animation
      Section 11.0.  Introduction
      Recipe 11.1.  Moving an Object
      Recipe 11.2.  Moving an Object in a Specific Direction
      Recipe 11.3.  Easing
      Recipe 11.4.  Acceleration
      Recipe 11.5.  Springs
      Recipe 11.6.  Using Trigonometry
      Recipe 11.7.  Applying Animation Techniques to Other Properties
    Chapter 12.  Strings
      Section 12.0.  Introduction
      Recipe 12.1.  Joining Strings
      Recipe 12.2.  Using Quotes and Apostrophes in Strings
      Recipe 12.3.  Inserting Special Whitespace Characters
      Recipe 12.4.  Searching for a Substring
      Recipe 12.5.  Extracting a Substring
      Recipe 12.6.  Parsing a String into Words
      Recipe 12.7.  Removing and Replacing Characters and Words
      Recipe 12.8.  Retrieving One Character at a Time
      Recipe 12.9.  Converting Case
      Recipe 12.10.  Trimming Whitespace
      Recipe 12.11.  Reversing a String by Word or by Character
      Recipe 12.12.  Converting Between Strings and Unicode or ASCII
    Chapter 13.  Regular Expressions
      Section 13.0.  Introduction
      Recipe 13.1.  Understanding Regular Expression Patterns
      Recipe 13.2.  Testing Regular Expressions
      Recipe 13.3.  Looking for Pattern Matches
      Recipe 13.4.  Removing and Replacing Characters and Words Using Patterns
      Recipe 13.5.  Creating a Nongreedy Pattern
      Recipe 13.6.  Validating User Input with Common Patterns
    Chapter 14.  Dates and Times
      Section 14.0.  Introduction
      Recipe 14.1.  Finding the Current Date and Time
      Recipe 14.2.  Retrieving the Date Values
      Recipe 14.3.  Retrieving the Day or Month Name
      Recipe 14.4.  Formatting the Date and Time
      Recipe 14.5.  Formatting Seconds or Milliseconds as Minutes and Seconds
      Recipe 14.6.  Converting Between DMYHMSM and Epoch Milliseconds
      Recipe 14.7.  Using Timers
      Recipe 14.8.  Calculating Elapsed Time or Intervals Between Dates
      Recipe 14.9.  Parsing a Date from a String
    Chapter 15.  Programming Sound
      Section 15.0.  Introduction
      Recipe 15.1.  Creating a Sound Object and Loading a Sound
      Recipe 15.2.  Starting and Stopping a Sound
      Recipe 15.3.  Setting the Buffer for a Sound
      Recipe 15.4.  Offsetting the Start of a Sound
      Recipe 15.5.  Playing a Sound Multiple Times (Looping)
      Recipe 15.6.  Getting the Size of a Sound File
      Recipe 15.7.  Reading the ID3 Tag of a Sound File
      Recipe 15.8.  Find Out When a Sound Finishes Playing
      Recipe 15.9.  Tracking the Progress of a Playing Sound
      Recipe 15.10.  Pausing and Restarting a Sound
      Recipe 15.11.  Reading the Level of a Sound
      Recipe 15.12.  Stopping All Sounds
      Recipe 15.13.  Reading the Sound Spectrum
      Recipe 15.14.  Changing the Volume or Pan of a Sound
      Recipe 15.15.  Creating a Sound Application
    Chapter 16.  Video
      Section 16.0.  Introduction
      Recipe 16.1.  Loading and Playing Back Video
      Recipe 16.2.  Controlling Video Sound
      Recipe 16.3.  Reading Playback Time
      Recipe 16.4.  Reading Video Duration
      Recipe 16.5.  Controlling Playback Time
      Recipe 16.6.  Scaling Video
      Recipe 16.7.  Managing and Monitoring Buffering and Loading
      Recipe 16.8.  Listening for Cue Points
      Recipe 16.9.  Applying Filters to Video
      Recipe 16.10.  Pausing and Resuming Video
      Recipe 16.11.  Stopping Video
      Recipe 16.12.  Scrubbing Video
      Recipe 16.13.  Clearing the Video Display
      Recipe 16.14.  Determining User Bandwidth
    Chapter 17.  Storing Persistent Data
      Section 17.0.  Introduction
      Recipe 17.1.  Creating and Opening a Local Shared Object
      Recipe 17.2.  Writing Data to a Shared Object
      Recipe 17.3.  Saving a Local Shared Object
      Recipe 17.4.  Reading Data from a Shared Object
      Recipe 17.5.  Removing Data from a Shared Object
      Recipe 17.6.  Serializing Custom Classes
      Recipe 17.7.  Sharing Data Between Flash Applications
      Recipe 17.8.  Controlling the Size of Local Shared Objects
    Chapter 18.  Communicating with Other Movies
      Section 18.0.  Introduction
      Recipe 18.1.  Creating Local Connections
      Recipe 18.2.  Sending Data
      Recipe 18.3.  Validating Receipt of Communication over Local Connections
      Recipe 18.4.  Accepting Local Communications from Other Domains
    Chapter 19.  Sending and Loading Data
      Section 19.0.  Introduction
      Recipe 19.1.  Loading Variables from a Text File
      Recipe 19.2.  Loading Variables from a Server-Side Script
      Recipe 19.3.  Loading a Block of Text (Including HTML and XML)
      Recipe 19.4.  Checking Load Progress
      Recipe 19.5.  Accessing Data Being Downloaded
      Recipe 19.6.  Sending Data to a Server-Side Script
      Recipe 19.7.  Sending Variables and Handling a Returned Result
    Chapter 20.  XML
      Section 20.0.  Introduction
      Recipe 20.1.  Understanding XML Structure (Reading and Writing XML)
      Recipe 20.2.  Creating an XML Object
      Recipe 20.3.  Adding Elements to an XML Object
      Recipe 20.4.  Adding Text Nodes to an XML Object
      Recipe 20.5.  Adding Attributes to an XML Element
      Recipe 20.6.  Reading Elements in an XML Tree
      Recipe 20.7.  Finding Elements by Name
      Recipe 20.8.  Reading Text Nodes and Their Values
      Recipe 20.9.  Reading an Element's Attributes
      Recipe 20.10.  Removing Elements, Text Nodes, and Attributes
      Recipe 20.11.  Loading XML
      Recipe 20.12.  Loading XML from Different Domains
      Recipe 20.13.  Sending XML
      Recipe 20.14.  Searching XML
      Recipe 20.15.  Using HTML and Special Characters in XML
    Chapter 21.  Web Services and Flash Remoting
      Section 21.0.  Introduction
      Recipe 21.1.  Calling Web Services Methods
      Recipe 21.2.  Handling Web Services Responses
      Recipe 21.3.  Handling Web Services Errors
      Recipe 21.4.  Calling Flash Remoting Methods
      Recipe 21.5.  Handling Flash Remoting Responses
    Chapter 22.  Building Integrated Applications
      Section 22.0.  Introduction
      Recipe 22.1.  Calling JavaScript Functions
      Recipe 22.2.  Calling ActionScript Functions
      Recipe 22.3.  Passing Parameters from HTML
    Chapter 23.  File Management
      Section 23.0.  Introduction
      Recipe 23.1.  Downloading Files
      Recipe 23.2.  Detecting When a User Selects a File to Upload
      Recipe 23.3.  Monitoring Download Progress
      Recipe 23.4.  Browsing for Files
      Recipe 23.5.  Filtering Files That Display in the Browser Window
      Recipe 23.6.  Detecting When the User Has Selected a File to Upload
      Recipe 23.7.  Uploading Files
      Recipe 23.8.  Monitoring File Upload Progress
    Chapter 24.  Socket Programming
      Section 24.0.  Introduction
      Recipe 24.1.  Connecting to a Socket Server
      Recipe 24.2.  Sending Data
      Recipe 24.3.  Receiving Data
      Recipe 24.4.  Handshaking with a Socket Server
      Recipe 24.5.  Disconnecting from a Socket Server
      Recipe 24.6.  Handling Socket Errors
    Appendix A.  Unicode Escape Sequences for Latin 1 Characters