Tải bản đầy đủ (.pdf) (640 trang)

Tài liệu FriendsofED Object Oriented ActionScript 3.0 Jul 2007 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.11 MB, 640 trang )

this print for reference only—size & color not accurate
spine = 1.206" 640 page count
PETER ELST SAS JACOBS TODD YARD
CYAN YELLOW
MAGENTA BLACK
US $49.99
Mac/PC compatible
www.friendsofed.com
ISBN-13: 978-1-59059-845-0
ISBN-10: 1-59059-845-8
9 781590 598450
5 4 9 9 9
SHELV IN G CAT EG OR Y
1. FLASH
Inside you’ll learn
»
The essential principles of object-oriented programming, including inheritance,
encapsulation, polymorphism, and more
»
Valuable lessons on ActionScript 3.0 project planning and programming, including design
patterns and source control
»
How to create your own extensible, reusable application framework using OOP
best practices
»
Advanced data integration techniques such as Web Services and communication between
Flash and the browser
Object-Oriented ActionScript 3.0
O
bject-oriented programming (OOP) is something that is usually considered
a black art for hardcore programmers, not a topic of conversation for Flash


developers. However, when Adobe introduced ActionScript 3.0 to the mix, it
changed everything. ActionScript 3.0 is much more powerful than previous versions,
allowing Flash developers to produce robust object-oriented applications. But
with that power comes great responsibility—OOP is now a requirement, rather
than optional, and there are new things to learn.
But never fear—this book, based on the ever-popular Object-Oriented ActionScript for
Flash 8, provides you all you need to delve into the world of OOP with confidence,
whether you are using the Flash IDE, Flex Builder, or even command-line tools for
your development work.
First, you are taken gently through all the principles of OOP that you need to know,
and then given a guide to designing and implementing applications in ActionScript
3.0. Next, we step up a gear, showing you the Flex Builder development environment
and teaching about creating reusable, extensible component frameworks—manager
classes, animation and effects classes, UI widgets, and more. Lastly, we look at some
more advanced topics such as communication between Flash and the browser and
Web Services. Case studies are included that apply the knowledge presented, giving
you real-world projects to learn from and adapt for use in your own work.
Mastering object-oriented programming is essential for modern Flash development,
and Object-Oriented ActionScript 3.0 is the only guide you’ll need.
» Learn object-oriented programming in ActionScript 3.0
» Covers both the Flash and Flex environments
» Includes design patterns, custom frameworks,
data binding, and other crucial techniques
Elst
Jacobs
Yard
ALSO AVAILABLE
friends of ED ADOBE
®
LEARNING LIBRARY

OBJECT-ORIENTED
ACTIONSCRIPT 3.0
Object-Oriented
ActionScript 3.0
Peter Elst
Sas Jacobs
Todd Yard
8458FM.qxd 6/15/07 11:41 AM Page i
Object-Oriented ActionScript 3.0
C
opyright © 2007 by Peter Elst, Sas Jacobs, and Todd Yard
A
ll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
e
lectronic or mechanical, including photocopying, recording, or by any information storage or retrieval
s
ystem, without the prior written permission of the copyright owner and the publisher.
I
SBN-13 (pbk): 978-1-59059-845-0
ISBN-10 (pbk): 1-59059-845-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
T
rademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the
trademark owner, with no intention of infringement of the trademark.
Distributed to the book trade worldwide by Springer-
Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail
,
or v

isit
www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail
,
or visit
www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or
indirectly by the information contained in this work.
The source code for this book is freely available to readers at www.friendsofed.com in the
Downloads section.
Cover images courtesy of NASA.
Credits
Lead Editor
Chris Mills
Technical Reviewer
Crystal West
Editorial Board
Steve Anglin, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jason Gilmore, Jonathan Hassell,
Chris Mills, Matthew Moodie, Jeffrey P
epper,
Ben Renow-Clarke, Dominic Shakeshaft,
Matt W
ade, Tom Welsh
Project Manager
Denise Santoro Lincoln
Copy Edit Manager

