Upload
trinhquynh
View
233
Download
2
Embed Size (px)
Citation preview
Object-Oriented Macromedia Flash MX
WilliAM DROL
APress Media, LLC
Object-Oriented Macromedia Flash MX Copyright 0 2002 by William Drol Originally published by Apress in 2002
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 978-1-59059-014-0 ISBN 978-1-4302-0838-9 (eBook) DOI 10.1007/978-1-4302-0838-9
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.
Technical Reviewer: Thomas Burr
Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore, Karen Watterson, John Zukowski
Managing Editor: Grace Wong
Copy Editor: Nicole I..eClerc
Compositor: Susan Glinert
Artist and Cover Designer: Kurt Krames
Indexer: Valerie Haynes Perry
Marketing Manager: Stephanie Rodriguez
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 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.
To my wife, Karen, and my cat, Suki. Thanks for all the proofreading, you two.
Contents at a Glance
Acknowledgments ................................................................................................. xiii
Introduction ......................................................................................................... xu
Part One OOP and ActionScript ....................................... 1
Chapter 1 Part One Overview ................................................................. 3
Chapter 2 Introduction to OOP ............................................................ 5
Chapter 3 Concepts for Beginning Programmers ......................... 13
Chapter 4 Flash ActionScript Programming .................................. 19
Part Two Flash OOP Basics ............................................... 45
Chapter 5 Part Two Overview and Setup ........................................... 47
Chapter 6 Planning .................................................................................. 57
Chapter 7 Encapsulation ....................................................................... 67
Chapter 8 Classes .................................................................................... 89
Chapter 9 Inheritance .......................................................................... 109
Chapter 10 Polymorphism ....................................................................... 137
Part Three Building a Dynamic Framework ................. l51
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Part Three Overview ........................................................ 153
MovieClip Class Extensions ......................................... 155
Classes for Dynamic Content ....................................... 179
Services for Dynamic Content ..................................... 207
Testing the Framework .................................................... 217
v
Contents at a Glance
Part Four Chapter 16
Chapter 17
Chapter 18
Chapter 19
Chapter 20
Part Five Chapter 21
Chapter 22
Chapter 23
Chapter 24
Chapter 25
Chapter 26
Part Six Appendix A
Appendix 8
Appendix C
Appendix D
Extending the Dynamic Framework .......... 239
Part Four Overview ........................................................... 241
Additional MovieClip Class Extensions .................. 243
The Menuitem Class ........................................................... 255
The Menu Class .................................................................... 275
The Menu Bar Class ............................................................. 311
XML in Flash ...................................................... 321
Part Five Overview ........................................................... 323
Introduction to XML ......................................................... 327
Using XML in Flash ........................................................... 337
XML Class Extensions ....................................................... 353
Finishing the XML Menu Service ................................. 367
Reusing the XML Menu Service ..................................... 379
Appendixes ........................................................... 387
New Features Overview .................................................... 389
Interacting with a Web Server .................................. .409
Movie Clip Events ............................................................. 429
Reference ............................................................................... 433
Index ...................................................................................................................... 471
vi
Contents
Acknowledgments ........................................................................................... xiii
Introduction .................................................................................................... xu
This Is Not Just Another Flash Book ....................................................... xu
The Purpose of This Book ............................................................................... xu
How to Use This Book ...................................................................................... xui
Basic Requirements .......................................................................................... xui
What J s New? ......................................................................................................... xuii
Part One OOP and Action Script ....................................... 1
Chapter 1 Part One Overview ........................................................... 3
Chapter 2 ................................................................................................................. 3
Chapter 3 ................................................................................................................. 3
Chapter 4 ................................................................................................................. 4
Chapter 2 Introduction to OOP ...................................................... s
The Scoop on OOP .................................................................................................. s Understanding Basic OOP Concepts ............................................................... 7
What)s Next? ........................................................................................................ 12
Chapter 3 Concepts for Beginning Programmers ............. 13
About Programming Slang ................................................................................ 13
The Building Blocks of Programming ........................................................ 14
OOP Concepts ........................................................................................................ 17
What)s Next? ........................................................................................................ 18
Chapter 4 Flash ActionScript Programming ........................ 19
About Comments .................................................................................................... 19
About Style ........................................................................................................... 20
The Building Blocks of Programming ........................................................ 24
OOP Concepts ........................................................................................................ 36
vii
Contents
Variations on ActionScript Syntax ........................................................... 39
Flash ActionScript Trouble Spots ............................................................ .41 What's Next? ......................................................................................................... 44
Part Two Flash OOP Basics ............................................... 45
Chapter 5 Part Two Overview and Setup ............................... .47
Overview .................................................................................................................. 47 Checkpoint .............................................................................................................. 48
New Project Setup .................... ., ........................................................................ 51 What's Next .......... , ................................................................................................ 55
Chapter 6 Planning .................. ., ............................................................. 57
The Importance of Planning ........................................................................... 57 Planning Flash Encapsulation ...................................................................... 59
Planning Flash Inheritance ........................................................................... 62 What's Next? ......................................................................................................... 65
Chapter 7 Encapsulation .................................................................... 67
Building the Parts ............................................................................................ 67
Writing the Code ................................................................................................ 75 Improving the Code ............................................................................................ 81 Encapsulation Summary ...................................................................................... 86 What's Next? ......................................................................................................... 87
Chapter 8 Classes .................................................................................... 89
Classes vs. Prototypes ................................................................................... 89
Implementing a Class ........................................................................................ 96 Including External Code ............................................................................... 101 Publishing the Document ............................................................................... 105
What's Next? ....................................................................................................... 107
Chapter 9 Inheritance ........................................................................ 109
About Class Hierarchy .................................................................................... 109 A Quick Inheritance Test ............................................................................. 110 The Bouncer Class ............................................................................................ 113 The Gravity Class ............................................................................................ 127 Publishing the Document ............................................................................... 133
viii
Contents
Inheritance Summary ....................................................................................... 134 What's Next? ...................................................................................................... 135
Chapter 10 Polymorphism .................................................................... 137
Editing the Parts ............................................................................................ 137 Publishing the Document .............................................................................. 147 Polymorphism Summary ..................................................................................... 148 What's Next? ...................................................................................................... 150
Part Three Building a Dynamic Framework ................. 151
Chapter 11 Part Three Overview .................................................. 153
Chapter 12 ........................................................................................................... 153 Chapter 13 ........................................................................................................... 153 Chapter 14 ........................................................................................................... 153 Chapter 15 ........................................................................................................... 153
Chapter 12 MovieClip Class Extensions ................................ 155
Why MovieClip Extensions? .......................................................................... 156 Creating a Test Template ............................................................................ 156 Creating a Test Document ............................................................................ 163 Writing the Extension Functions ............................................................. 163 Testing the Extension Functions ............................................................. 173
Exporting the Extension Functions ......................................................... 176 What's Next? ...................................................................................................... 177
Chapter 13 Classes for Dynamic Content .............................. 179
Flash Document Levels ................................................................................... 179 The MovieManager Class ................................................................................. 180 The ClipDragger Class ................................................................................... 185 Class Updates .................................................................................................... 191 What's Next? ...................................................................................................... 206
Chapter 14 Services for Dynamic Content ........................... 207
Creating a Service Template ...................................................................... 208 Building the Service Manager .................................................................... 210 Testing the Service Manager ...................................................................... 212 What's Next? ...................................................................................................... 215
ix
Contents
Chapter 15 Testing the Framework .............................................. 217
Building a Dynamic Project ......................................................................... 217 Publishing the Document ............................................................................... 236 What}s Next? ....................................................................................................... 237
Part Four Extending the Dynamic Framework .......... 239
Chapter 16 Part Four Overview ..................................................... 241
Chapter 17 ............................................................................................................ 241 Chapter 18 ............................................................................................................ 241 Chapter 19 ............................................................................................................ 241 Chapter 20 ............................................................................................................ 242
Chapter 17 Additional MovieClip Class Extensions ............................................................. 243
Adding the New Extensions ........................................................................... 243 Testing the New Extensions ......................................................................... 247 Updating the ServiceManager ...................................................................... 249 About the New Extensions ............................................................................. 249 What}s Next ......................................................................................................... 253
Chapter 18 The Menuitem Class ..................................................... 255
Building the Menu Service ........................................................................... 255 Introducing Dynamic Class Registration .............................................. 258 The Menuitem Class .......................................................................................... 262 What}s Next ......................................................................................................... 274
Chapter 19 The Menu Class ................................................................ 275
Writing the Public Functions .................................................................... 275 Writing the Private Functions .................................................................. 287 Menu Class Documentation ............................................................................. 307 What} s Next ......................................................................................................... 309
Chapter 20 The MenuBar Class ........................................................ 311
Inheriting the Menu Class ........................................................................... 311
Overriding the Menu Class ........................................................................... 312
X
Contents
Testing the MenuBar Class .......................................................................... 314
MenuBar Class Documentation ...................................................................... 319
What's Next ......................................................................................................... 320
Part Five XML in Flash ...................................................... 321
Chapter 21 Part Five Overview ..................................................... 323
Chapter 22 ........................................................................................................... 323
Chapter 23 ........................................................................................................... 323
Chapter 24 ........................................................................................................... 323
Chapter 25 ........................................................................................................... 323
Chapter 26 ........................................................................................................... 323
About XML and the Menu Service ............................................................... 324
Chapter 22 Introduction to XML .................................................. 327
What Is XML? ...................................................................................................... 327
What Are the Rules? ....................................................................................... 327
What's Next ......................................................................................................... 335
Chapter 23 Using XML in Flash ..................................................... 337
Testing the XML Class ................................................................................... 337
Checking XML in Flash ................................................................................... 349
What's Next ......................................................................................................... 352
Chapter 24 XML Class Extensions ................................................ 353
Processing XML Document Nodes ................................................................. 353
Updating the ServiceManager ...................................................................... 363
Documenting the XML Class Extensions .................................................. 364
What's Next ......................................................................................................... 365
Chapter 25 Finishing the XML Menu Service ...................... 367
Creating the XML File ................................................................................... 367
Updating the Menu Service .......................................................................... 368
Testing the Menu Service ............................................................................ 375
Publishing the Document .............................................................................. 376
The Icing on the Cake ................................................................................... 377
What's Next ......................................................................................................... 378
xi
Contents
Chapter 26 Reusing the XML Menu Service ............................ 379
Building a Dynamic Menu Template ........................................................... 379
Demonstrating Reuse ........................................................................................ 380
What's Next? ....................................................................................................... 384
Part Six Appendixes ................................................. 387
Appendix A New Features Overview .............................................. 389
ActionScript LoadVars Class ...................................................................... 390
ActionScript switch Keyword ............................................... : ...................... 390
Code Hints ............................................................................................................ 391
Creating Empty MovieClips ........................................................................... 394
Dynamic Image Support .................................................................................... 395
Digital Video Support .................................................................................... 395
Interval Callback Functions ...................................................................... 398
MovieClip Events .............................................................................................. 399
Shared Objects ................................................................................................... 399
System Capabilities ........................................................................................ 405
Appendix 8 Interacting with a Web Server ......................... 409
Installing Microsoft !IS ............................................................................. 409
Web Server Communications ........................................................................... 418
Appendix C Movie Clip Events ........................................................ 429
Find the Event Handler ................................................................................. 430
Appendix D Reference ............................................................................. 433
Documentation ..................................................................................................... 433
Code Listings ..................................................................................................... 450
Index .................................................................................................................... 471
xii
Acknowledgments
MY SINCERE THANKS to everyone at Apress for making this a thoroughly pleasant
experience. To Grace Wong for support, advice, and management; Nicole LeClerc
for endless, tedious copy editing; Stephanie Rodriguez for marketing support; Dan
Appleman and Gary Cornell for inviting me to write this book in the first place; and
everyone else at Apress who made this book possible, many of whom I am no
doubt unaware.
Thanks to Thomas Burr, who agreed to tackle the technical review of this book.
Thank you, Thomas, for spotting code problems or difficult passages, suffering
code revisions, and offering much-needed advice for streamlining the first drafts.
Thanks to Mom, Dad, Karen Wright, and Ken To risky for proofreading and
support; Jeff and Laura Janosek for declaring this the funniest book ever (huh?);
Jim, Janet, and Rudy Sears for directing security; Suki for walking on the keyboard
whenever I took a break; and Jeff Wootton for being the nicest guy you'll ever meet
and refusing to be part of this.
xiii
Introduction
WELCOME TO Object-Oriented Macro media Flash MX. This may be the only
Macromedia Flash book entirely devoted to object-oriented programming (OOP),
especially for programmers of all skill levels. Even the best ActionScript books
typically contain but a few slim OOP chapters. I want to change that right now.
This Is Not Just Another Flash Book
There are numerous Flash books, so why another? Many Flash books regard pro
gramming as a nuisance or an afterthought, and tend to focus merely on the syntax
of the language (what you must type exactly to avoid errors). The harder skills, such
as writing well-crafted code or recognizing the benefits of planning and documen
tation, are usually absent.
On the other hand, there are excellentActionScript reference books available now.
Their primary fimction, however, is thorough language documentation, not OOP.
My goal is to bridge the gaps I have found in other books. I will build a series of
related Flash projects using ActionScript and OOP. You will learn the value of good
planning and good coding, and you will see how applications grow from building
blocks of reusable code. This is a hands-on book, so follow along and enjoy a cola
or two.
The Purpose of This Book
If you want to do more than load and publish the prebuilt Flash .MX templates, if
you want to make sure that your work is reusable, if you need to understand a
process for development, and if you want to learn solid programming techniques,
this book is for you.
This book is not a tutorial for the Flash authoring environment, nor is it an
exhaustive ActionScript reference. Please read your product documentation to under
stand the authoring environment; make sure you're comfortable with the basics.
XV
Introduction
xvi
How to Use This Book
Please read Parts One through Five in order (later material depends on earlier material). Appendixes A through C, however, contain independent topics to read at any time (especially if you're an experienced Flash user and curious about new :MX features).
• For new programmers. If you're a new programmer, welcome! I believe OOP is a very good place for beginners to start-just read carefully. Allow enough practice with Parts One and Two before moving on.
• For programmers with some experience. Chapters 1 through 3 contain introductory material, but you may want to skim through them anyway (don't miss Chapter 2 if you're unfamiliar with OOP).
• For programmers with OOP experience. You can skip Chapters 1 through 3. If you're unfamiliar with Flash ActionScript, however, please read Chapter 4.
Basic Requirements
This is a hands-on book: You need software and experience to follow along. Please review the following checklist.
• Basic familiarity with Flash 5 or MX recommended. This is a code-centric book, but you should also have a general familiarity with Flash. If you're comfortable with Flash 5 or :MX, you'll do fine. If not, please study your product documentation; make sure you understand the basics.
• Programming experience not required. Previous programming experience is welcome, but it's not a strict requirement. This book respects new programmers too.
• Flash MX required. I mean the full Flash :MX authoring environment used to create applications. This isn't the same thing as the browser plug-in that many Web sites prompt you to download.
TIP Visit Macromedia's Web site (http: I !'vMII. macromedia. com) if you do not own Flash MX. You should be able to download a free trial version for limited use.
What's New?
In a word: everything. The obvious change in Flash MX is its new interface, but it's
new under the hood too. There's new ActionScript for better OOP and event handling, and there's more support for dynamic content, images, and video. Appendix A
surveys the major new Flash MX features, but here's a short introductory list:
• Updated authoring environment with auto docking panels and a friendlier interface. Pop-up code hints are available while you type.
• New ActionScript to support inheritance and consistent event handling.
• New user interface components (smarter Smart Clips).
• More support for dynamic content and images.
• Digital video support.
Introduction
xvii