Preface


The ActionScript 3.0 Cookbook is written with all levels of ActionScript developers in mindpeople like you who want practical solutions to common problems. Keep this book next to your computer to tackle programming challenges. It is filled with exciting yet accessible and practical examples, solutions, and insights into the situations that Flash and ActionScript developers are sure to encounter.

The book is in the style of the classic O'Reilly Cookbook series format, in which each recipe presents the problem, the solution, and a discussion of the solution. You can quickly locate the recipe that most closely matches your situation and get the solution without having to read the whole book to understand the underlying code. The Discussion sections of each recipe offer a deeper analysis of how the solution works and possible design choices and ramifications. So you get the best of both worldsquick and easy access to the answers you want and deeper insights into the nature of both the problem and the solution. The ActionScript 3.0 Cookbook helps you develop your understanding of concepts by applying them in real situations.

What's Not in This Book

This book contains a lot of information on a wide range of topics. It covers the gamut of client-side ActionScript. Every recipe is presented in the context of an applied solution or example. Although the book discusses some minimal theory, it is not intended as an introduction to any of these subjects. There are many good books, references, and documents that discuss ActionScript in a comprehensive fashion. The ActionScript 3.0 Cookbook is not designed to be that sort of book. It is intentionally designed in a particular format to help you with solutions to specific problems.

Compatibility

As the title of the book says, this is the ActionScript 3.0 Cookbook. As such, all of the code examples in this book are based on ActionScript 3.0 and only compatible work with products that support ActionScript 3.0. Flex 2.0 and Flash 9 allow you to author ActionScript 3.0 content. Flash Player 9 supports ActionScript 3.0. If you are using a product that does not support ActionScript 3.0, then the code in this book is not likely to work.

Flash Platform Naming Conventions

ActionScript 3.0 is an important part of the Flash Platform. The Flash Platform is inclusive of many related technologies centering on the Flash Player. There are so many Flash Platform technologies these days that it can be difficult to keep track of all of them. To further complicate matters, many of the terminology are used very informally, generically, and even incorrectly by many developers. Although we don't take a harsh view of such informal use of terminology, we do want to be as clear and precise as possible when discussing these technologies throughout this book; therefore, we use terminology in very specific ways. Table P-1 lists the terms we use and their meanings.

Table P-1. Flash naming conventions used in this book
NameMeaning
Flex frameworkThe library of ActionScript classes that ship as part of the Flex SDK and Flex Builder.
Flex BuilderThe Adobe IDE for authoring Flex applications.
Flex SDKThe compiler and Flex framework used to author Flex applications without Flex Builder.
FlexA technology used to author SWF files from MXML documents and ActionScript files. Unless a version number follows, all mentions of Flex in this book refer to Flex 2.0.
Flash PlayerThe deployment platform for SWF files published from Flash or Flex. Unless a version number follows, all mentions of Flash Player in this book refer to Flash Player 9.
FlashThe authoring tool used to author SWF files. Unless a version number follows, all mentions of Flash in this book refer to Flash 9.


The Code

This book contains a lot of ActionScript 3.0 (or more colloquially, code). Many recipes offer suggested custom classes that we have found to be invaluable. We trust you will find them useful as well.

You can download the complete ActionScript 3.0 libraries from http://www.rightactionscript.com/ascb. Once you've downloaded the library files, you'll need to add them to the classpath for your projects. Directions for setting your ActionScript 3.0 classpath are also found on that web site.

Audience for This Book

Is this book for you? We surely hope it is. But to be sure, let's review the prerequisites and summary of the goals of this book.

What You'll Need to Know

We assume you are already familiar with the product or products you are using to author Flash Player content. This book won't discuss Flex SDK, Flex Builder, or Flash basics. If you don't yet know how to compile a basic project or how to deploy a project, then you'll probably get the most from this book if you first learn the basics of working with the product or products you're using.

You'll also want to learn programming basics before reading this book. Although we talk about a lot of basic programming concepts in the context of how to solve specific problems (e.g., how to loop through the elements of an array) you won't find a step-by-step tutorial in this book with regard to basic programming skills.

Who This Book Is For

This book is for all ActionScript 3.0 developers. We hope there's something in this book for everyone: from novice to expert. If you want to quickly find a solution, this is the perfect book for you.

Who This Book Isn't For

This book discusses ActionScript 3.0, which is a language that runs within Flash Player. For the most part, this book does not discuss server-side solutions or other client-side languages in detail. For example, although this book does discuss the ActionScript code for working with Flash Remoting (a technology for making remote procedure calls) complete with examples, it does not explain how to write the corresponding server-side code (e.g., a ColdFusion component). Likewise, this book discusses how to use ActionScript to call JavaScript functions, yet we don't explain how to write JavaScript. If you are looking for an ActionScript 3.0 book, then this is it; but if you're looking for a book that discusses non-ActionScript topics in detail, this isn't the book you're looking for.

How This Book Is Organized

This book consists of the following chapters and appendix.


Chapter 1, ActionScript Basics

Basic programming tasks such as looping statements, timers, etc.


Chapter 2, Custom Classes