Nicole Flores
Copy Editor
Ami Knox
Assistant Production Director
Kari Brooks-Copony
Production Editor
Laura Esterman
Compositor
Dina Quan
Proofreader
Linda Seifert
Indexer
Broccoli Information Management
Interior and Cover Designer
Kurt Krames
Manufacturing Director
T
om Debolski
8458FM.qxd 6/15/07 11:41 AM Page ii
8458FM.qxd 6/15/07 11:41 AM Page iii
CONTENTS AT A GLANCE
Foreword
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xvi
About the Authors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xviii
About the Technical Reviewer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xix
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xx
Introduction
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxi
PART ONE: OOP AND ACTIONSCRIPT
Chapter 1: Introduction to OOP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
Chapter 2: Programming Concepts
.
. . . . . . . . . . . . . . . . . . . . . . . . . .
13
Chapter 3: ActionScript 3.0 Programming
.
.
.
.
. . . . . . . . . . . . . . . . . .
21
PART TWO: FLASH OOP GUIDELINES
Chapter 4: Planning
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
Chapter 5: Project Workflow
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53
Chapter 6: Best Practices
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
Chapter 7: Working with Flex 2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
iv
8458FM.qxd 6/15/07 11:41 AM Page iv
PART THREE: CORE OOP CONCEPTS
Chapter 8: Encapsulation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
125
Chapter
9: Classes
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
147
Chapter 10: Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
161
Chapter 11: Polymorphism
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
177
Chapter 12: Interfaces
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
187
Chapter 13: Design Patterns
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

201
Chapter 14: Case Study: An OOP Media Player
. . . . . . . . . . . . . . . . .
245
PART FOUR: BUILDING AND EXTENDING DYNAMIC FRAMEWORKS
Chapter 15: Manager Classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
289
Chapter 16: UI Widgets
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
327
Chapter 17: OOP Animation and Effects
.
. . . . . . . . . . . . . . . . . . . . .
381
PART FIVE: DATA INTEGRATION
Chapter 18: Exchanging Data Between Components
.
.
.
. . . . . . . . . .
437
Chapter 19: Communication Between Flash and the Browser
. . . . . .
477
Chapter 20: Server Communication (XML and Web Services)
. . . . . .
505
Chapter 21: Case Study: Slideshow Engine
. . . . . . . . . . . . . . . . . . . .

549
Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
599
v
8458FM.qxd 6/15/07 11:41 AM Page v
CONTENTS
Foreword
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xvi
About the Authors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xviii
About the Technical Reviewer
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xix
Acknowledgments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xx
Introduction
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xxi
PART ONE: OOP AND ACTIONSCRIPT
Chapter 1: Introduction to OOP
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
The scoop with OOP .

