Beginning ActionScript 2.0 2006
.pdfAcknowledgments
There are too many people to count who have given me support and well wishes as I have worked away on this book. I would first like to acknowledge my parents for their constant love and support. They have helped to make me who I am. They also inspired me to pursue computing science as a career, for which I am eternally grateful. I would also like to acknowledge my fiancée, Renée. She has been supremely caring, supportive, and patient, has given me encouragement when I needed it. Through her actions she has been brought even closer to me than I could have imagined.
I would also like to acknowledge Jen DeHaan (www.deseloper.com), who gave me the generous gift of the opportunity to write this book, as well as providing me with a forum to help the Flash developer community. I am very grateful for her generosity. I am also grateful for the whole support team at Wiley, including my editor, Maryann Steinhart, for her tireless efforts to make this book happen, and my technical editor, Sean Christmann, who helped me with many valuable suggestions for improvements throughout the book.
There is no way I could undertake the effort of writing this book on my own. A big thank-you goes to Jeff Berg, who took on the daunting task of writing half of this book. He has been a great person to work with, both with the book and at IBM: Jeff has always been available to bounce ideas off of, is very creative with his solutions, and loves playing with the technology. He has also been a support on a personal level and a friend.
Thank you to Greg Charbonneau for making the time to take my photo for the cover. It was through the camera I initially bought from him that I earnestly started photography as a hobby. Thank you to all my friends and co-workers who regularly asked me about my book, and to my manager Elvyna who supported my writing efforts. Finally, thank you also for all those in the Flash Lounge who have been my champions, and who have been chomping at the bit to get an autographed copy of the book. At last it is ready!
—Nathan Derksen
I want to thank my co-author, Nathan Derksen, for his support and guidance. Nathan, your attitude, knowledge, and dedication are an inspiration. Also wish to thank Jack Blanchard. Jack, you taught me what an if statement was so many years ago, and you continue to guide me to new discoveries.
I could not have completed this project without the patient support of my wife Kara, the inspiration from my son Cary, and joy from my daughter Evie.
—Jeff Berg
Contents
Acknowledgments |
viii |
Introduction |
xxv |
Chapter 1: Getting Started with Macromedia Flash |
1 |
Introducing the Development Environment |
1 |
Tools Panel |
2 |
Properties Panel |
2 |
Timeline |
3 |
Keyframes and Animation |
3 |
About the Library and Symbols |
4 |
Working with Buttons |
5 |
Working with Movie Clips |
7 |
About Layers, Depth, and Levels |
8 |
Setting Up Your Scripting Environment |
8 |
Introducing the Actions Panel |
8 |
Exploring ActionScript Preferences |
9 |
Automatic Indentation |
9 |
Tab Size |
10 |
Code Hints |
10 |
Font |
10 |
Color |
11 |
ActionScript 2.0 Settings |
11 |
Exploring Auto Format Preferences |
12 |
Publishing a Project |
13 |
The Flash Tab |
14 |
The HTML Tab |
15 |
The Image Tabs |
15 |
Options for Detecting the Flash Player |
16 |
Summary |
18 |
Exercises |
18 |
Chapter 2: Getting Started with ActionScript 2.0 |
19 |
Understanding Statements |
19 |
Using Simple Statements |
19 |
Using Compound Statements |
20 |
Contents
Understanding Operators |
20 |
Using Operator Precedence |
22 |
Exploring Operator Associativity |
22 |
Understanding the Common Operators |
23 |
Using White Space |
25 |
Using Comments |
26 |
Introducing Variables |
27 |
Assigning Data to a Variable |
28 |
Viewing the Contents of a Variable |
28 |
Passing Data from Variable to Variable |
29 |
Naming Variables |
29 |
Introducing Constants |
30 |
Using Strong Variable Typing |
30 |
Exploring Data Types |
33 |
Distinguishing Primitive and Composite Data Types |
34 |
Passing by Value Versus Pass by Reference |
35 |
Duplicating Data for Composite Types |
36 |
Using Dot Syntax |
37 |
Discerning Special Variables and Keywords |
38 |
_root |
38 |
_level0 |
39 |
_global |
39 |
this |
39 |
Working with Collections of Data |
40 |
Understanding Arrays |
40 |
concat() |
42 |
join() |
43 |
push() |
43 |
sort() |
43 |
splice() |
45 |
Exploring Associative Arrays and Objects |
45 |
Viewing the Object Class as a Container |
46 |
Revisiting the Associative Array |
47 |
Summary |
50 |
Exercises |
51 |
Chapter 3: Understanding ActionScript Expressions and Loops |
53 |
Making Decisions |
53 |
Expressions |
54 |
Building Expressions |
55 |
Using if..then..else |
59 |
Using switch..case |
60 |
x
|
Contents |
Understanding Loops |
66 |
The for Loop |
66 |
The for..in Loop |
69 |
The while Loop |
70 |
The do..while Loop |
72 |
Dealing with Loop Errors |
76 |
Off-by-One Errors |
76 |
Infinite Loops |
77 |
Summary |
80 |
Exercises |
80 |
Chapter 4: Exploring ActionScript Functions and Scope |
83 |
Functions |
83 |
Functions in Action |
86 |
An Alternate Syntax |
87 |
Passing Functions as Arguments |
88 |
Variable Scope |
92 |
Managing Variable Scope |
95 |
Side Effects |
96 |
Summary |
98 |
Exercises |
99 |
Chapter 5: Getting Started with Coding |
101 |
Introduction to Object-Oriented Programming |
101 |
Defining Object-Oriented Programming |
101 |
Classes Versus Objects |
103 |
Class Inheritance |
106 |
Goals of Object-Oriented Programming |
106 |
Coding Best Practices |
107 |
Understanding Variable Naming |
107 |
Making Variable Names Readable |
107 |
Making Boolean Variables into Questions |
108 |
Avoiding Reserved Words |
108 |
Distinguishing Variable Names from Class Names |
110 |
Variable Typing |
110 |
Type Casting |
112 |
Commenting |
116 |
Formatting Code |
118 |
Understanding Variable Scope |
119 |
Declaring All Global and Timeline Variables in One Place |
119 |
Creating an Object Just for Data |
120 |
xi
Contents
Creating Access Functions |
120 |
Managing Data with Custom Classes |
121 |
Accessing Variables in Another Timeline |
122 |
Creating Your Own Functions |
129 |
Strongly Typing Function Arguments |
129 |
Avoiding Hard-Coded Movie Clip References |
130 |
Declaring Return Values |
130 |
Avoiding Oversized Functions |
131 |
Avoiding Entanglement |
131 |
Avoiding Duplication |
133 |
Summary |
140 |
Exercises |
141 |
Chapter 6: Setting Up Flash Projects |
143 |
Setting Up the Library |
143 |
Working with Bitmap Images |
144 |
Keep Images Organized on the Desktop |
145 |
Keep Images Organized in the Library |
145 |
Embed Images into Movie Clips |
145 |
Nesting Sections within a Movie Clip |
148 |
Using Keyframes to Manage Application State |
149 |
Keeping Code in External Files |
151 |
Using Script to Manage Application State |
153 |
Summary |
157 |
Exercise |
157 |
Chapter 7: Controlling Movie Clips |
159 |
The Movie Clip Revealed |
159 |
MovieClip Class Methods |
160 |
attachBitmap() |
161 |
attachMovie() |
162 |
beginFill() |
162 |
clear() |
163 |
createEmptyMovieClip() |
163 |
createTextField() |
163 |
duplicateMovieClip() |
163 |
endFill() |
164 |
getBytesLoaded() |
164 |
getBytesTotal() |
165 |
getURL() |
166 |
gotoAndPlay() |
167 |
hitTest() |
167 |
xii
|
Contents |
lineStyle() |
168 |
lineTo() |
169 |
loadMovie() |
169 |
moveTo() |
170 |
play() |
170 |
removeMovieClip() |
170 |
setMask() |
171 |
startDrag() |
171 |
stop() |
172 |
stopDrag() |
172 |
swapDepths() |
173 |
unloadMovie() |
173 |
MovieClip Class Properties |
174 |
MovieClip Class Events |
176 |
Creating Movie Clips On-the-Fly |
178 |
Attaching Movie Clips from the Library |
181 |
Loading External Movies |
184 |
Loading Movies into Levels |
184 |
Loading Media into Existing Movie Clips |
185 |
Fully Qualified, Absolute, and Relative URLs |
186 |
Using Movie Clips as Masks |
190 |
Improving Movie Clip Performance |
196 |
Bitmap Caching |
196 |
Transparency |
196 |
Filters and Blending Modes |
197 |
Full Screen |
197 |
Summary |
197 |
Exercises |
198 |
Chapter 8: Pre-Loading Movies |
199 |
Using a Custom Pre-Loader |
199 |
Polling with onEnterFrame() Versus setInterval() |
204 |
Understanding the MovieClipLoader Class |
205 |
MovieClipLoader Class Events |
205 |
MovieClipLoader Class Methods |
206 |
addListener() |
206 |
getProgress() |
207 |
loadClip() |
207 |
removeListener() |
208 |
unloadClip() |
208 |
Implementing the MovieClipLoader Class |
209 |
xiii
Contents
Examining the Loader and ProgressBar Components |
212 |
Loader Component Method, Properties, and Events |
212 |
ProgressBar Component Method, Properties, and Events |
213 |
Implementing the Loader and ProgressBar Components |
215 |
Strategies for Using Pre-Loaders |
217 |
The Monolithic Movie Approach |
217 |
The Split-Up Movie Approach |
221 |
Summary |
225 |
Exercises |
226 |
Chapter 9: Working with Components |
229 |
New to Version 2.0 |
229 |
Exploring Components |
230 |
Data Components |
230 |
FLV Playback and FLV Playback Custom UI Components |
231 |
Media Components for Flash Players 6 and 7 |
233 |
User Interface Components |
233 |
Placing Components Manually |
234 |
Placing Components on the Stage with Script |
237 |
Scripting Components |
242 |
Summary |
245 |
Exercises |
245 |
Chapter 10: Interacting with the User |
247 |
Handling Events |
247 |
Creating a Listener Object |
247 |
Variation of Creating a Listener Object |
254 |
Creating a Listener Function |
255 |
Which Listener Technique Should You Use? |
257 |
Attaching Multiple Listeners to Multiple Components |
262 |
Organizing the Listeners |
263 |
Handling Events from Multiple Sources |
264 |
Manually Invoking Events in a Component |
270 |
Summary |
271 |
Exercise |
272 |
Chapter 11: Controlling Components |
273 |
Introducing the Component Inspector Panel |
273 |
Creating Data Bindings between Components Using the Bindings Tab |
275 |
Using an XML File as a Data Source |
277 |
xiv
|
Contents |
Controlling Component Appearance |
281 |
Using setStyle() to Change Component Styles |
282 |
Setting Styles Globally |
282 |
Setting Styles for a Component Type |
283 |
Setting Styles Using Custom Classes |
283 |
Style Search Order |
283 |
Available Styles |
284 |
Alert Style Names |
286 |
Custom Date Classes |
287 |
Skinning Components |
290 |
Summary |
294 |
Exercises |
295 |
Chapter 12: Debugging Debugged |
297 |
Types of Bugs |
297 |
Compile-Time Bugs |
297 |
Logic Bugs |
298 |
If Statement Logic Errors |
298 |
Uninitialized Variables |
299 |
Off-by-One Errors |
301 |
Fence Post Errors |
301 |
Infinite Loops |
302 |
Numerical Precision Errors |
302 |
Develop to Debug |
303 |
Make Your Code Readable |
303 |
Develop in Small Chunks |
304 |
Use Small Functions |
304 |
The Science of Debugging |
305 |
Develop a Theory |
305 |
Run an Experiment |
306 |
Using the Built-In Debugger |
306 |
Using the Trace Statement |
311 |
Commenting Out Code |
312 |
Analyze the Results |
313 |
Summary |
314 |
Exercises |
314 |
Chapter 13: Working with Vector Graphics |
319 |
Using the Drawing API |
320 |
Tools for Drawing Vectors with ActionScript |
320 |
lineStyle() |
320 |
beginFill() |
322 |
xv
Contents
beginBitmapFill() |
322 |
beginGradientFill() |
323 |
endFill() |
324 |
moveTo() |
324 |
lineTo() |
325 |
curveTo() |
325 |
clear() |
325 |
Drawing Vectors with ActionScript |
325 |
The Matrix Object |
334 |
Summary |
334 |
Exercises |
334 |
Chapter 14: Applying Filter Effects |
335 |
DropShadowFilter |
336 |
BlurFilter |
337 |
GlowFilter |
337 |
BevelFilter |
338 |
GradientGlowFilter |
339 |
GradientBevelFilter |
339 |
ConvolutionFilter |
340 |
ColorMatrixFilter |
343 |
DisplacementMapFilter |
345 |
Cloning Filters |
346 |
Applying Multiple Filters |
346 |
Applying Blending Modes |
352 |
Summary |
353 |
Exercises |
353 |
Chapter 15: Working Directly with Bitmap Data |
355 |
The Bitmap Object’s Methods |
356 |
applyFilter() |
356 |
clone() |
356 |
colorTransform() |
356 |
copyChannel() |
357 |
copyPixels() |
357 |
dispose() |
357 |
draw() |
358 |
fillRect() |
358 |
floodFill() |
358 |
generateFilterRect() |
359 |
xvi
|
Contents |
getColorBoundsRect() |
359 |
getPixel() |
359 |
getPixel32() |
360 |
hitTest() |
360 |
loadBitmap() |
360 |
merge() |
361 |
noise() |
361 |
paletteMap() |
362 |
perlinNoise() |
362 |
pixelDissolve() |
363 |
scroll() |
364 |
setPixel() |
364 |
setPixel32() |
364 |
threshold() |
365 |
The bitmapData Object’s Properties |
365 |
Converting a Movie Clip into a Bitmap Image |
366 |
Manipulating Bitmap Data |
366 |
Summary |
368 |
Exercises |
368 |
Chapter 16: Using ActionScript for Animation |
369 |
Scripted Versus Non-Scripted Animation |
369 |
Creating Animations Using Keyframes |
370 |
Creating Animations Using ActionScript |
372 |
Moving a Movie Clip Using onEnterFrame() |
372 |
Moving a Movie Clip Using setInterval() |
374 |
Frame-Based Versus Time-Based Animation |
376 |
The Effect of Movie Frame Rate on Animation |
376 |
Which Technique Should You Use? |
379 |
Choosing a Frame Rate or Update Interval |
379 |
Creating Animation with Movie Clips |
380 |
Animating Multiple Movie Clips |
381 |
Adding Random Behavior |
382 |
Introducing Easing and Acceleration |
387 |
Easing Out |
387 |
Easing In |
391 |
Summary |
402 |
Exercises |
402 |
xvii