Writing custom classes for use with ActionScript 3.0.


Chapter 3, Runtime Environment

Getting information about the system, device, and player version in use, as well as security functions.


Chapter 4, Numbers and Math

Working with numbers in ActionScript, including parsing numbers from strings, converting numbers to formatted strings, and using different bases for numbers.


Chapter 5, Arrays

Working with indexed collections of data called arrays: from adding and removing elements to sorting.


Chapter 6, Display List

Using display objects to display visual data on the screen.


Chapter 7, Drawing and Masking

Programmatic drawing and masking using ActionScript.


Chapter 8, Bitmaps

Working with low-level bitmap data.


Chapter 9, Text

Everything text, from displaying text to loading text to formatting text.


Chapter 10, Filters and Transforms

Applying effects to display objects by using transforms (color and geometric) and filters such as drop shadows, bevels, and even emboss and edge detection effects.


Chapter 11, Programmatic Animation

Animating display objects using ActionScript.


Chapter 12, Strings

Working with string data: from finding substrings to working with Unicode.


Chapter 13, Regular Expressions

Using native regular expressions to match patterns in strings.


Chapter 14, Dates and Times

Working with dates and times, including converting between timezones, using timers, and formatting dates.


Chapter 15, Programming Sound

Working with audio, including loading MP3s, reading ID3 tags, and displaying sound waves.


Chapter 16, Video

Programming for Flash video.


Chapter 17, Storing Persistent Data

Using shared objects to store data on the client computer.


Chapter 18, Communicating with Other Movies

Using local connections to communicate between content running in Flash Player instance on the same computer.


Chapter 19, Sending and Loading Data

Enabling two-way communication with a web server and the Flash Player.


Chapter 20, XML

Working with XML using the E4X support in Flash Player.


Chapter 21, Web Services and Flash Remoting

Working with remote procedure calls using web services and Flash Remoting technologies.


Chapter 22, Building Integrated Applications

Using the native Flash Player programming interface for integrating Flash Player content with the host application, such as calling JavaScript functions from ActionScript or calling ActionScript functions from JavaScript.


Chapter 23, File Management

Uploading and downloading files.


Chapter 24, Socket Programming

Working with XML and binary sockets for low-latency applications.


Appendix, Unicode Escape Sequences for Latin 1 Characters

This appendix lists the characters in the Latin 1 character repertoire, with Unicode equivalents in the range of U+0000 to U+00FF (that is, C0 Controls, Basic Latin, C1 Controls, and Latin 1 Supplemental).

How to Use This Book

Think of this book like a friend and a counselor. Don't put it on a shelf. Keep it on your desk where you can consult it often. When you are uncertain as to how something works or how to approach a specific programming issue pick up the book and flip to the relevant recipe(s). We have written this book in a format so that you can get answers to specific questions quickly. And since it's a book you don't ever have to worry that it will laugh at you for asking questions. No question is too big or too small.

Although you can read the book from cover to cover, we encourage you to use this book when you need an answer. Rather than teaching you a bunch of theory, this book intends to help you solve problems and accomplish tasks. This book is meant for field work, not the research lab.

Conventions Used in This Book

The following typographical conventions are used in this book:


Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).


Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.


Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands.


Constant width bold

Shows commands or other text that should be typed literally by the user.


Constant width italic

Shows text that should be replaced with user-supplied values.

When referring to properties and methods of objects and classes, the following conventions are used:

  • Class-level constants are shown with the both the class name and property in constant width because they should both be entered verbatim. For example, Event.COMPLETE.

  • Instance-level properties are shown with the class or object instance in constant width italic because they should be replaced by a specific instance. The property itself is shown in constant width and should be entered as shown. For example, Button .enabled.

  • Method and function names, and the class or object to which they pertain, are always shown in italics and followed by parentheses, as in BitmapData.clone( ). Refer to the online help to know whether to include the class name literally (i.e., if it is a so-called static method), as in String.fromCharCode( ), or replace it with an instance name, such as exampleBitmap.clone( ).

  • For brevity, we often omit the class name when discussing a property or method of a class. For example, if discussing the htmlText property of the TextField class, when we say "set the htmlText property," you should infer from context that we mean, "set the exampleTextField .htmlText property, where exampleTextField is the identifier for your particular text field."

In addition, the following formats are used to grab your attention and relieve the tedium of what could otherwise be monotonous reading:

This icon signifies a tip, suggestion, or general advice.


This icon indicates a warning or caution.


Using Code Examples

ActionScript 3.0 Cookbook is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product's documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "ActionScript 3.0 Cookbook, by Joey Lott, Darron Schall, and Keith Peters. Copyright 2007 O'Reilly Media, Inc., 978-0-596-52695-5."

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

O'Reilly Cookbooks

Looking for the right ingredients to solve a programming problem? Look no further than O'Reilly Cookbooks. Each cookbook contains hundreds of programming recipes, and includes hundreds of scripts, programs, and command sequences you can use to solve specific problems.

The recipes you'll find in an O'Reilly Cookbook follow a simple formula:


Problem

Each Problem addressed in an O'Reilly Cookbook is clearly stated, specific, and practical.


