Essential ActionScript 3.0 I Colin Moock i" O'REILLY

cabbagetreatmentSoftware and s/w Development

Jul 4, 2012 (5 years and 2 months ago)

298 views

Essentia l ActionScrip t 3.0
I
Colin Moock
i" O'REILLY 8
| Beijin g • Cambridg e • Farnha m • Kol n • Pari s • Sebastopo l • Taipe i • Toky o
Tabl e o f Content s
Forewor d
Prefac e ".
xv
xi x
Par t I. ActionScrip t fro m th e Groun d Up
1. Cor e Concept s
Tools for Writing ActionScript Code
Flash Client Runtime Environments
Compilation
Quick Review
Classes and Objects
Creating a Program
,_ Packages
i Defining a Class
| Virtual Zoo Review
; Constructor Methods
Creating Objects
^Variables and Values
Constructor Parameters and Arguments
Expressions
Assigning One Variable's Value to Another
An Instance Variable for Our Pet
, , Instance Methods
Members and Properties
Virtual Zoo Review
Brea k Time!
. 3
3
4
5
6
6
8
9
11
13
14
16
19
24
26
28
30
31
42
42
43
2. Conditionals and Loops 4 4
Conditionals '4 4
Loops 5 0
Boolean Logic 5 8
Back to Classes and Objects 6 2
3. Instance Methods Revisited 63
Omitting the this Keyword 6 4
Bound Methods 6 6
Using Methods to Examine and Modify an Object's State 68
Get and Set Methods 7 2
Handling an Unknown Number of Parameters 75
Up Next: Class-Level Information and Behavior 76
4. Static Variables and Static Methods .7 7
Static Variables 7 7
Constants . 80
Static Methods 8 2
Class Objects 8 5
C++ and Java Terminology Comparison 8 6
On to Functions , 86
5. Functions 8 7
Package-Level Functions 8 8
Nested Functions 9 0
Source-File-Level Functions 9 1
Accessing Definitions from Within a Function 92
Functions as Values 9 3
Function Literal Syntax 9 3
Recursive Functions 9 5
Using Functions in the Virtual Zoo Program 96
Back to Classes 10 0
6. Inheritance 10 1
A Primer on Inheritance 10 1
Overriding Instance Methods ' 105
Constructor Methods in Subclasses 10 8
Preventing Classes from Being Extended and Methods
from Being Overridden 11 2
Table of Contents
Subclassing Built-in Classes' 11 3
The Theory of Inheritance ' 114
Abstract Not Supported 12 0
Using Inheritance in the Virtual Zoo Program 121
Virtual Zoo Program Code • . . . • 126
It's Runtime! 12 9
7. Compiling and Running a Program -.,.,., 130
Compiling with the Flash Authoring Tool . 130
Compiling with Flex Builder 2 , 131
Compiling with mxmlc - 133
Compiler Restrictions 13 4
The Compilation Process and the Classpath , 134
Strict-Mode Versus Standard-Mode Compilation 135
The Fun's Not Over 13 6
8. Datatypes and Type Checking .13 7
Datatypes and Type Annotations 13 8
Untyped Variables, Parameters, Return Values, and Expressions 142
Strict Mode's Three Special Cases 14 3
Warnings for Missing Type Annotations 144
Detecting Reference Errors at Compile Time 145
Casting 14 6
Conversion to Primitive Types 15 0
Default Variable Values " • • 153
null and undefined '|;?| 153
Datatypes in the Virtual Zoo • ' ' 154.
More Datatype Study Coming Up . 158
9. Interfaces 15 9
The Case for Interfaces 15 9
• * Interfaces and Multidatatype Classes , 161
\ Interface Syntax and Use 16 2
* Another Multiple-Type Example 16 5
, More Essentials Coming • 171
• -* -
10. ' Statements and Operators 172
', Statements 17 2
' Operators . 174
• ' Up Next: Managing Lists of Information 185
Tabl e o f Contents' I vi i
11. Arrays .. ... 186
• ' What Is an Array? 18 6
The Anatomy of an Array ' 187
Creating Arrays < .1 8 7
Referencing Array Elements 18 9
Determining the Size of an Array • 191
Adding Elements to an Array 19 3
Removing Elements from an Array 19 7
Checking the Contents of an Array with the toString() Method 199
Multidimensional Arrays 20 0
On to Events 20 1
12. Events and Event Handling 202
ActionScript Event Basics • '• 202
Accessing the Target Object 20 9
Accessing the Object That Registered the Listener 212
Preventing Default Event Behavior " ' 213
Event Listener Priority ' " 214
Event Listeners and Memory Management ' 216
Custom Events '2 2 1
Type Weakness in ActionScript's Event Architecture ' 233
Handling Events Across Security Boundaries 236
What's Next? 240
13. Exceptions and Error Handling 241
The Exception-Handling^Cycl e 24 1
Handling Multiple Types of Exceptions • 244
Exception Bubbling ' 253
The finally Block 25 8
Nested Exceptions 260
Control-Flow Changes in try/catch/finally • 264
Handling a Built-in Exception ' 267
More Gritty Work Ahead • 268
14. Garbage Collection 26 9
Eligibility for Garbage Collection 26 9
Incremental Mark and Sweep 27 2
Disposing of Objects Intentionally 27 3
Deactivating Objects 27 4
vii i I Tabl e o f Content s
Garbage Collection Demonstration . 277
On to ActionScript Backcountry • 27 8
15. Dynamic ActionScript 27 9
Dynamic Instance Variables , _ _ 280
Dynamically Adding New Behavior to an Instance 284
Dynamic References to Variables and Methods 286
Using/Dynamic Instance Variables to Create Lookup Tables 287
Using Functions to Create Objects . . 289
Using Prototype Objects to Augment Classes 291
The Prototype Chain 29 2
Onward! 29 4
16. Scop e .2 9 5
Global Scope 29 6
Class Scope 29 7
Static Method Scope , 298
Instance Method Scope 29 8
Function Scope . . , 299
Scope Summary .• t 300
The Internal Details •. 30 0
Expanding the Scope Chain via the with Statement . . 302
On to Namespaces • . 303
17. Namespaces 30 4
Namespace Vocabulary '}\ $». 304
ActionScript Namespaces ' I 305
Creating Namespaces 30 7
Using a Namespace to Qualify Variable and Method Definitions 310
Qualified Identifiers 31 2
A Functional Namespace Example 31 4
... Namespace Accessibility . • 317
\ Qualified-Identifier Visibility 32 1
I Comparing Qualified Identifiers 32 2
(Assigning and Passing Namespace Values 32 3
.Open Namespaces and the use namespace Directive 334
Namespaces for Access-Control Modifiers 33 8
'Applied Namespace Examples 34 1
'Final Core Topics 35 2
Tabl e o f Content s
18. XML and E4X 35 3
Understanding XML Data as a Hierarchy < • 353
Representing XML Data in E4X 35 5
Creating XML Data with E4X ' 357
Accessing XML Data • . 359
Processing XML with for-each-in and for-in 37 7
Accessing Descendants ' 37 9
Filtering XML Data • 38 3
Traversing XML Trees ' " 386
Changing or Creating New XML Content ' 387
Loading XML Data • 397
Working with XML Namespaces 39 8
Converting XML and XMLList to a String 40 4
Determining Equality in E4X 40 7
More to Learn 41 0
19. Flash Player Security Restrictions 411
What's Not in This Chapter 41 2
The Local Realm, the Remote Realm, and Remote Regions ' ' 412
Security-Sandbox-Types ' 413
Security Generalizations Considered Harmful - ' 415
Restrictions on Loading Conteht, Accessing Content as Data," -
Cross-Scripting, and Loading Data •••••.-.• • 415
Socket Security 42 2
Example Security Scenarios ' ' 422
Choosing a Local Security-Sandbox-Type ' 425
Distributor Permissions (Policy Files) 42 9
Creator Permissions (allowDomainO) , • ' 444
Import Loading 44 6
Handling Security Violations 44 8
Security Domains ' 450
Two Common Security-Related Development Issues 452
On to Part II! ' 454
Tabl e o f Content s
Part II. Display and Interactivity
20. The Display API and the Display List 457
Display API Overview 45 8
The Display List . 462
Containment Events 48 7
Custom'Graphical Classes v 499
Go with the Event Flow 50 1
21. Events and Display Hierarchies 502
Hierarchical Event Dispatch 50 2
Event Dispatch Phases 50 3
Event Listeners and the Event Flow 50 5
Using the'Event Flow to Centralize Code 51 1
Determining the Current Event Phase 51 4
Distinguishing Events Targeted at an Object from Events Targeted at That
Object's Descendants 51 6
Stopping an Event Dispatch 51 8
Event Priority and the Event Flow 52 2
Display-Hierarchy Mutation and the Event Flow 523
Custom Events and the Event Flow 52 6
On to Input Events 53 0
22. Interactivity 53 1
Mouse-Input Events 'i \ «,». 532
Focus Events * •*] 548
Keyboard-Input Events 55 5
* Text-Input Events 56 5
Flash Player-Level Input Events 58 0
From the Program to the Screen . 586
V
23. Screen Updates 58 7
Scheduled Screen Updates , 587
Post-Event Screen Updates 59 6
Redraw Region 600
Optimization with the Event.RENDER Event 601
Let's Make It Move! 60 9
Tabl e o f Content s
24. Programmatic Animation 61 0
No Loops ''61 0
Animating with the ENTER_FRAME Event 611
Animating with the TimerEvent.TIMER Event 616
Choosing Between Timer and Event.ENTER_FRAME 623
A Generalized Animator ' 624
Velocity-Based Animation 62 7
Moving On to Strokes 'n' Fills ' ' 628
25. Drawing with Vectors 62 9
Graphics Class Overview 62 9
Drawing Lines 630
Drawing Curves 63 3
Drawing Shapes 63 4
Removing Vector Content 63 6
Example: An Object-Oriented Shape Library' 637
From Lines to Pixels 64 7
26. Bitmap Programming 64 8
The BitmapData and Bitmap Classes 64 9
Pixel Color Values : ' ' 649
Creating a New Bitmap Image 65 4
Loading an External Bitmap Image ' 656
Examining a Bitmap 65 8
Modifying a Bitmap 66 4
Copying Graphics jt'o^a BitmapData Object 672
Applying Filters and Effects 68 6
Freeing Memory Used by Bitmaps 69 4
Words, Words, Words 69 5
27. Text Display and Input 69 6
Creating and Displaying Text 69 9
Modifying a Text Field's Content 70 5
Formatting Text Fields 70 8
Fonts and Text Rendering 73 5
Missing Fonts and Glyphs 74 8
Determining Font Availability 74 9
Determining Glyph Availability 75 1
Embedded-Text Rendering 75 2
xi i I Tabl e o f Content s
Text Fiel d Inpu t . . , ..,.•..'. " . 75 5
Text Field s and the Flas h Authorin g Too l •;, 75- 9
Loading...Pleas e Wait... 76 1
28. Loadin g Externa l Displa y Asset s 76 2
Using Loader to Load Display Assets at Runtime : 763
Compile-Time Type-Checking for Runtime-Loaded Assets 781
Accessing Assets in Multiframe .swf Files • ' 790
Instantiating a Runtime-Loaded Asset . • 793
Using Socket to Load Display Assets at Runtime 796
Removing Runtime Loaded .swf Assets 80 6
Embedding Display Assets at CompileTime 807
On to Part III 81 8
Par t III. Applie d ActionScrip t Topic s
29. ActionScrip t an d th e Flas h Authorin g Too l 82 1
The Flash Document 82 1
Timelines and Frames 82 2
Timeline Scripting 82 6
The Document Class 82 8
Symbols and Instances 83 2
Linked Classes for Movie Clip Symbols 83 4
Accessing Manually Created Symbol Instances 838
Accessing Manually Created Text ' ''l l w ^44
Programmatic Timeline Control • ' i 845
Instantiating Flash Authoring Symbols via ActionScript 847
Instance Names for Programmatically Created Display Objects 848
Linking Multiple Symbols to a Single Superclass 849
The Composition-Based Alternative to Linked Classes 851
\ Preloading Classes 85 2
\ Up Next: Using the Flex Framework 85 5
Tabl e o f Content s I xii i
30. A Minimal MXML Application 856
The General Approach 85 6
A Real UI Component Example 85 9
Sharing with Your Friends 860
31. Distributing a Class Library 861
Sharing Class Source Files • 862
Distributing a Class Library as a .swc File - • 863
Distributing a Class Library as a .swf File • 867
But Is It Really Over? 87 3
Appendix 87 5
Index 89 1
xi v I Tabl e o f Content s