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 BookThis 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. CompatibilityAs 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 ConventionsActionScript 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.
The CodeThis 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 BookIs 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 KnowWe 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 ForThis 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 ForThis 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 OrganizedThis book consists of the following chapters and appendix.
How to Use This BookThink 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 BookThe following typographical conventions are used in this book:
When referring to properties and methods of objects and classes, the following conventions are used:
In addition, the following formats are used to grab your attention and relieve the tedium of what could otherwise be monotonous reading:
Using Code ExamplesActionScript 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 CookbooksLooking 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:
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® EnabledWhen 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 UsPlease address comments and questions concerning this book to the publisher:
There is a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
The authors maintain a site for the book, located at:
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:
About the Tech ReviewersStacey 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. AcknowledgmentsThis 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 LottThank 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 SchallThank 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 PetersThank 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. |