Apress Foundation ActionScript For Flash 8 Apr 2006 ISBN 1590596188 pdf

  Foundation ActionScript for Flash 8 Kristian Besley Sham Bhangal

  David Powers with Eric Dolecki

  

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.com in 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

  CONTENTS AT A GLANCE

About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

About the Cover Image Designer . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

  Chapter 1 Interactive Flash

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

  Chapter 2 Making Plans

  . . . . . . . . . . . . . . . . . . . . . . . . . 59

  Chapter 3 Movies That Remember

  . . . . . . . . . . . . . . . 105

  Chapter 4 Movies That Decide for Themselves

  . . . . . . . . . . . . . . . . . . . . . . . . 139

  Chapter 5 More Power, Less Script

  . . . . . . . . . . 185

  Chapter 6 Movies That Remember How to Do Things

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

  Chapter 7 Objects and Classes

  . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

  Chapter 8 Objects on the Stage

  . . . . . . . . . . . . 299

  Chapter 9 Reusable Code and Realistic Movement

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

  Chapter 10 Games and Sprites

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

  Chapter 11 Drawing API

  . . . . . . . . . . . . . . . . . . . . . . . . . 453

  Chapter 12 Adding Sound to Flash

  . . . . . . . . . . . . . . . . . 487

  Chapter 13 Loading Dynamic Data with XML

  . . . . . . . . . . . . . 525

  Chapter 14 Finishing the Futuremedia Case Study

  . . . . 559

  Chapter 15 Advanced ActionScript: Components and Classes Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

  

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

  CONTENTS CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . 59

  Chapter 3 Movies That Remember 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

  . . . . . . . . . . . . . . . 105

  Chapter 4 Movies That Decide for Themselves 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

  CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . 139

  Chapter 5 More Power, Less Script 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 . . . . . . . . . . 185

  Chapter 6 Movies That Remember How to Do Things 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 CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

  Chapter 7 Objects and Classes 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

  . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

Chapter 8 Objects on the Stage 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

  CONTENTS . . . . . . . . . . . . 299

  Chapter 9 Reusable Code and Realistic Movement 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

  . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

  Chapter 10 Games and Sprites 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 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Chapter 11 Drawing API 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

  CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . 453

  Chapter 12 Adding Sound to Flash Choosing the right sound format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 Using sound on a timeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 Using the ActionScript Sound class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Playing sounds from ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 Attaching sounds to a sound instance . . . . . . . . . . . . . . . . . . . . . . . 458 Starting and stopping sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Creating dynamic soundtracks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 Using ActionScript to control volume and balance . . . . . . . . . . . . . . . . 469 Dealing with large sound files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 Using compression to reduce download times . . . . . . . . . . . . . . . . . . . . . 476 Loading sound from external files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 Silence can also be golden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

  . . . . . . . . . . . . . . . . . 487

  Chapter 13 Loading Dynamic Data with XML XML 101 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 How an XML document is structured . . . . . . . . . . . . . . . . . . . . . . . . . . 490 Using the right version and encoding . . . . . . . . . . . . . . . . . . . . . . . . . . 492 Using non-English text with XML in Flash . . . . . . . . . . . . . . . . . . . . . . 492 Saving XML files in UTF-8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 How tags are used in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 Checking that your document is well formed . . . . . . . . . . . . . . . . . . . . . 496 Loading XML into Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Book project: Controlling structure and content with XML . . . . . . . . . . . . . . . . 503 Building the basic XML structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 Loading the XML data into Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Creating the Futuremedia site’s data structure . . . . . . . . . . . . . . . . . . . . . 511 How the page array is structured . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Populating the actual data values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Moving to the next level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 Sanity check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522

  . . . . . . . . . . . . . 525

  Chapter 14 Finishing the Futuremedia Case Study Getting the data into the user interface . . . . . . . . . . . . . . . . . . . . . . . . . . 526 Amending the way events are handled . . . . . . . . . . . . . . . . . . . . . . . . 530 Reading content pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 Adding the backward path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 Making sure the UI knows where it needs to return . . . . . . . . . . . . . . . . . . 533 How the buildIcon() function works . . . . . . . . . . . . . . . . . . . . . . . . 536 Tidying up the user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 A great big sanity check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544 Adding the status text messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548 Adding a preloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 Loading your own content into the site . . . . . . . . . . . . . . . . . . . . . . . . 555 Parting shots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 CONTENTS . . . . 559

