Contents ix Part V Input/Output 359 Chapter 13 Loading Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Loading SWFs and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Loading Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Communicating with Loaded SWFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Additional Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Chapter 14 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Understanding XML Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Creating an XML Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 Using Variables in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Reading XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Deleting XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Loading External XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Sending to and Loading from a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 An XML-Based Navigation System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405 What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> xi When deciding if the book in your hands will be a good resource for your library, it might help you to know why we, the authors, wrote this particular book. We are both developers who use ActionScript extensively in our every- day work, but we are also teachers. Collectively, we have taught thousands of stu dents at multiple universities, training facilities, and conferences, and yet we share one significant common experience. We were consistently told that no feature-rich ActionScript book existed that didn’t assume readers already had extensive programming experience and an understanding of object- oriented programming. So, we started to research how we could fill this void and provide a book to our students that would really help them beyond the classroom. We talked with a lot of students, user groups, and instructors and began to sketch out a book that would put what we learned into practice. When ActionScript 3.0 was released, the interest in the language grew dra- matically. In the Flash community reactions ranged from excitement to uncertainty to fear, as the ActionScript 3.0 learning curve became apparent. Talk of the Flash Platform splintering into Flex (“developer”) and Flash (“designer”) camps left many designers and programmers more uncertain than ever about their futures. When Flash CS3 Professional was released, the need for a guiding resource increased, and we knew it was time to write the book you hold in your hands. We hope this book will help ActionScript coders of all kinds—from curious to intimi dated, from eager to experienced—embrace the power and perfor- mance of ActionScript 3.0. We hope these pages will ease the transition from whatever prior version of ActionScript you may have used (if any) to 3.0—the biggest architectural change to the language since its inception. PrefaCe Download from Wow! eBook <www.wowebook.com> Preface xii Who This Book Is For This book is aimed at designers and developers without extensive ActionScript 3.0 experience. Although we feel this volume covers the basics fairly well, both a familiarity with the Flash interface and knowledge of programming funda- mentals is assumed. We’ve tried to explain the material herein clearly and concisely enough for any reader with at least this minimal background. However, we recom- mend that you skim Chapter 2 to see if you think we’ve provided enough core programming fundamentals to fill any gaps in your knowledge base. Throughout this book we cover relevant syntax with extensive comments, but the first two chapters serve as a foundation upon which the rest of the chapters are built. Similarly, if you are a relatively experienced ActionScript 2.0 programmer, you may wish to glance at a few chapters of interest before deciding whether or not this book is for you. Migration from ActionScript 2.0-to-ActionScript 3.0 is not our primary focus, so we want you to be happy with the tone and straightforward approach we’ve adopted before you decide to rely solely on this book. If you need additional support with the Flash Professional interface, want solutions to specific problems, or would benefit from a quick look at migra- tion issues, consider augmenting this book with the ActionScript Quick Reference Guide by David Stiller, Rich Shupe, Jen deHaan, and Darren Richardson (O’Reilly). The book is divided into two halves, starting with interface-centric material and culminating with a series of recipe-style problem-solving chapters, including one that focuses on ActionScript 2.0 to 3.0 migration. Push Yourself Although this book was written for a reader still finding his or her way with ActionScript 3.0, we’ve tried to include exercises throughout the book that encourage you to push yourself. When exercises move somewhat beyond the basics of a topic, we’ve identified them with this icon: We’ve also tried to mention additional exercises and resources from the companion website (which we’ll talk about in a moment) that may help you continue your explorations. In most cases, these exercises and notes are not central to understanding syntax or a topic as a whole. If you find any of these inclusions to be too much to digest, feel free to skip them and come back to them later. Between these two supplemental efforts, we hope this book will be useful to a wide variety of scripters and allow you to progress along the ActionScript 3.0 learning curve quicker than expected. P u s h Y o u r s e l f ! Download from Wow! eBook <www.wowebook.com> Preface xiii ActionScript Editors Although we try to remain ActionScript-editor neutral whenever possible, the examples in this book were created in Flash Professional. We’ve provided source files that are compatible with the oldest version of Flash Professional that the applicable feature will allow. Most are compatible with Flash CS3 Professional, some require later versions of the tool, and some require Flash Player 10.1, the latest version as of this writing. However, we’ve also tried to provide files for users that are working with other ActionScript editors, like Adobe’s Flash Builder, Powerflasher’s FDT, or the open-source FlashDevelop (Windows-only). These class-based files may also be useful to readers who already have experience with object-oriented programming. Despite these efforts, it’s very important to understand that these supplemental files will not be actively supported. You should buy this book knowing that many of the source files are in FLA format and, even if you typed in the scripts yourself, some rely on assets found in the libraries of these FLA files. If you are not a Flash Professional user, you may need to recreate these scripts and assets as best you can. How This Book Is Organized Unlike any other book on ActionScript 3.0 that we’ve seen, this book does not rely extensively on object-oriented programming (OOP) principles. If you are unfamiliar with this term, don’t worry. You have the correct book in your hands, and you’ll learn more with each successive chapter. We demonstrate key chapter concepts using focused syntax that’s executable within the Flash Professional timeline and gradually introduce OOP con- cepts along the way. The first five chapters—including coverage of the new ActionScript 3.0 event model and means of displaying content (the display list)—do not introduce more than a modicum of content that is class- or OOP-related. Starting in Chapter 6, we provide increased object-oriented coverage, beginning with an OOP primer, and continuing for the remaining chapters with select class- or OOP-based applied examples. This book was designed to be read linearly. Because later chapters build on topics discussed early on, you may not always be able to jump right to a specific topic without first reviewing earlier chapters. If you’re looking for specific solutions to specific problems, take a look at the ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, and Keith Peters (O’Reilly). Download from Wow! eBook <www.wowebook.com> Preface xiv What Is—and Isn’t—In This Book We’ve tried to design a book that covers as many ActionScript essentials as we could include, even while being constrained by a page count designed to keep the book affordable. What’s In Part I: Getting Started Part I begins with Chapter 1, discussing ActionScript 1.0, 2.0, and 3.0, and how the different versions are used in the Flash Professional application and Flash Player. It concludes with Chapter 2 looking at the building blocks that are ActionScript’s version-neutral core fundamentals. Part II: Graphics and Interaction Chapter 3 leads off Part II with explanations of the basic vocabulary of ActionScript: properties, methods, and events (including ActionScript 3.0’s significantly different event model). Chapter 4 focuses on displaying content dynamically, which is also a big departure from prior versions of the language. Chapter 5 covers timeline control, and Chapter 6 introduces OOP. Chapter 7 discusses animating objects using ActionScript, and Chapters 8 and 9 explain drawing with code. Part III: Text Chapter 10 is the only chapter in Part III and focuses on text formatting, HTML support, and the use of Cascading Style Sheets. Part IV: Sound and Video Chapter 11 opens Part IV with a discussion about sound. In addition to manipulating internal and external sounds, it touches on parsing ID3 metadata and culminates with a sound visualization exercise, drawing a sound’s waveform during live playback. Chapter 12 wraps up Part IV by demonstrating how to play video both with and without components, as well as how to subtitle your videos for accessibility. Part V: Input/Output Part V focuses on loading assets into Flash and sending data out to a server or another client. Chapter 13 covers loading SWF files, images, text, URL variables, and binary data, as well as communicating between loader and loadee SWFs. Chapter 14 covers XML and the new standard for working with XML that makes the task as easy as working with other ActionScript objects, methods, and properties. Part VI: 3D (Download) A special bonus chapter, available for download from the companion web- site, takes a short look at the 3D capabilities built-in to ActionScript 3.0. Download from Wow! eBook <www.wowebook.com> Preface xv What’s Not As mentioned previously, this book focuses on ActionScript 3.0 (which applies to most segments of the Flash platform), but is presented within a Flash Professional context. As such, it does not include coverage of Flex, AIR, Flash Media Server, or other evolving Flash platform technologies. As a basic text, this book has understandable constraints that limit the extent of coverage we can offer. Browsing through the Table of Contents will tell you what we include and, in some cases, the depth in which we’ll cover the material. While it does include coverage of object-oriented programming techniques, for example, it does not address this material in great depth. (For more information about this point, please see the previous section, “How This Book Is Organized.”) When you want to continue your OOP studies, we recommend Object-Oriented ActionScript 3.0 by Peter Elst, Todd Yard, and Sas Jacobs (Friends of Ed). We didn’t intend this text to be a reference book, but rather a learning tool. If you’re looking for a comprehensive reference volume, we recommend Essential ActionScript 3.0 by Colin Moock (O’Reilly). Our book may serve as a useful companion to this title, particularly if you are not an advanced user, but it’s not a substitute. Companion Website All the exercises included in this book are available for download from the book’s companion website, http://www.LearningActionScript3.com. Supplemental materials are also available, including additional exercises, self quizzes, extended examples, ongoing learning suggestions, a list of additional resources, reader comments, errata, and more. The source file archives for each chapter are available from the Downloads page, and you can sort posts by category or use the search feature to find posts by name. Both authors can be reached directly through this website. Typographical 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 modi- fiers (such as Alt and Command). Italic Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, and directories. Download from Wow! eBook <www.wowebook.com> Preface xvi Constant width Indicates ActionScript code, text output from executing scripts, XML tags, HTML tags, and the contents of files. Constant width bold Shows commands or other text that should be typed literally. Constant width italic Shows text that should be replaced with user-supplied values. Using Code Examples This book 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 por- tion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distribut- ing 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, copyright holder, and ISBN. For example: Learning ActionScript 3.0, Second Edition, by Rich Shupe with Zevan Rosser (O’Reilly). Copyright 2011 Rich Shupe and Zevan Rosser, 978-1-449-39017-4. 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. We’d Like To Hear From You 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) We have 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/9781449396558 N OT E A note gives additional information, such as resources or a more detailed explanation. WA R N I NG This box indicates a warning or caution. Download from Wow! eBook <www.wowebook.com> Preface xvii 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 website at: http://www.oreilly.com Acknowledgments We would like to give thanks to our talented O’Reilly team: Linda Laflamme, Ron Bilodeau, Nellie McKesson, Rachel Monaghan, Teresa Elsey, Nancy Kotary, Mary Treseler, Betsy Waliszewski, Anselm Bradford, Chrissy Rey- Drapeau, Bentely Wolfe, Tim Goss, Robyn Thomas, Steve Weiss, Michele Filshie, Matthew Roberts, Jill Steinberg, Joy Dean Lee, Phil Dangler, Linda Seifert, Mark Paglietti, Karen Montgomery, and Laurie Petrycki. Extra thanks to Linda, Ron, and Rachel for their endless patience and support. Zevan would like to thank: Rich Shupe, The School of Visual Arts, Jesse Reznick and the creative team at SOM, Ann Oren, all of his students, and his family. Rich would like to thank: Zevan Rosser, Jodi Rotondo, Sally Shupe, Claire Shupe, Mike Wills, Steven Mattson Hayhurst, Thomas Yeh, Anita Ramroop, and his family. Rich would also like to show his appreciation for: • Bruce Wands, Joe Dellinger, Russet Lederman, Mike Barron, Jaryd Lowder, Diane Field, Jenny Lin, Annie Wang, all at The School of Visual Arts, and all my students. • Mark Anders, Paul Burnett, Mike Chambers, Mike Downey, Richard Galvan, Mally Gardiner, Stefan Gruenwedel, Jeff Kamerer, John Nack, Michael Ninness, Pete Falco, Nivesh Rajbhandari, and all at Adobe. • John, Jo, and Amy Davey, Joe Franklin, Hippy Wright, and everyone at Flash on the Beach and Geeky By Nature; Dave Schroeder and everyone at Flashbelt; Susan Horowitz, William Morrison, and the University of Hawaii’s Outreach program; Kelly Sanders, Tomo Kuriyama, and Julie Loo of Sheraton Hotels. • Alex Taylor (Eltima); Gaby Ciordas, Alin Dogar, Raul Popa (Jumpeye Components); John Pattenden (Screentime Media); Coby Rich (Sorenson Media); Jerry Chabolla, Richard Blakely, and Grant Garrett at Influxis (the only streaming media host you’ll ever need). • Lynda Weinman, Bruce Heavin, and everyone at Lynda.com; everyone at Flashcoders NYC. Download from Wow! eBook <www.wowebook.com> Preface xviii • Aral Balkan, Pete Barr-Watson, Rob Bateman, Brendan Dawes, Julian Dolce, Stephen (Tink) Downs, Joa Ebert, Hugh Elliot, Peter Elst, Hardy Fox, Homer Flynn, Jared Ficklin, Jesse Freeman, Chris Georgenes, Hoss Gifford, Bruce Gilbert, Brandon Hall, Ralph Hauwert, Robert Hodgin, Thibault Imbert, Scott Janousek, Penn Jillette, Mike Jones, Lisa Larson- Kelley, Philip Kerman, Mario Klingemann, Seb Lee-Delisle, Graham Lewis, Richard Lord, Jobe Makar, Niqui Merret, André Michelle, Stacey Mulcahey, Erik Natzke, Colin Newman, James Paterson, Chris Pelsor, Keith Peters, Robert Reinhart, Lou Reed, Tim Saguinsin, Grant Skinner, David Stiller, Craig Swann, Jared Tarbell, Teller, Jer Thorpe, Carlos Ulloa, (and no doubt others that I’m forgetting) for support and/or inspiration. • (Extra special thanks to) Hudson Ansley, Tim Beynart, Anselm Bradford, Lee Brimelow, Veronique Brossier, Thaylin Burns, Xingyi Guo, Colin Holgate, Tyler Larson, Chris Niemiec, Sonia Garbès Putzel, Kevin Suttle, and Josh Tynjala. • (Supreme nod to) Scotty and Kat Meltzer, Steve and Cindy Shupe, Dennis and Elaine Rotondo, Mari Howard, and Brian and Abigail Shupe. You know why. Welcome Lucas Robert Bilodeau! Best wishes to Tom Kelley. I wish I could say this book is for whomever Kyle Baker is going out with now, but that was done long ago. This book is for Sally and Claire. About the Authors Rich Shupe is the founder and president of FMA—a full-service multimedia development company and training facility in New York City. Rich teaches a variety of digital technologies in academic and commercial environments, and has frequently lectured on these topics at conferences all over the world. He is currently on the faculty of New York’s School of Visual Arts in the MFA Computer Art department. Rich has written or co-written multiple books, including Learning Flash CS4 Professional, The ActionScript Quick Reference Guide, and Flash 8: Projects for Learning Animation and Interactivity (all O’Reilly), Flash CS3 Professional Video Training Book (Lynda.com/Peachpit), and the CS3 Web and Design Workflow Guides (Adobe). He also presents video training for Lynda.com. Visit Rich’s website at http://www.fmaonline.com. Zevan Rosser is a freelance designer/programmer/consultant and computer artist. He teaches ActionScript and Flash animation at New York’s School of Visual Arts in the Undergraduate and Continuing Education programs, and has acted as thesis advisor for a handful of Masters students. He also teaches ActionScript and Flash at FMA in New York. When he’s not working on commercial projects, he works on his personal site, http//www.shapevent.com. Download from Wow! eBook <www.wowebook.com> . publisher: O’Reilly Media, Inc. 100 5 Gravenstein Highway North Sebastopol, CA 954 72 ( 800 ) 99 8-9 938 (in the United States or Canada) ( 707 ) 829 -0 5 15 (international or local) ( 707 ) 829 -0 1 04. for you. Migration from ActionScript 2 . 0- to -ActionScript 3. 0 is not our primary focus, so we want you to be happy with the tone and straightforward approach we’ve adopted before you decide to. experienced—embrace the power and perfor- mance of ActionScript 3. 0. We hope these pages will ease the transition from whatever prior version of ActionScript you may have used (if any) to 3. 0 the