.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Understanding the object-oriented approach . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Classes and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Encapsulation: Hiding the details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Polymorphism: Exhibiting similar features . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Inheritance: Avoid rebuilding the wheel . . . . . . . . . . . . . . . . . . . . . . . . . . 10
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Chapter 2: Programming Concepts
. . . . . . . . . . . . . . . . . . . . . . . . . . .
13
About programming slang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Building blocks of programming
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
About variable data
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
vi
8458FM.qxd 6/15/07 11:41 AM Page vi
Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
About calling functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
About function parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
OOP concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 3: ActionScript 3.0 Programming
. . . . . . . . . . . . . . . . . . . . . .
21
ActionScript 2.0 vs. ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Declaring variables .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Classes vs. prototypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Public and private scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Strong typing and code hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
ActionScript gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Case sensitivity .
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Use of the this keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
PART TWO: FLASH OOP GUIDELINES
Chapter 4: Planning
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
The importance of planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Initial phase: Planning reusability! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Planning encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Planning inheritance
.
.
.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Analyzing a Flash ActionScript project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Flash files run on the client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Securing data sent to the server
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Parsing data in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
vii
8458FM.qxd 6/15/07 11:41 AM Page vii
Introduction to UML modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
W
hy use UML?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
U
ML offers standardized notation and has a language-neutral syntax . . . . . . . 46
UML can be used to model anything . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Class diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Association and generalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Aggregation and composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Chapter 5: Project Workflow
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
Introducing version control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
About Concurrent Versions System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using TortoiseCVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Approaches to programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Rapid Application Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Extreme Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Usability testing .
.
.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Chapter 6: Best Practices
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
External ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
About commenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Naming conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Functions.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Programming styles .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Alternative programming styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Coding practices: Peter Elst. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Coding practices: Todd Yard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Coding practices: Sas Jacobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Commenting code .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Chapter 7: Working with Flex 2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
W
orking with Flex Builder 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Downloading Flex Builder 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Installing Flex Builder 2 Windows version. . . . . . . . . . . . . . . . . . . . . . . . . . 92
Starting Flex Builder 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Understanding the Flex Builder 2 interface
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . 98
CONTENTS
viii
8458FM.qxd 6/15/07 11:41 AM Page viii
Getting started with Flex Builder 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
W
orking with controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
B
uilding a Flex application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Creating a Flex project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Understanding MXML files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Creating the interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Adding controls to the application . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Adding ID attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Populating the ComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Creating a new application state. . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Performing the calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Running your application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Debugging applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Tips for working with Flex Builder 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Creating folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Exporting projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Importing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Collapsing code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Viewing a quick outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Displaying line numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Adding tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Listing useful shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Compiling SWF files from the command line . . . . . . . . . . . . . . . . . . . . . . . . . 120
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
PART THREE: CORE OOP CONCEPTS
Chapter 8: Encapsulation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
125
Setting up encapsulation .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Creating new layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Drawing a background .
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Aligning and locking the background . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Drawing a ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Converting the ball into a Library symbol . . . . . . . . . . . . . . . . . . . . . . . . . 132
Content summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

Writing the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Creating an event handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
What about encapsulation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
T
esting the event handler
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Updating the ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Improving the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Enhancing behavior with properties
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Narrowing the focus with functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Encapsulation summary
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
CONTENTS
ix
8458FM.qxd 6/15/07 11:41 AM Page ix
Chapter 9: Classes
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
147
Classes vs. prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
C
onstructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Implementing a class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

The Mover class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Chapter 10: Inheritance
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
161
About class hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
A quick inheritance test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
About inheritance syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
The Bouncer class .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The Gravity class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Inheritance summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Chapter 11: Polymorphism
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
177
Building a polymorphism example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Implementing polymorphism for application reuse. . . . . . . . . . . . . . . . . . . . . . 180
Basic concept of polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Functional polymorphism at work .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Chapter 12: Interfaces
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
187
Interfaces overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Interface use cases.
.

.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
What an interface looks like . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Implementing an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Chapter 13: Design Patterns
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
201
Understanding design patterns .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Implementing design patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Observer pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Basic implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Practical implementation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Extending the practical implementation . . . . . . . . . . . . . . . . . . . . . . . 212
Singleton pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Basic implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Practical implementation
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Building an interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
CONTENTS
x
8458FM.qxd 6/15/07 11:41 AM Page x
Decorator pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
B
asic implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
P

ractical implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Model-View-Controller pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Basic implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Practical implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Bringing together the Model, View, and Controller . . . . . . . . . . . . . . . . . 240
Design patterns summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Chapter 14: Case Study: An OOP Media Player
. . . . . . . . . . . . . . . . .
245
Planning the player. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Picking a pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Guaranteeing methods and datatypes with an interface . . . . . . . . . . . . . . . . . 246
Examining class structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Building the media player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Defining the interfaces .
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Defining events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Controlling media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Defining properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Protected methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Public methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Controlling FLVs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Public methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Private and protected methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Controlling sound .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Building a video view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Testing in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Testing in Flex Builder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Improving the display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Setting up MediaDisplay .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Providing for scale modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Adding the accessors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Displaying FLVs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Controlling SWFs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Building a SWF view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Controlling MP3s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
PART FOUR: BUILDING AND EXTENDING DYNAMIC FRAMEWORKS
Chapter 15: Manager Classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
289
Planning the framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
What to manage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Diagramming the classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
CONTENTS
xi
8458FM.qxd 6/15/07 11:41 AM Page xi
Building managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
I

