Table Of Content6188FM.qxd 3/8/06 12:48 PM Page i
Foundation ActionScript
for Flash 8
Kristian Besley
Sham Bhangal
David Powers
with Eric Dolecki
6188FM.qxd 3/8/06 12:48 PM Page ii
Foundation ActionScript for Flash 8
Copyright © 2006 by Kristian Besley, Sham Bhangal, and David Powers
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage or retrieval system, without the
prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-618-0
ISBN-10 (pbk): 1-59059-618-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked 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 orders-ny@springer-sbm.com, or visit www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710.
Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, 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.comin the Downloads section.
Credits
Lead Editor Assistant Production Director
Chris Mills Kari Brooks-Copony
Technical Reviewers Production Editor
Kristian Besley and David Powers Kelly Winquist
Editorial Board Compositor
Steve Anglin, Dan Appleman, Dina Quan
Ewan Buckingham, Gary Cornell,
Jason Gilmore, Jonathan Hassell, Artist
James Huddleston, Chris Mills, Kinetic Publishing Services, LLC
Matthew Moodie, Dominic Shakeshaft,
Jim Sumser, Matt Wade Proofreader
Linda Seifert
Project Manager Indexer
Denise Santoro Lincoln Julie Grady
Copy Edit Manager Cover Image Designer
Nicole LeClerc Corné van Dooren
Copy Editors Interior and Cover Designer
Ami Knox and Nicole LeClerc Kurt Krames
Manufacturing Director
Tom Debolski
6188FM.qxd 3/8/06 12:48 PM Page iii
CONTENTS AT A GLANCE
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Cover Image Designer . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Chapter 1 Interactive Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2 Making Plans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Chapter 3 Movies That Remember . . . . . . . . . . . . . . . . . . . . . . . . . 59
Chapter 4 Movies That Decide for Themselves . . . . . . . . . . . . . . . 105
Chapter 5 More Power, Less Script . . . . . . . . . . . . . . . . . . . . . . . . 139
Chapter 6 Movies That Remember How to Do Things . . . . . . . . . . 185
Chapter 7 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Chapter 8 Objects on the Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Chapter 9 Reusable Code and Realistic Movement . . . . . . . . . . . . 299
Chapter 10 Games and Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Chapter 11 Drawing API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Chapter 12 Adding Sound to Flash . . . . . . . . . . . . . . . . . . . . . . . . . 453
Chapter 13 Loading Dynamic Data with XML . . . . . . . . . . . . . . . . . 487
Chapter 14 Finishing the Futuremedia Case Study . . . . . . . . . . . . . 525
Chapter 15 Advanced ActionScript: Components and Classes . . . . 559
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
iii
6188FM.qxd 3/8/06 12:48 PM Page v
CONTENTS
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Cover Image Designer . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Chapter 1 Interactive Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Giving your movies instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Working with the Actions panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Direct typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Who are you talking to? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Controlling movie clips on the stage . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Listening to what your movies are telling you . . . . . . . . . . . . . . . . . . . . . . . . 20
Events in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
External events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Internal events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Introducing event handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Nesting spiders—argh! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Animating movie clips with ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . 25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Chapter 2 Making Plans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Defining the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Keep your ideas in a safer place than your head . . . . . . . . . . . . . . . . . . . . 32
Storyboarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Building your ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Thinking from the top down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Thinking from the bottom up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Flowcharting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Book project: Introducing the Futuremedia site . . . . . . . . . . . . . . . . . . . . . . 44
Solving problem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Solving problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Solving problems 3 and 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
v
6188FM.qxd 3/8/06 12:48 PM Page vi
CONTENTS
Chapter 3 Movies That Remember . . . . . . . . . . . . . . . . . . . . . . . . . 59
Introducing variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Values and types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Creating variables and using them with literals and expressions . . . . . . . . . . . . 63
Naming variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Creating variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Using literal values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Using expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Input and output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Using string expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Working with numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Other uses for numeric expressions . . . . . . . . . . . . . . . . . . . . . . . . . 81
Working with Boolean values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Logic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Reasons for using arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Arrays let you store related information together . . . . . . . . . . . . . . . . . 86
Arrays let you hold information in a specific order . . . . . . . . . . . . . . . . . 86
Arrays let you index information . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Arrays let you link information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Creating a new array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Typing an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Using variable values as offsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Book project: Starting the Futuremedia site design . . . . . . . . . . . . . . . . . . . . . 92
What you’re going to do . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
What you’re going to learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Choosing and setting the stage size . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Setting up the timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Creating layout guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Adding the position and status text . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Choosing a font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Adding the text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Embedding the font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Chapter 4 Movies That Decide for Themselves . . . . . . . . . . . . . . . 105
Decision making . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Making decisions in ActionScript: The if action . . . . . . . . . . . . . . . . . . . . 107
Defining a decision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Alternative actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Acting on alternatives: The else action . . . . . . . . . . . . . . . . . . . . . . . . . 118
More than one alternative: The else if action . . . . . . . . . . . . . . . . . . . . . 119
Handling lots of alternatives: The switch action . . . . . . . . . . . . . . . . . . . . 123
Switching without breaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Organizing your code in the correct order . . . . . . . . . . . . . . . . . . . . . . . . . 136
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
vi
6188FM.qxd 3/8/06 12:48 PM Page vii
CONTENTS
Chapter 5 More Power, Less Script . . . . . . . . . . . . . . . . . . . . . . . . 139
Timeline loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
ActionScript loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
while loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Useful things to do with while loops . . . . . . . . . . . . . . . . . . . . . . . . . . 142
for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Some useful examples of for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Simple loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Reverse loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Two at a time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Looping through elements in an array . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Applying an operation to all the elements in an array . . . . . . . . . . . . . . . . 155
Searching an array for a specific value . . . . . . . . . . . . . . . . . . . . . . . . . 156
Cross-indexing a pair of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Hangman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Book project: Creating the static graphics . . . . . . . . . . . . . . . . . . . . . . . . . 172
Setting up the timeline layers for your graphics . . . . . . . . . . . . . . . . . . . . 174
Creating the back strip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Adding structure to the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating the frame cutout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Revealing the frame cutout and finishing the FLA . . . . . . . . . . . . . . . . . . . 180
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 6 Movies That Remember How to Do Things . . . . . . . . . . 185
Breaking down a task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Bundling actions and running them afterward . . . . . . . . . . . . . . . . . . . . . . . 187
Using anonymous and named functions . . . . . . . . . . . . . . . . . . . . . . . . 187
Using functions to hide evil math . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Using functions to take care of repetitive jobs . . . . . . . . . . . . . . . . . . . . . 191
Choosing which actions to bundle and where . . . . . . . . . . . . . . . . . . . . . . . 192
Arguments and redundancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Local variables and modular code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Returning values from a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Typing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Running in circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Nesting functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Using nested functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Using more function nesting to tidy up your script . . . . . . . . . . . . . . . . . . 210
Book project: Creating the dynamic graphics . . . . . . . . . . . . . . . . . . . . . . . . 213
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
vii
6188FM.qxd 3/8/06 12:48 PM Page viii
CONTENTS
Chapter 7 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Introducing objects and classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Type and object-oriented programming . . . . . . . . . . . . . . . . . . . . . . . . 224
Classes, generalization, and abstraction . . . . . . . . . . . . . . . . . . . . . . . . . 224
Seeing arrays in a new light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The Array constructor method . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Other Array methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Array properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Creating classes and objects (instances) in Flash . . . . . . . . . . . . . . . . . . . . . . 229
Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
The Object object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Viewing an object in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Objects, objects, everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Lurking objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Making a show reel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Book project: Initializing the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Getting yourself comfortable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Initializing the site variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Telling ActionScript about the stage . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Sanity check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Setting up your tricolor colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Sanity check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Final words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Chapter 8 Objects on the Stage . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Movie clips and buttons as objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Symbol types and behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Two sides of the same object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Working with Library items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Let chaos reign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Bitmap caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
The other side of bitmap caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Bitmap caching and RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Changing the appearance of a cached movie clip . . . . . . . . . . . . . . . . . . . 285
When to use bitmap caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Referencing different timelines with ActionScript . . . . . . . . . . . . . . . . . . . . . 287
Different place, different variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Locating variables from inside an event handler function . . . . . . . . . . . . 288
Reusing handler functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
The apply method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Global variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
viii
6188FM.qxd 3/8/06 12:48 PM Page ix
CONTENTS
Chapter 9 Reusable Code and Realistic Movement . . . . . . . . . . . . 299
Breaking down big tasks into smaller ones . . . . . . . . . . . . . . . . . . . . . . . . . 300
Black-box programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Creating simple components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Creating a modular set of playback controls . . . . . . . . . . . . . . . . . . . . . . 303
Dark Valentine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Modular control of movie clips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
How to simulate realistic movement . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Motion with acceleration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Trailing the pointer (a mouse follower) . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Understanding the inertial code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Fine-tuning the component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Function-based modular code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Swarming behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Taking the swarming effect forward . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Creating tweens with ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Using import to access the Tween class . . . . . . . . . . . . . . . . . . . . . . . . . 333
How to use the Tween constructor function . . . . . . . . . . . . . . . . . . . . . . 334
Understanding the transition types . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Book project: Setting up the color transition . . . . . . . . . . . . . . . . . . . . . . . . 338
Making it modular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Sanity check #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Sanity check #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Running the FLA: The results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Chapter 10 Games and Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
What is a sprite? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
External and internal data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Planning zapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
The game world (the main timeline) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
gSCREEN_TOP, gSCREEN_BOTTOM, gSCREEN_RIGHT, and gSCREEN_LEFT . . . . . 368
score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
level, skill, and accel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
shipDead, fired, and gSHIP_HEIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . 368
The timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
ix
6188FM.qxd 3/8/06 12:48 PM Page x
CONTENTS
The code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Global constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
The “start game” trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
The player (the ship) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
The SwarmAlien . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
alienSpawn() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
onEnterFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
alienBrain() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
alienReincarnate() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
The SwarmAlien code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
The bullet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
The debris of war . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Book project: Navigation event handling . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Sanity check #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Adding the basic UI animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
navigate() and posTransition() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Adding typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Sanity check #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Creating a smooth transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Chapter 11 Drawing API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Turtle graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Drawing lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Drawing curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
How the sketching code works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Filling shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Creating a kaleidoscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Kaleidoscope math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Building the kaleidoscope engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Book project: Color transition event handling and data . . . . . . . . . . . . . . . . . . 435
Wiring the colTransition() function . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
The game plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Fading color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Coding the color transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Finishing the text transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Reviewing the code so far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Data-driven sites (and why you need to understand them) . . . . . . . . . . . . . . 442
Defining data for Futuremedia . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
x