Solution

The Solution is easy to understand and implement.


Discussion

The Discussion clarifies and explains the context of the Problem and the Solution. It also contains sample code to show you how to get the job done. Best of all, all of the sample code you see in an O'Reilly Cookbook can be downloaded from the book's web site, at http://www.oreilly.com/catalog/actscpt3ckbk.


See Also

The See Also section directs you to additional information related to the topic covered in the recipe. You'll find pointers to other recipes in the book, to other books (including non-O'Reilly titles), web sites, and more.

To learn more about the O'Reilly Cookbook series, or to find other Cookbooks that are up your alley, visit their web site at http://cookbooks.oreilly.com.

Safari® Enabled

When you see a Safari®-enabled icon on the cover of your favorite technology book, that means it's available online through the O'Reilly Network Safari Bookshelf.

Safari offers a solution that's better than e-books. It's a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O'Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

There is a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/actscpt3ckbk

The authors maintain a site for the book, located at:

http://www.rightactionscript.com/ascb

To comment or ask technical questions about this book, send email to:

bookquestions@oreilly.com

For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:

http://www.oreilly.com

About the Tech Reviewers

Stacey Mulcahy is a Flex and Flash developer who once had a torrid love affair with Macromedia Director. When she's not building Rich Internet Applications that even designers like, she can be found showing others how to do the same thing as a Flash instructor. She rants more than raves on her Flash-centric blog: http://www.bitchwhocodes.com.

Sam Robbins has been working with Flash and ActionScript for more than six years and has been experimenting with Flex for almost two. During that time, most of his efforts have been in developing Rich Internet Applications. Sam also started a sub-group of the Boston Flash Platform User Group (BFPUG), which examines design patterns on a monthly basis. In his spare time he tries to update his blog (http://pixelconsumption.com), plays Xbox, and cuts his lawn. He lives in Massachusetts with his fiancée Andrea.

Steven Schelter is a software developer currently employed at Schematic. He's fluent in various front- and backend scripting languages and applications, but his primary concentration is ActionScript development. Steven was pulled into interactive media industry from Flash's appeal as a tool to merge his eye for design with his background in math and logistics. Other interests include 3D modeling, artificial intelligence, and user interactivity.

Roger Braunstein is a developer and designer living in Brooklyn. He is obsessed with beautiful code, motion design, graphics programming, and video games. He also cultivates an interest in cooking, photography, biking, and 8-bit music. One day he hopes to have a real web site at http://www.partlyhuman.com.

Muon Thi Van is a software developer at Schematic, a full-service interactive agency with offices in Los Angeles and New York City. She has served as architect, developer, and designer on numerous web and mobile-based Flash applications and games and has spoken at industry conferences such as Flashbelt and Flashforward. Muon holds a BS in computer science from Northwestern University.

Daniel Williams is a software developer at Schematic in New York, where he lives and breathes ActionScript. During those really late nights he dreams it as well. On the side, he has an avid interest in physics, fast two-wheeled vehicles, and the human brain. Occasionally, he'll ramble about his thoughts and experiences on his personal web site, the inappropriately named http://www.danieldoesdallas.com.

Acknowledgments

This book is the work of three authorsJoey Lott, Darron Schall, and Keith Peterswhose names are on the cover. However, this book certainly would not be possible without the help of many people whose names don't go on the cover. We'd like to extend our collective thanks and gratitude to the following people.

Thank you Chuck Toporek for your dedication, patience, and fantastic editing.

Likewise, thanks to Steve Weiss for believing in this book and its authors. We thank you for always advocating for what is best for us and the book.

We'd also like to thank Tim O'Reilly for raising the bar in technical publishing and for making this book possible in this format. We are honored to work with such a progressive and forward-thinking company.

Without the many efforts of the entire editorial and support staff at O'Reilly this book would not be where it is today. Thank you to everyone who's time and energy went into this book.

We'd like to thank our agent, Margot Hutchinson at Waterside Productions, for helping coordinate all the necessary details.

The entire Flex, Flash, and Flash Player teams at Adobe are always invaluable and incredibly helpful. They answer our questions so we can answer yours. Thank you to everyone at Adobe.

And we'd also like to thank all the technical reviewers for their assistance in making this book the best it can be.

Joey Lott

Thank you, Keith and Darron, for your help with this book. I am honored to work with two of the foremost experts in this industry.

I'd also like to thank my friends and family for all support and encouragement. And I'd like to thank life for all the support it provides in both expected and unexpected ways.

Darron Schall

Thank you, Joey, for allowing me to help fill in these pages. Keith, thanks for sharing the work as well. You both have been great to write with, and I'm proud to be your co-author. Chuck and Steve, your support was incalculable, and I definitely couldn't have done this without your help.

To my beautiful wife Jen, thank you for your understanding and tireless patience. I love you.

To all of my familyespecially my grandfather Edwinthanks for your encouragement and thanks for believing in me.

Keith Peters

Thank you, Joey, for the opportunity to be part of this project. Thanks also to Steve, Chuck, and Darron for help along the way, and as usual Kazumi and Kristine for putting up with me glued to the monitor all too often.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net