Describable and IStyleable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
S
tyleFormat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
StyleManager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Coding with style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
SoundManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Sounding off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
LabelManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
LabelEllipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Slap on a label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Chapter 16: UI Widgets
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
327
Diagramming the classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
UIObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
SimpleButton.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Making the foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Managing skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Basic building block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
A simple skin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Block party . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Adding interactivity with SimpleButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
From block to button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Mapping out some new classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Label .

.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
LabelButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
So much more than a text field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Slapping on a label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Putting it all together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
LabelButton .
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Not-so-SimpleRect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Final test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Chapter 17: OOP Animation and Effects
. . . . . . . . . . . . . . . . . . . . . .
381
Preparing for animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Animator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
T
weening properties and values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Tweener. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Easing values
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Adding events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Grouping animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
TweenComposite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
T

weenSequence
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
AnimationHold
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Testing animation sequences and composites . . . . . . . . . . . . . . . . . . . . 400
CONTENTS
xii
8458FM.qxd 6/15/07 11:41 AM Page xii
9cc2cbcaa5806aff5cef01dba1f8d7f9
Introducing TweenRunner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
M
over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
M
otion blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Transitioning views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
FadeTransition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Testing transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
ColorTransition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
BlurTransition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
NoiseTransition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
DissolveTransition and WaterTransition . . . . . . . . . . . . . . . . . . . . . . . . . . 432
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
PART FIVE: DATA INTEGRATION
Chapter 18: Exchanging Data Between Components
. . . . . . . . . . . . .
437
Data binding .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Using curly braces syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Using direct binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Using ActionScript expressions .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Using an E4X expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Binding with an ActionScript function . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Using the <mx:Binding> tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Using direct binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Creating multiple bindings with the same controls. . . . . . . . . . . . . . . . . . . . 446
Using ActionScript expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Using an E4X expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Binding with an ActionScript function .
. . . . . . . . . . . . . . . . . . . . . . . . . . 450
Working through a binding example .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Using the BindingUtils class in ActionScript . . . . . . . . . . . . . . . . . . . . . . . . 457
Binding to a property .
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Binding to a method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Working through a scripted binding example. . . . . . . . . . . . . . . . . . . . . . . 460
Adding a formatter to a binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Using built-in formatters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Using the CurrencyFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Using the DateFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Using the NumberFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Using the PhoneFormatter .

.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Using the ZipCodeFormatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Detecting formatter errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
W
orking with custom formatters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
What’s next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
CONTENTS
xiii
8458FM.qxd 6/15/07 11:41 AM Page xiii
Chapter 19: Communication Between Flash and the Browser
.
. . . . .
477
Communicating using fscommand() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
U
nderstanding Flash Player Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Using the fscommand() function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Understanding the ExternalInterface class . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Using the call() method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Using the addCallback() method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Understanding the limitations of the ExternalInterface class . . . . . . . . . . . . 494
Understanding the Flex-Ajax Bridge . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Getting started with the Flex-Ajax Bridge . . . . . . . . . . . . . . . . . . . . . . . 495
Writing FABridge JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Working through a simple example . . . . . . . . . . . . . . . . . . . . . . . . . . 496
ActionScript communication with other languages. . . . . . . . . . . . . . . . . . . . 500
Making function calls .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

