Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Actionscript 3.0 bible 2nd edition docx
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
www.it-ebooks.info
ActionScript®
3.0 Bible
www.it-ebooks.info
www.it-ebooks.info
ActionScript®
3.0 Bible
Second Edition
Roger Braunstein
Wiley Publishing, Inc.
www.it-ebooks.info
ActionScript® 3.0 Bible
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-52523-4
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher,
or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should
be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)
748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties
with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties,
including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended
by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation.
This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other
professional services. If professional assistance is required, the services of a competent professional person should be
sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not
mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have
changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2009943640
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or
its affiliates, in the United States and other countries, and may not be used without written permission. ActionScript
is a registered trademark of Adobe Systems Incorporated. All other trademarks are the property of their respective
owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
www.it-ebooks.info
About the Author
Roger Braunstein is a multidisciplinary programmer and the Chief Roger Officer of partlyhuman inc.,
an independent development and design shop in Brooklyn, NY. Roger is an author of the first edition
of ActionScript 3.0 Bible, and a short e-book, Introduction to Flex 2. Roger builds games, mobile apps,
interactive art, tools, web apps, and websites using any technology he can wrap his head around. In
just the past two years he’s built stuff for LEGO, MoMA, Tim Burton, Antony and the Johnsons, AOL,
HBO, Puma, General Mills, Sport Chalet, Pepsi, Coke, OMGPOP, the French Culinary Institute, and
more. Additionally, he keeps busy with a series of independent projects. When not in front of a computer, Roger enjoys normal human activities such as biking, cooking, reading, traveling, taking photos,
roller-skating, and dancing to music made on GameBoys. He is perpetually too preoccupied to put
anything interesting on his site http://partlyhuman.com/, but you can use it to get in touch
with him.
www.it-ebooks.info
www.it-ebooks.info
Credits
Acquisitions Editor
Scott Meyers
Project Editor
Brian MacDonald
Technical Editor
Caleb Johnston
Production Editor
Daniel Scribner
Copy Editor
Karen Gill
Editorial Director
Robyn B. Siesky
Editorial Manager
Mary Beth Wakefield
Marketing Manager
David Mayhew
Production Manager
Tim Tate
Vice President and Executive
Group Publisher
Richard Swadley
Vice President and Executive
Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Nancy Bell
Indexer
Robert Swanson
Cover Image
Joyce Haughey
Cover Designer
Michael E. Trent
www.it-ebooks.info
www.it-ebooks.info
This book, like all gargantuan projects, was a team effort. Its pages were touched by many
hands, at Wiley and elsewhere. Losing even a single pair of these hands would mean you
wouldn’t have this book in yours right now, so I want to thank each and every person on
my team, whether I sent you 20 e-mails a day or never met you.
My most heartfelt thanks go to my project editor Brian MacDonald, and my acquisitions editor
Scott Meyers, who were nothing short of bodacious to work with. When I think of how many times I
pushed for some new, unconventional feature or change to the established Bible series, and then look
at this book and see all of my ideas in print, I know it’s Brian and Scott who have somehow done
their magic, convinced the right people, and made it happen. Thank you.
Perhaps every author forms a good relationship with his project editor just because he’s the person
who’s there from the first day of writing through the last, exhausted round of edits. But I think Brian
MacDonald is exceptional. I’ve never worked with an editor half as good. He had the right solution
for every problem. He was behind me and my ideas through thick and thin. His edits were always
spot-on. He was always available, always funny, and he even got my nerdy jokes. Trust me, if you’re
writing a programming book, bribe whoever you need to hire Brian.
I can’t talk about my team without mentioning the exhaustive copy editing done by Karen Gill. She
kept tabs on all my writing, cut down my useless tiresome redundant logorrhea, and made this book
less of a slog to get through. Trust me, you owe her one. I have the feeling her job is a little thankless,
but I do thank her.
Caleb Johnston, besides being a good friend, was a terrific technical editor. Somehow, with a full
course load for his master’s degree, he managed to find the time to review this entire book by himself. (The previous edition, although smaller, had three technical editors!) More than just scrutinizing
it, he made detailed and thoughtful criticisms, with as much ruthlessness as I demanded of him. He
found some quirky errors, and because he found them, you won’t have to.
Special thanks go out to my friend Corey Lucier at Adobe, who was kind enough to answer a few
questions about the internal workings of Flash Player or forward them on to Flash Player engineers.
Their additions helped me be even more accurate in this book, and Corey’s help was invaluable.
Special thanks also go to Whitney Gardner, who made two sublime illustrations for this book when I
was at my wit’s end.
Thanks to the U.S. economy for making my day job so unprofitable it made sense to sit in my
room and write for nine months. Thanks to coffee: you complete me. Thanks to my crazy SafeType
keyboard, which saved me from crippling arm pain. Thanks to my friends for, I dunno, whatever,
I like you.
Last but not least, thanks and lotsa lotsa love to my parents.
ix
www.it-ebooks.info
www.it-ebooks.info
Introduction ..............................................................................................................................................xli
Part I: ActionScript 3.0 Language Basics . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: Introducing ActionScript 3.0 ...................................................................................................3
Chapter 2: ActionScript 3.0 Language Basics ..........................................................................................15
Chapter 3: Functions and Methods .........................................................................................................39
Chapter 4: Object Oriented Programming ..............................................................................................51
Chapter 5: Validating Your Program .....................................................................................................103
Part II: Core ActionScript 3.0 Data Types . . . . . . . . . . . . . . . . . 111
Chapter 6: Text, Strings, and Characters ..............................................................................................113
Chapter 7: Numbers, Math, and Dates .................................................................................................125
Chapter 8: Arrays ...................................................................................................................................145
Chapter 9: Vectors .................................................................................................................................167
Chapter 10: Objects and Dictionaries ...................................................................................................181
Chapter 11: XML and E4X ....................................................................................................................191
Chapter 12: Regular Expressions ...........................................................................................................225
Chapter 13: Binary Data and ByteArrays ..............................................................................................257
Part III: The Display List . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Chapter 14: Visual Programming with the Display List .......................................................................273
Chapter 15: Working in Three Dimensions ..........................................................................................301
Chapter 16: Working with DisplayObjects in Flash Professional ........................................................321
Chapter 17: Text, Styles, and Fonts ......................................................................................................329
Chapter 18: Advanced Text Layout .......................................................................................................367
Chapter 19: Printing ..............................................................................................................................399
Part IV: Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . 407
Chapter 20: Events and the Event Flow ...............................................................................................409
Chapter 21: Interactivity with the Mouse and Keyboard .....................................................................429
Chapter 22: Timers and Time-Driven Programming ............................................................................461
Chapter 23: Multitouch and Accelerometer Input ................................................................................473
Part V: Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
Chapter 24: Errors and Exceptions .......................................................................................................491
Chapter 25: Using the AVM2 Debugger ...............................................................................................505
Chapter 26: Making Your Application Fault-Tolerant ..........................................................................523
Part VI: External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Chapter 27: Networking Basics and Flash Player Security ...................................................................533
Chapter 28: Communicating with Remote Services .............................................................................561
xi
www.it-ebooks.info
Contents at a Glance
Chapter 29: Storing and Sending Data with SharedObject ..................................................................571
Chapter 30: File Access .........................................................................................................................589
Part VII: Sound and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Chapter 31: Playing and Generating Sound .........................................................................................605
Chapter 32: Playing Video .....................................................................................................................625
Chapter 33: Capturing Sound and Video .............................................................................................643
Part VIII: Graphics Programming and Animation . . . . . . . . . .655
Chapter 34: Geometric and Color Transformations .............................................................................657
Chapter 35: Programming Vector Graphics ..........................................................................................687
Chapter 36: Programming Bitmap Graphics .........................................................................................733
Chapter 37: Applying Filters .................................................................................................................769
Chapter 38: Writing Shaders with Pixel Bender ...................................................................................803
Chapter 39: Scripting Animation ...........................................................................................................835
Chapter 40: Advanced 3D .....................................................................................................................855
Part IX: Flash in Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .877
Chapter 41: Globalization, Accessibility, and Color Correction ..........................................................879
Chapter 42: Deploying Flash on the Web ............................................................................................897
Chapter 43: Interfacing with JavaScript ................................................................................................905
Chapter 44: Local Connections between Flash Applications ...............................................................911
Index .......................................................................................................................................................921
xii
www.it-ebooks.info
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xli
Part I: ActionScript 3.0 Language Basics 1
Chapter 1: Introducing ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . 3
What Is ActionScript 3.0? ...............................................................................................................3
Exploring the Flash Platform ..........................................................................................................4
A Programmer’s Perspective .................................................................................................5
Language .....................................................................................................................5
API .............................................................................................................................6
Libraries ......................................................................................................................7
Compilers, Tools, and IDEs .......................................................................................7
SWFs .........................................................................................................................8
Flex .............................................................................................................................9
In Short .......................................................................................................................9
A User’s Perspective ..............................................................................................................9
Runtimes .....................................................................................................................9
Platforms and Platform Independence ....................................................................10
The Flash Player Zoo ...............................................................................................10
In Short .....................................................................................................................11
From ActionScript 2.0 to ActionScript 3.0 ...................................................................................11
Display List ..........................................................................................................................11
Runtime Errors ....................................................................................................................12
Runtime Data Types ............................................................................................................12
Method Closures .................................................................................................................12
Intrinsic Event Model .........................................................................................................12
Regular Expressions ............................................................................................................12
E4X ......................................................................................................................................12
Summary ........................................................................................................................................13
Chapter 2: ActionScript 3.0 Language Basics . . . . . . . . . . . . . . . . . . . . . 15
The Bare Essentials ........................................................................................................................15
Using Variables ..............................................................................................................................17
Anatomy of a Variable Declaration .....................................................................................17
Constants in This Changing World ...................................................................................18
Taking It Literally ...............................................................................................................18
xiii
www.it-ebooks.info