Chapter 15 Advanced ActionScript: Components and Classes Flash version 2 components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Getting more out of components with ActionScript . . . . . . . . . . . . . . . . . . 563 Components and event handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 How event listeners work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 Adding an event listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 Understanding the advantages of event listeners . . . . . . . . . . . . . . . . . 570 Commonly used components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572 Radio buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572 Check boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Combo boxes (drop-down menus) . . . . . . . . . . . . . . . . . . . . . . . . . 576 Using code hints with components . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 Loading components at runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 Removing components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 Removing listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 ActionScript and OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 The future road map for ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . 588 Is OOP for me? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 How class-based coding works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591 Extending classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 Extending a built-in class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600 Storing classes in a central location . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 Final thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

  ABOUT THE AUTHORS Kristian Besley

  is a Flash/web developer working in education and specializing in interactivity and dynamically driven content using ASP.NET and PHP. In his spare time, he is also a lecturer in multi- media at the higher education level.

  Kristian has written a number of friends of ED books, such as the

  Foundation Flash series (including the recently published Foundation Flash 8), Flash MX Video, and Learn Programming with Flash MX. He was a contributor to the Flash Math Creativity books, Flash MX 2004 Games Most Wanted, Flash MX Video Creativity, and

  countless others. He also writes for Computer Arts magazine and has

  Image courtesy of Simon James at