Chapter 20: Server Communication (XML and Web Services)
.
.
. . . .
505
Understanding XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Understanding well-formed documents . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Writing comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Adding special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
XML declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Using ActionScript 3.0 with XML .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Understanding E4X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Navigating XML documents with E4X . . . . . . . . . . . . . . . . . . . . . . . . . 512
E4X with ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Understanding the XML class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Properties of the XML class .
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . 514
Methods of the XML class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Understanding the XMLList class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Creating XML content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Assigning XML content to an XML object . . . . . . . . . . . . . . . . . . . . . . . 517
Passing string content to the XML constructor . . . . . . . . . . . . . . . . . . . . 517
Loading XML content from an external source.
. . . . . . . . . . . . . . . . . . . 517
Understanding the URLLoader class . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Understanding the HTTPService class . . . . . . . . . . . . . . . . . . . . . . . . . 518

Working through an example with the URLLoader class . . . . . . . . . . . . . . . . . 519
W
orking through an example with the <mx:HTTPService> tag . . . . . . . . . . . . . 525
Understanding Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Understanding SOAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Consuming Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Understanding the W
ebService class.
. . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Working through a scripted example . . . . . . . . . . . . . . . . . . . . . . . . . 530
Properties of the WebService class . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Methods of the WebService class . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Events dispatched by the WebService class . . . . . . . . . . . . . . . . . . . . . . 538
Using the <mx:WebService> element . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
CONTENTS
xiv
8458FM.qxd 6/15/07 11:41 AM Page xiv
Working with Flash Player security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
S
ecurity.allowDomain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
C
ross-domain policy files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Using a server-side proxy script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Chapter 21: Case Study: Slideshow Engine
. . . . . . . . . . . . . . . . . . . .
549
Understanding the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Brainstorming designs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Diagramming the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

Preparing the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
Defining the XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
Building the base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
SlideShowEvent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
SlideProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
SlideShowModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
SlideShow.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Slide. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Plugging in slides. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
SlideFactory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Adding slides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
BlankSlide. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
TitleSlide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
ImageSlide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
SlideCollection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Revisiting the factory .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Setting slide properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Timing slides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Collecting slides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Completing the slides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
TitleSlide .
.
.
.
.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
ImageSlide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Testing the show . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Enhancing the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Loading dynamic slides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Updating the engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Compiling the slides. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Transitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Summary
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
599
CONTENTS
xv
8458FM.qxd 6/15/07 11:41 AM Page xv
FOREWORD
If there’s one thing I’ve learned as a developer, it’s this: complexity happens; simplicity, you
have to strive for. Nowhere is this truer than in education. Our role as teachers, by definition,
is to simplify subjects so that they can be easily understood. A good teacher dispels trepida-
tion with anecdote, abstraction with analogy, superstition and magic with knowledge.
Simplicity, however, is not easily attained. In order to simplify, you must first gain an encom-
passing understanding of the complex. It is a rare person who can simultaneously exist in
both the simple and complex plains of a problem domain and communicate effectively at
both levels. It is, however, these same people who make the best teachers.
Object-oriented programming (OOP) is a subject that many Flash developers do not approach
due to a widespread erroneous perception of its enormous scope and complexity. Nothing
could be further from the truth. The core concepts behind OOP are simple enough for a pri-
mary school student with a particularly nasty case of Hynerian flu to understand in a single

