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
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
[email protected],
or v
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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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 .
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
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
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
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.
.
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
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 .
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 .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
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
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
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: