Tài liệu học lập trình flash với as2
LEARNING ACTIONSCRIPT ™ 2.0 IN FLASH ® © 2007 Adobe Systems Incorporated. All rights reserved. Learning ActionScript 2.0 in Adobe® Flash® 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®, Flash®, FlashHelp®, Flash® Player, JRun™, Macromedia® and Shockwave® are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries. Macintosh® is a trademark of Apple Computer, Inc., registered in the United States and other countries. Windows® is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their respective owners. Portions of this product contain code licensed from Nellymoser. (www.nellymoser.com). Sorenson Spark ™ video compression and decompression technology licensed from Sorenson Media, Inc. Flash CS3 video is powered by On2 TrueMotion video technology. © 1992-2005 On2 Technologies, Inc. All Rights Reserved. http://www.on2.com. 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. 3 Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Intended audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 System requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Updating Flash XML files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 About the documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Additional resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Chapter 1: What’s New in Flash ActionScript . . . . . . . . . . . . . . . . 19 New in ActionScript 2.0 and Flash Player 9.x . . . . . . . . . . . . . . . . . . . . . 19 New in ActionScript 2.0 and Flash Player 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Changes to security model for locally installed SWF files. . . . . . . . . . 28 Chapter 2: About ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 What is ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 About choosing between ActionScript 1.0 and ActionScript 2.0 . . . 33 Understanding ActionScript and Flash Player . . . . . . . . . . . . . . . . . . . 34 Chapter 3: Data and Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . 35 About data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 About data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 About variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Organizing data in objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 About casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 Chapter 4: Syntax and Language Fundamentals . . . . . . . . . . . . . 77 About syntax, statements, and expressions . . . . . . . . . . . . . . . . . . . . . . .78 About dot syntax and target paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 About language punctuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 About constants and keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 About statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 About arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 About operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 4Contents Chapter 5: Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . 163 About functions and methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Understanding methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Chapter 6: Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 About object-oriented programming and Flash . . . . . . . . . . . . . . . . . . 188 Writing custom class files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 About working with custom classes in an application . . . . . . . . . . . . . 199 Example: Writing custom classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223 Example: Using custom class files in Flash . . . . . . . . . . . . . . . . . . . . . .236 Assigning a class to symbols in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Compiling and exporting classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Understanding classes and scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 About top-level and built-in classes . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 About working with built-in classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 Chapter 7: Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 About inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 About writing subclasses in Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265 Using polymorphism in an application . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Chapter 8: Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 About interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Creating interfaces as data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Understanding inheritance and interfaces . . . . . . . . . . . . . . . . . . . . . . .282 Example: Using interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 Example: Creating a complex interface . . . . . . . . . . . . . . . . . . . . . . . . .285 Chapter 9: Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 About ActionScript and events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 Using event handler methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Using event listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Using event listeners with components . . . . . . . . . . . . . . . . . . . . . . . . .298 Using button and movie clip event handlers . . . . . . . . . . . . . . . . . . . . 300 Broadcasting events from component instances. . . . . . . . . . . . . . . . 305 Creating movie clips with button states . . . . . . . . . . . . . . . . . . . . . . . . 305 Event handler scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 Scope of the this keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Using the Delegate class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Contents 5 Chapter 10: Working with Movie Clips . . . . . . . . . . . . . . . . . . . . . 313 About controlling movie clips with ActionScript . . . . . . . . . . . . . . . . . .314 Calling multiple methods on a single movie clip . . . . . . . . . . . . . . . . . . 316 Loading and unloading SWF files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Changing movie clip position and appearance . . . . . . . . . . . . . . . . . . .319 Dragging movie clips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 Creating movie clips at runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .321 Adding parameters to dynamically created movie clips. . . . . . . . . . . 325 Managing movie clip depths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 About caching and scrolling movie clips with ActionScript . . . . . . . 330 Using movie clips as masks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Handling movie clip events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Assigning a class to a movie clip symbol. . . . . . . . . . . . . . . . . . . . . . . . 339 Initializing class properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Chapter 11: Working with Text and Strings . . . . . . . . . . . . . . . . 343 About text fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 About loading text and variables into text fields . . . . . . . . . . . . . . . . . 353 Using fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 About font rendering and anti-alias text . . . . . . . . . . . . . . . . . . . . . . . . 367 About text layout and formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Formatting text with Cascading Style Sheet styles . . . . . . . . . . . . . . 382 Using HTML-formatted text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Example: Creating scrolling text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .410 About strings and the String class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Chapter 12: Animation, Filters, and Drawings . . . . . . . . . . . . . . 429 Scripting animation with ActionScript 2.0 . . . . . . . . . . . . . . . . . . . . . . 430 About bitmap caching, scrolling, and performance . . . . . . . . . . . . . 440 About the Tween and TransitionManager classes . . . . . . . . . . . . . . .441 Using filter effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456 Working with filters using ActionScript . . . . . . . . . . . . . . . . . . . . . . . . 463 Manipulating filter effects with code . . . . . . . . . . . . . . . . . . . . . . . . . . . 489 Creating bitmaps with the BitmapData class . . . . . . . . . . . . . . . . . . . . 493 About blending modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 About operation order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498 Drawing with ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 Understanding scaling and slice guides . . . . . . . . . . . . . . . . . . . . . . . . .514 6Contents Chapter 13: Creating Interaction with ActionScript . . . . . . . . . . 519 About events and interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520 Controlling SWF file playback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520 Creating interactivity and visual effects . . . . . . . . . . . . . . . . . . . . . . . . 524 Creating runtime data bindings using ActionScript . . . . . . . . . . . . . . .537 Deconstructing a sample script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .546 Chapter 14: Working with Images, Sound, and Video . . . . . . . 549 About loading and working with external media . . . . . . . . . . . . . . . . 550 Loading external SWF and image files. . . . . . . . . . . . . . . . . . . . . . . . . . 551 About loading and using external MP3 files . . . . . . . . . . . . . . . . . . . . 555 Assigning linkage to assets in the library . . . . . . . . . . . . . . . . . . . . . . . 559 About using FLV video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 About creating progress animations for media files. . . . . . . . . . . . . . 580 Chapter 15: Working with External Data . . . . . . . . . . . . . . . . . . 589 Sending and loading variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 Using HTTP to connect to server-side scripts . . . . . . . . . . . . . . . . . . 594 About file uploading and downloading . . . . . . . . . . . . . . . . . . . . . . . . . 600 About XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608 Sending messages to and from Flash Player . . . . . . . . . . . . . . . . . . . . 617 About the External API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621 Chapter 16: Understanding Security . . . . . . . . . . . . . . . . . . . . . . 631 About compatibility with previous Flash Player security models . . . 631 About local file security and Flash Player. . . . . . . . . . . . . . . . . . . . . . . .633 Restricting networking APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .648 About domains, cross-domain security, and SWF files . . . . . . . . . . 650 Server-side policy files for permitting access to data . . . . . . . . . . . . .657 HTTP to HTTPS protocol access between SWF files . . . . . . . . . . . .662 Chapter 17: Best Practices and Coding Conventions for ActionScript 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665 Naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .666 Using comments in your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 ActionScript coding conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 ActionScript and Flash Player optimization . . . . . . . . . . . . . . . . . . . . 695 Formatting ActionScript syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .697 Contents 7 Appendix A: Error Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 Appendix B: Deprecated Flash 4 operators . . . . . . . . . . . . . . . . 713 Appendix C: Keyboard Keys and Key Code Values . . . . . . . . . . 715 Appendix D: Writing Scripts for Earlier Versions of Flash Player . . 721 About targeting earlier versions of Flash Player . . . . . . . . . . . . . . . . . . 721 Using Flash to create content for Flash Player 4 . . . . . . . . . . . . . . . . 722 Appendix E: Object-Oriented Programming with ActionScript 1.0. 725 About ActionScript 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 Creating a custom object in ActionScript 1.0 . . . . . . . . . . . . . . . . . . . . 728 Assigning methods to a custom object in ActionScript 1.0. . . . . . . . 729 Defining event handler methods in ActionScript 1.0 . . . . . . . . . . . . . 730 Creating inheritance in ActionScript 1.0 . . . . . . . . . . . . . . . . . . . . . . . . 732 Adding getter/setter properties to objects in ActionScript 1.0 . . . . . 734 Using Function object properties in ActionScript 1.0 . . . . . . . . . . . . . 735 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .737 8Contents 9 Introduction Adobe Flash CS3 Professional is the professional standard authoring tool for producing high- impact web experiences. ActionScript is the language you use to add interactivity to Flash applications, whether your applications are simple animated SWF files or more complex rich Internet applications. You don’t have to use ActionScript to use Flash, but if you want to provide basic or complex user interactivity, work with objects other than those built into Flash (such as buttons and movie clips), or otherwise turn a SWF file into a more robust user experience, you’ll probably want to use ActionScript. For more information, see the following topics: Intended audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Updating Flash XML files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 System requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 About the documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Additional resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Intended audience This manual assumes that you have already installed Flash and know how to use the user interface.You should know how to place objects on the Stage and manipulate them in the Flash authoring environment. If you have used a scripting language before, ActionScript will seem familiar. But if you’re new to programming, ActionScript basics are easy to learn. You can start with simple commands and build more complexity as you progress. You can add a lot of interactivity to your files without having to learn (or write) a lot of code. 10 Introduction System requirements ActionScript 2.0 does not have any system requirements in addition to Flash. Flash CS3 Professional introduces ActionScript 3.0. Flash Player 9 and ActionScript 3.0 are the default publishing settings for Flash. This manual provides information on how to use ActionScript 2.0 with Flash. You must change the publishing settings for your Flash files to Flash Player 9 and ActionScript 2.0. If you do not change the default settings, explanations and code samples in the documentation might not work correctly. If you develop applications for earlier versions of Flash Player, see Appendix D, “Writing Scripts for Earlier Versions of Flash Player,” on page 721. Updating Flash XML files It is important that you always have the latest Flash XML files installed. Adobe sometimes introduces features in dot releases (minor releases) of Flash Player. When such a release is available, you should update your version of Flash to get the latest XML files. Otherwise, the Flash compiler might generate errors if you use new properties or methods that were unavailable in the version of Flash Player that came with your Flash installation. For example, Flash Player 7 (7.0.19.0) contained a new method for the System object, System.security.loadPolicyFile . To access this method, you must use the Player Updater installer to update all the Flash players that are installed with Flash. Otherwise, the Flash compiler displays errors. Remember that you can install a Player Updater that is one or more major versions ahead of your version of Flash. By doing this, you will get the XML files that you need but shouldn’t have any compiler errors when you publish to older versions of Flash Player. Sometimes new methods or properties are available to older versions, and having the latest XML files minimizes the compiler errors you get when you try to access older methods or properties. . of Flash Player . . 721 About targeting earlier versions of Flash Player . . . . . . . . . . . . . . . . . . 721 Using Flash to create content for Flash. addition to Flash. Flash CS3 Professional introduces ActionScript 3.0. Flash Player 9 and ActionScript 3.0 are the default publishing settings for Flash. This