sitting.
It must be because OOP is essentially such a simple concept that we sometimes feel the need
to protect ourselves with important-sounding words the length of major rivers in order to
explain it. Because, hey, if we said that OOP involves the interaction of objects, each of which
is an instance of a certain blueprint and has certain traits and behaviors—well, that would
just be too simple. Who’d respect our geeky prowess then? Instead, we lock ourselves in
ivory towers guarded by the frightening monsters we call Inheritance, Composition,
Polymorphism, and Encapsulation, and hope that the FlashKit masses will tend to their
tweens and leave us to meditate quietly on the path to programming nirvana.
But object-oriented programming is easy. All right, it’s easy just like chess is easy. You can
pick up the basics in an hour and start playing the game but it can take years to fully master
.
This book teaches you the basics to get you started on the right foot.
What you may not know is that if you have done any Flash development at all, you have
already used at least some object
-based programming. Each time you set the x and y coordi-
nates of a movie clip or you tell it to
gotoAndPlay(), you are interacting with the properties
and methods of an object (a movie clip, just like everything else in Flash, is an object).
However, your applications do not magically become object oriented just because you use
xvi
8458FM.qxd 6/15/07 11:41 AM Page xvi
objects. You can very easily use objects in a procedural manner without practicing object-
oriented programming. What this book teaches you is to go beyond a mixture of procedural
and object-based programming to understand how to structure your Flash applications in an
object-oriented manner. It goes beyond the mere use of existing objects to teach you how to
model your own objects and structure the communication between objects using good
practices.
OOP is so often presented in such pretentious prose so as to be illegible to all but a handful
of PhDs. If grandiose, self-important passages of academic rambling are what you’re after,

you should put this book down and walk away now. I’m sure you’ll find an 800-page hard-
back elsewhere to satisfy your thirst for confusion. If, however, you are looking for a prag-
matic guide to OOP and ActionScript 3.0 (AS3) that is simply written and easy to understand,
you could do far worse than to look through these pages more closely.
Aral Balkan
June 4, 2007
Brighton, United Kingdom
xvii
8458FM.qxd 6/15/07 11:41 AM Page xvii
ABOUT THE AUTHORS
Peter Elst is a certified Flash instructor, an Adobe Community Expert,
and an active member of the Belgian Adobe user group. As the
managing director of MindStudio, a freelance Flash platform con-
sultant, and a respected member of the online community, Peter has
spoken at various international industry events and published his
work in leading journals. He regularly posts his views on emerging
trends in Rich Internet Application development on his blog:
www.peterelst.com.
Sas Jacobs is a web developer who loves working with Flash and Flex
applications. She set up her business, Anything Is Possible, in 1994,
working in the areas of web development, IT training, and technical
writing. The business works with large and small clients building web
applications with Flash, Flex, ASP.NET, XML, and databases. Sas has
spoken at many international conferences on topics relating to XML
and dynamic content in Flash. In her spare time, she is passionate
about traveling, photography, running, and enjoying life. You can
find out more about her at
www.sasjacobs.com.
Todd Yard is a senior software engineer at Brightcove in Cambridge,
Massachusetts, leading the development of their Flash video players

and templating system. He has contributed as an author to over ten
friends of ED books and as a technical reviewer for several others.
His personal site,
www.27Bobs.com, really needs updating, but he
’s a
busy guy.
xviii
8458FM.qxd 6/15/07 11:41 AM Page xviii
ABOUT THE TECHNICAL REVIEWER
Crystal West has been using Flash and ActionScript to build Rich
Internet Applications since 2002. She is currently working as a soft-
ware engineer at Brightcove (
www.brightcove.com) in Cambridge,
Massachusetts, helping to build the future of Internet TV. When she
is not coding or problem solving, you’ll most likely find her in dance
class, hanging out with her husband, Jeffrey, and their dog, Gus, or
pining for the next
LOST episode. She and her husband currently
reside in Boston, Massachusetts.
xix
8458FM.qxd 6/15/07 11:41 AM Page xix
ACKNOWLEDGMENTS
Special thanks to friends and family for their support; Tink and Crystal for their help and
technical expertise; Serge, Koen, and the local Adobe community for the inspiration; and,
last but not least, coauthors Sas and Todd, and the friends of ED team for making this book
a reality.
Peter Elst
xx
8458FM.qxd 6/15/07 11:41 AM Page xx
INTRODUCTION

