Detailed instructions for use are in the User's Guide.
Programming ActionScript 3.0
Adobe Flex 2
® TM
© 2006 Adobe Systems Incorporated. All rights reserved. Flex 2 Programming ActionScript 3.0 If this guide is distributed with software that includes an end-user agreement, this guide, as well as the software described in it, is furnished under license and may be used or copied only in accordance with the terms of such license. Except as permitted by any such license, no part of this guide may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, recording, or otherwise, without the prior written permission of Adobe Systems Incorporated. Please note that the content in this guide is protected under copyright law even if it is not distributed with software that includes an end-user license agreement. The content of this guide is furnished for informational use only, is subject to change without notice, and should not be construed as a commitment by Adobe Systems Incorporated. Adobe Systems Incorporated assumes no responsibility or liability for any errors or inaccuracies that may appear in the informational content contained in this guide. Please remember that existing artwork or images that you may want to include in your project may be protected under copyright law. The unauthorized incorporation of such material into your new work could be a violation of the rights of the copyright owner. Please be sure to obtain any permission required from the copyright owner. Any references to company names in sample templates are for demonstration purposes only and are not intended to refer to any actual organization. Adobe, the Adobe logo, Flex, Flex Builder and Flash Player are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. ActiveX and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and other countries. Macintosh is a trademark of Apple Computer, Inc., registered in the United States and other countries. All other trademarks are the property of their respective owners. Speech compression and decompression technology licensed from Nellymoser, Inc. (www.nellymoser.com). SorensonTM SparkTM video compression and decompression technology licensed from Sorenson Media, Inc. Opera ® browser Copyright © 1995-2002 Opera Software ASA and its suppliers. All rights reserved. Adobe Systems Incorporated, 345 Park Avenue, San Jose, California 95110, USA Notice to U.S. government end users. The software and documentation are "Commercial Items," as that term is defined at 48 C.F.R. §2.101, consisting of "Commercial Computer Software" and "Commercial Computer Software Documentation," as such terms are used in 48 C.F.R. §12.212 or 48 C.F.R. §227.7202, as applicable. Consistent with 48 C.F.R. §12.212 or 48 C.F.R. §§227.7202-1 through 227.7202-4, as applicable, the Commercial Computer Software and Commercial Computer Software Documentation are being licensed to U.S. Government end users (a) only as Commercial items and (b) with only those rights as are granted to all other end users pursuant to the terms and conditions herein. Unpublished-rights reserved under the copyright laws of the United States. Adobe Systems Incorporated, 345 Park Avenue, San Jose, CA 95110-2704, USA. For U.S. Government End Users, Adobe agrees to comply with all applicable equal opportunity laws including, if appropriate, the provisions of Executive Order 11246, as amended, Section 402 of the Vietnam Era Veterans Readjustment Assistance Act of 1974 (38 USC 4212), and Section 503 of the Rehabilitation Act of 1973, as amended, and the regulations at 41 CFR Parts 60-1 through 60-60, 60-250 ,and 60-741. The affirmative action clause and regulations contained in the preceding sentence shall be incorporated by reference.
Contents
PART 1: OVERVIEW OF ACTIONSCRIPT PROGRAMMING About This Manual. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Using this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Accessing ActionScript documentation . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Developer Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Chapter 1: Introduction to ActionScript 3.0 . . . . . . . . . . . . . . . . . . 15 About ActionScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Advantages of ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 What's new in ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Compatibility with previous versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 2: Getting Started with ActionScript . . . . . . . . . . . . . . . 23 The basic ActionScript development process . . . . . . . . . . . . . . . . . . . . 23 Options for organizing your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Example: Creating a basic application . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Chapter 3: ActionScript Language and Syntax . . . . . . . . . . . . . 35 Language overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Objects and classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Packages and namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 4: Object-Oriented Programming in ActionScript . . . . 107 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
3
Advanced topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Example: GeometricShapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Chapter 5: Display Programming . . . . . . . . . . . . . . . . . . . . . . . . . 159 Understanding the display architecture . . . . . . . . . . . . . . . . . . . . . . . . . 160 Working with display objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Basics for working with the core display classes . . . . . . . . . . . . . . . . . 176 Example: SpriteArranger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
PART 2: CORE ACTIONSCRIPT 3.0 DATA TYPES AND CLASSES Chapter 6: Working with Dates and Times . . . . . . . . . . . . . . . . . 197 Managing calendar dates and times . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Controlling time intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Example: Simple analog clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Chapter 7: Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . 209 Creating strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 The length property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Working with characters in strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Obtaining string representations of other objects . . . . . . . . . . . . . . . . 213 Concatenating strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Finding substrings and patterns in strings . . . . . . . . . . . . . . . . . . . . . . . 214 Converting strings between uppercase and lowercase . . . . . . . . . . . 219 Example: ASCII Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Chapter 8: Working with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 227 Indexed arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Associative arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 Multidimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Cloning arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 Advanced Topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 Example: PlayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Chapter 9: Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Types of errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 Error handling in ActionScript 3.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258 Working with the debugger version of Flash Player . . . . . . . . . . . . . 260
4
Contents
Handling synchronous errors in an application . . . . . . . . . . . . . . . . . . .261 Creating custom error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Responding to error events and status . . . . . . . . . . . . . . . . . . . . . . . . . 267 Comparing the Error classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Example: CustomErrors application. . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Chapter 10: Using Regular Expressions . . . . . . . . . . . . . . . . . . . 285 Introduction to Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Regular expression syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Methods for using regular expressions with strings . . . . . . . . . . . . . . 303 Example: A Wiki parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Chapter 11: Working with XML . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 A quick introduction to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312 The E4X approach to XML processing . . . . . . . . . . . . . . . . . . . . . . . . . .313 XML objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 XMLList objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Initializing XML variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 Assembling and transforming XML objects . . . . . . . . . . . . . . . . . . . . . 320 Traversing XML structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Using XML namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 XML type conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 Reading external XML documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Example: Loading RSS data from the Internet . . . . . . . . . . . . . . . . . . 330
PART 3: FLASH PLAYER APIS Chapter 12: Flash Player API Overview . . . . . . . . . . . . . . . . . . . 337 flash.accessibility package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 flash.display package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 flash.errors package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 flash.events package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 flash.external package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 flash.filters package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 flash.geom package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...