www.thefresh.co.uk produced freelance work for numerous clients, including the BBC.

  Kristian currently resides in Swansea, Wales, the city of his birth. He is a fluent Welsh speaker and is the creator of the first-ever Welsh translation search plug-in for Firefox and Mozilla (available from http://mycroft.mozdev.org).

  Sham Bhangal

  has written on new media for friends of ED since the imprint’s inception. In that time, he has been involved in the writing, production, and specification of just under 20 books. Sham has considerable working experience with Macromedia and Adobe products, with a focus on web design and motion graphics. Creating books that tell other people about his favorite subjects is probably the best job he has had (ignoring the long hours, aggressive deadlines, lost manuscripts, and occasional wiped hard drives). If he was doing something else, he’d probably be losing sleep thinking about writing anyway. Sham currently lives in the north of England with his longtime partner, Karen.

  David Powers

  is a professional writer who has been involved in electronic media for more than 30 years, first with BBC radio and television, and more recently with the Internet. This is his sixth book for Apress/friends of ED on programming for the Web. Among his previous titles are the highly successful Foundation PHP 5 for Flash (friends of ED, ISBN: 1-59059-466-5) and Foundation PHP for

  Dreamweaver 8 (friends of ED, ISBN: 1-59059-569-6). David’s other

  main area of expertise is Japan. He was a BBC correspondent in Tokyo during the late 1980s and early 1990s, and later was Editor, BBC Japanese TV. He has also translated several plays from Japanese.

ABOUT THE COVER IMAGE DESIGNER

  Corné van Dooren designed the front cover image for this book.

  Having been given a brief by friends of ED to create a new design for the Foundation series, he was inspired to create this new setup com- bining technology and organic forms. With a colorful background as an avid cartoonist, Corné discovered the infinite world of multimedia at the age of 17—a journey of discovery that hasn’t stopped since. His mantra has always been “The only limit to multimedia is the imagination,” a philosophy that is keeping him moving forward constantly.

  After enjoying success after success over the past years—working for many international clients, as well as being featured in multimedia magazines, testing software, and working on many other friends of ED books—Corné decided it was time to take another step in his career by launching his own company, Project 79, in March 2005.

  You can see more of Corné’s work and contact him through www.cornevandooren.com or www.project79.com. If you like his work, be sure to check out his chapter in New Masters of

  Photoshop: Volume 2 (friends of ED, ISBN: 1-59059-315-4).

  INTRODUCTION

  Welcome to Foundation ActionScript for Flash 8, the fourth edition of this legendary ActionScript book. ActionScript is, quite simply, the driving force behind Flash applications, allowing you to go beyond simple tweened animations and give your movies intelligence, power, and class! The current version of ActionScript in Flash 8, 2.0, is a fully featured, very powerful programming language. But ActionScript is that scary code stuff that programmers do, right? Wrong. ActionScript adds power and potential to your design work. It’s not going to turn you into a reclusive nerd who speaks in 1s and 0s, and who only comes out after dark. It’s going to turn you into someone who finally has the power to achieve his or her web design goals, rather than being hemmed in by frustrating limitations. And Flash 8 has a treasure trove of new features for you to play with. It has amazing new design fea- tures such as filters and blend modes, features such as bitmap caching to enhance the speed of your movies, exciting new video capabilities, a great new BitmapData API for manipulating images on the fly, and much more. If you know nothing or little about ActionScript, this book will provide you with a real foundation of knowledge from which you can build some awe-inspiring structures. You’ll learn all the important stuff you’ll need to make that giant leap toward becoming an ActionScript guru.

  What you need to know

  You’ve picked up this book, so we guess that you have the Flash basics under your belt. You’ll prob- ably have built some basic timeline-based movies with tweens and so on, and you may have read an introductory Flash book such as friends of ED’s acclaimed Foundation Flash 8. If you haven’t, we do recommend looking at it; you can find it at www.friendsofed.com.

  If you don’t have a copy of Flash 8 yet, you can download a fully functional 30-day free trial from www.macromedia.com. You can use either the Basic or Professional edition of Flash with this book, but we highly recommend going for Professional, as it features even more amazing functionality than the Basic edition! INTRODUCTION FLAs for download

  There’s a wealth of code and support files available for this book. They’re organized by chap- ter at the Foundation ActionScript for Flash 8 page at www.friendsofed.com. Look under the book option on the site’s main navigation to find it, and feel free to look around the site in general!

  The case study: Futuremedia Throughout the course of this book you’ll create a website called Futuremedia from scratch.

  You can access a fully functioning version of the website you’ll be building as you progress through this book at the URL you’ll find on the downloads page (or you can go to www.futuremedia.org.uk for the latest incarnation).

  Centering the Futuremedia site in the browser

  When you publish the Futuremedia site, you should use the following settings in the File ➤

  Publish Settings ➤ HTML tab:

  INTRODUCTION

  With these settings, you’ll see something like this in the browser (press F12 to publish the site and view it in your browser): INTRODUCTION

  That’s fine, but most professional sites center the Flash site in the browser, so it looks some- thing like this instead: There’s no direct way of achieving this in Flash—you have to edit the HTML. To do this, find the HTML file created by Flash (it will be in the same folder as the FLA), and open it in a text editor such as Notepad. You’ll see something like this:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ➥ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html ns=http://www.w3.org/1999/xhtml xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;➥ charset=iso-8859-1" /> <title>index</title> </head> <body bgcolor="#666666"> <!--urls used in the movie--> <!--text used in the movie--> <!-- futuremedia future work media people loading: this is a skip-intro free site

  • >

  INTRODUCTION <object classid="clsid:d27cdb6e-ae6d-➥

  11cf-96b8-444553540000" codebase=➥ "http://fpdownload.macromedia.com/pub/➥ shockwave/cabs/flash/swflash.cab#➥ version=8,0,0,0" width="800" height=➥ "600" id="index" align="middle">

  <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="index.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#666666" /> <embed src="index.swf" quality="high"➥ bgcolor="#666666" width="800" height=➥

  "600" name="index" align="middle"➥ allowScriptAccess="sameDomain" type=➥ "application/x-shockwave-flash"➥ pluginspage="http://www.macromedia.com/➥ go/getflashplayer" />

  </object> </body> </html>

  This is XHTML, so you should really play ball and use CSS and <div> and <span>, and no HTML tables or table horizontal and vertical centering (not least because vertical centering of a table doesn’t work in XHTML!). Add the following lines to create a CSS-based centered-in-browser page:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ➥ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html ns=http://www.w3.org/1999/xhtml xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;➥ charset=iso-8859-1" /> <title>index</title>

  <style type="text/css"> <!-- body { margin: 0px;

  background-color:#666666;

  } #centercontent { text-align: center; margin-top: -300px; margin-left: -400px; position: absolute; top: 50%; left: 50%; }

  • --> </style>

  </head> <body> <!--urls used in the movie--> <!--text used in the movie--> <!-- futuremedia future work media people loading: this is a skip-intro free site

  • >

  <div id="centercontent">

  <object classid="clsid:d27cdb6e-ae6d-➥ 11cf-96b8-444553540000" codebase=➥ "http://fpdownload.macromedia.com/pub/➥ shockwave/cabs/flash/swflash.cab#➥ version=8,0,0,0" width="800" height=➥ "600" id="index" align="middle">

  <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="index.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#666666" /> <embed src="index.swf" quality="high"➥ bgcolor="#666666" width="800" height=➥

  "600" name="index" align="middle"➥ allowScriptAccess="sameDomain" type=➥ "application/x-shockwave-flash"➥ pluginspage="http://www.macromedia.com/➥ go/getflashplayer" />

  </object>