Hello and welcome to Object-Oriented ActionScript 3.0. We’ve written this book to guide
you through the world of object-oriented programming in ActionScript 3.0.
As you’ll no doubt know, Adobe revolutionized ActionScript when it introduced ActionScript 3.0.
Far from being a minor overhaul, it’s a complete update—we think it’s fair to say that the
language has been reinvented as a full-scale proper programming language, with many
things vastly improved, such as the way it handles events and XML, and some very cool addi-
tions, such as regular expressions, a standardized event model, and a new Display List API for
working with visual objects much more effectively. But one of the biggest overriding things
to note involves object orientation: whereas with ActionScript 2.0, it was possible, and cer-
tainly beneficial, to code your applications the OO way, with ActionScript 3.0 it is completely
mandatory.
But there’s no need to go running for cover—object-oriented programming is not something
to be scared about. What it really comes down to is best practices for structuring your code
and finding techniques dealing with common problems that arise when building applications.
And this book takes you through those best practices and techniques from the ground up.
After a quick introduction to the area, and a run-through of essential ActionScript 3.0 syntax,
we jump into OOP best practices (including workflow and planning) and cover OOP principles
in detail—inheritance, classes, encapsulation, interfaces, design patterns, and polymor-
phism—showing how to apply these to ActionScript 3.0 most effectively. In this edition of
our book, examples are provided in both the Flash IDE and Flex Builder. Flex Builder has an
entire chapter devoted to it to show you how best to code OO applications with it.
The first three parts of the book cover the topics just described. This should get you
comfortable enough to start mastering OOP ActionScript 3.0 applications, no matter what
development environment you choose to use to work with them.
The last two parts of the book (which cover dynamic frameworks and data integration) take
all the concepts covered in the first three parts and apply them to building lots of real-word
examples that solve different problems you might encounter in your professional work. We
recommend you follow through all the examples attentively
, as there is a lot of learning and
xxi

8458FM.qxd 6/15/07 11:41 AM Page xxi
inspiration to be taken from them. Specific cases range from manager classes and OOP
a
nimation and effects frameworks, to communication between Flash and the browser and
X
ML and Web Services.
All too often, object-oriented programming and design patterns are presented as some
sort of religious edict; the aim of this book is to present these topics to you in a way that
is pragmatic, practical, and down to earth, giving you the inspiration to start developing
well-maintained, reusable, and well-structured code.
Onward!
Mac or PC?
This book supports both PC and Mac platforms. During the writing of this book, both
operating systems were used, and differences in how the platforms support certain
features were taken into account. These are highlighted throughout the chapters in rele-
vant places.
Development environment?
As alluded to earlier, there are numerous ways to author ActionScript 3.0 content. The two
most common ways are through Flash CS3 and Flex Builder, but you could also use the
free Flex SDK, available at
www.adobe.com/products/flex/sdk/, or even open source
tools that support development of ActionScript 3.0. One specifically worth mentioning is
FlashDevelop (PC only), available at
www.flashdevelop.org. There is also a large, vibrant
community of open source Flash developers who have created a wealth of open source
tools—see
for more.
Intended audience
This book is aimed at readers who have some previous experience developing in
ActionScript and are looking to broaden their knowledge on the latest syntax introduced

in ActionScript 3.0 and start writing object-oriented code. Some familiarity with the Flash
or Flex authoring environments is recommended to be able to easily follow along with the
example code.
Layout conventions
To keep this book as clear and easy to follow as possible, the following text conventions
are used throughout:
INTRODUCTION
xxii
8458FM.qxd 6/15/07 11:41 AM Page xxii
Important words or concepts are normally highlighted on the first appearance in italic
t
ype
.
C
ode is presented in
f
ixed-width font
.
New or changed code is normally presented in
bold fixed-width font.
Menu commands are written in the form
Menu

Submenu

Submenu
.
Where we want to draw your attention to something, we’ve highlighted it like this:
Sometimes code won’t fit on a single line in a book. Where this happens, we use an arrow
like this:

➥.
This is a very, very long section of code that should be written all ➥
on the same line without a break.
Ahem, don’t say I didn’t warn you.
INTRODUCTION
xxiii
8458FM.qxd 6/15/07 11:41 AM Page xxiii
8458FM.qxd 6/15/07 11:41 AM Page xxiv

×