Adobe ActionScript 3.0 Developer’s Guide Action Script As3 Dev

User Manual: adobe ActionScript - 3.0 - Developer’s Guide Free User Guide for Adobe Flash Software, Manual

Open the PDF directly: View PDF PDF.
Page Count: 1110

DownloadAdobe ActionScript 3.0 Developer’s Guide Action Script - As3 Dev
Open PDF In BrowserView PDF
ACTIONSCRIPT® 3.0
Developer’s Guide

© 2011 Adobe Systems Incorporated and its licensors. All rights reserved.
Copyright

ActionScript® 3.0 Developer’s Guide
This guide is protected under copyright law, 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.
This guide is licensed for use under the terms of the Creative Commons Attribution Non-Commercial 3.0 License. This License allows users to copy, distribute,
and transmit the guide for noncommercial purposes only so long as (1) proper attribution to Adobe is given as the owner of the guide; and (2) any reuse or
distribution of the guide contains a notice that use of the guide is governed by these terms. The best way to provide notice is to include the following link. To
view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
Adobe, the Adobe logo, Acrobat, Acrobat Capture, Acrobat Connect, Acrobat Messenger, Acrobat 3D Capture, ActionScript, ActiveTest, Adobe ActionSource,
Adobe AIR, Adobe AIR logo, Adobe Audition, Adobe Caslon, Adobe Connect, Adobe DataWarehouse, Adobe Dimensions, Adobe Discover, Adobe Financial
Services, Adobe Garamond, Adobe Genesis, Adobe Griffo, Adobe Jenson, Adobe Kis, Adobe OnLocation, Adobe Originals logo, Adobe PDF logo, Adobe
Premiere, AdobePS, Adobe SiteSearch, Adobe Type Manager, Adobe Wave, Adobe Wave logo , Adobe WebType, Adobe Wood Type, After Effects, AIR , Alexa,
Andreas, Arno, ATM, Authorware, Balzano, Banshee, Benson Scripts, Better by Adobe. , Bickham Script, Birch, Blackoak, Blue Island, Brioso, BusinessCatalyst,
Buzzword, Caflisch Script, Cairngorm, Calcite, Caliban, Captivate, Carta, Chaparral, Charlemagne, Cheq, Classroom in a Book, ClickMap, Co-Author,
ColdFusion, ColdFusion Builder, Conga Brava, Contribute, Copal, Coriander, Cottonwood, Creative Suite, Critter, Cronos, CS Live, Custom Insight,
CustomerFirst, Cutout, Digital Pulse, Director, Distiller, DNG logo, Dreamweaver, DV Rack, Encore, Engaging beyond the Enterprise, ePaper, Ex Ponto,
Fireworks, Flash, Flash logo, Flash Access, Flash Access logo, Flash Builder, Flash Cast , FlashCast, Flash Catalyst, FlashHelp, Flash Lite, Flash on., FlashPaper,
Flash Platform Services logo , Flex, Flex Builder, Flood, Font Folio, Frame , FrameCenter, FrameConnections, FrameMaker, FrameManager, FrameViewer,
FreeHand, Fusaka, Galahad, Giddyup, Giddyup Thangs, GoLive, GoodBarry, Graphite, HomeSite, HBX, HTML Help Studio, HTTP Dynamic Streaming logo ,
Hypatia, Illustrator, ImageReady, Immi 505, InCopy, InDesign, Ironwood, Jimbo, JRun, Juniper, Kazuraki, Kepler, Kinesis, Kozuka Gothic, Kozuka Mincho,
Kuler, Leander Script, Lens Profile Creator logo , Lightroom, Lithos, LiveCycle, Macromedia, Madrone, Mercado, Mesquite, Mezz, Minion, Mojo, Montara,
Moonglow, MXML, Myriad, Mythos, Nueva, Nyx, 1-Step RoboPDF, Omniture, Open Screen Project, Open Source Media Framework logo, OpenType logo,
Ouch!, Ovation, PageMaker, PageMaker Portfolio, PDF JobReady, Penumbra, Pepperwood, Photoshop, Photoshop logo, Pixel Bender, Poetica, Ponderosa,
Poplar, Postino, PostScript, PostScript logo, PostScript 3, PostScript 3i, Powered by XMP, Prana, PSPrinter, Quake, Rad, Reader, Real-Time Analytics, Reliq,
RoboEngine, RoboHelp, RoboHTML, RoboLinker, RoboPDF, RoboScreenCapture, RoboSource Control, Rosewood, Roundtrip HTML, Ryo, Sanvito, Sava,
Scene7, See What’s Possible , Script Teaser, Shockwave, Shockwave Player logo, Shuriken Boy, Silentium, Silicon Slopes, SiteCatalyst, SiteCatalyst NetAverages,
Software Video Camera, Sonata, Soundbooth, SoundEdit, Strumpf, Studz, Tekton, Test&Target, 360Code, Toolbox, Trajan, TrueEdge, Type Reunion, Ultra,
Utopia, Vector Keying, Version Cue, VirtualTrak, Visual Call, Visual Communicator, Visual Sciences, Visual Sensor, Visual Server, Viva, Voluta , Warnock,
Waters Titling, Wave , Willow, XMP logo, Zebrawood are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or
other countries.
Android is a trademark of Google Inc. 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 Inc., registered in the United States and other countries. Java is a trademark or registered trademark of Sun
Microsystems, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Updated Information/Additional Third Party Code Information available at http://www.adobe.com/go/thirdparty.
Portions include software under the following terms:
This product includes software developed by the Apache Software Foundation (http://www.apache.org/).
MPEG Layer-3 audio compression technology licensed by Fraunhofer IIS and Thomson Multimedia (http://www.mp3licensing.com).
This software is based in part on the work of the Independent JPEG Group.
Speech compression and decompression technology licensed from Nellymoser, Inc. (www.nellymoser.com).
Video in Flash Player is powered by On2 TrueMotion video technology. © 1992-2005 On2 Technologies, Inc. All Rights Reserved. http://www.on2.com.
This product contains either BSAFE and/or TIPEM software by RSA Security, Inc.

Sorenson Spark™ video compression and decompression technology licensed from Sorenson Media, Inc.
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 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.

Last updated 3/21/2011

iii

Contents
Chapter 1: Working with dates and times
Managing calendar dates and times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Controlling time intervals

.............................................................................................. 4

Date and time example: Simple analog clock

........................................................................... 6

Chapter 2: Working with strings
Basics of strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Creating strings

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

The length property

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Working with characters in strings
Comparing strings

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Obtaining string representations of other objects
Concatenating strings

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Finding substrings and patterns in strings

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Converting strings between uppercase and lowercase
Strings example: ASCII art

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Chapter 3: Working with arrays
Basics of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Indexed arrays

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Associative arrays

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Multidimensional arrays
Cloning arrays

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Extending the Array class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Arrays example: PlayList

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Chapter 4: Handling errors
Basics of error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Types of errors

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

Error handling in ActionScript 3.0

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

Working with the debugger versions of Flash runtimes
Handling synchronous errors in an application
Creating custom error classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

Responding to error events and status
Comparing the Error classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

Handling errors example: CustomErrors application

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

Chapter 5: Using regular expressions
Basics of regular expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Regular expression syntax

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

Methods for using regular expressions with strings
Regular expressions example: A Wiki parser

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Last updated 3/21/2011

iv

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 6: Working with XML
Basics of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The E4X approach to XML processing
XML objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

XMLList objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Initializing XML variables

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Assembling and transforming XML objects
Traversing XML structures
Using XML namespaces
XML type conversion

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Reading external XML documents

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

XML in ActionScript example: Loading RSS data from the Internet

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Chapter 7: Handling events
Basics of handling events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
How ActionScript 3.0 event handling differs from earlier versions
The event flow

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

Event objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

Event listeners

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Event handling example: Alarm Clock

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Chapter 8: Working with application domains
Chapter 9: Display programming
Basics of display programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Core display classes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

Advantages of the display list approach

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Working with display objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Manipulating display objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Animating objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Stage orientation

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

Loading display content dynamically
Display object example: SpriteArranger

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Chapter 10: Working with geometry
Basics of geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Using Point objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Using Rectangle objects
Using Matrix objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Geometry example: Applying a matrix transformation to a display object

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

Chapter 11: Using the drawing API
Basics of the drawing API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
The Graphics class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

Drawing lines and curves

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

Drawing shapes using built-in methods
Creating gradient lines and fills

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

Using the Math class with drawing methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

Last updated 3/21/2011

v

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Animating with the drawing API

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

Drawing API example: Algorithmic Visual Generator
Advanced use of the drawing API

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Chapter 12: Working with bitmaps
Basics of working with bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
The Bitmap and BitmapData classes
Manipulating pixels
Copying bitmap data

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

Making textures with noise functions
Scrolling bitmaps

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

Taking advantage of mipmapping

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

Bitmap example: Animated spinning moon

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

Asynchronous decoding of bitmap images

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

Chapter 13: Filtering display objects
Basics of filtering display objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Creating and applying filters
Available display filters

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

Filtering display objects example: Filter Workbench

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

Chapter 14: Working with Pixel Bender shaders
Basics of Pixel Bender shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Loading or embedding a shader
Accessing shader metadata

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

Specifying shader input and parameter values
Using a shader

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Chapter 15: Working with movie clips
Basics of movie clips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Working with MovieClip objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Controlling movie clip playback

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Creating MovieClip objects with ActionScript
Loading an external SWF file

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

Movie clip example: RuntimeAssetsExplorer

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

Chapter 16: Working with inverse kinematics
Basics of Inverse Kinematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Animating IK Armatures Overview

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Getting information about an IK armature

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

Instantiating an IK Mover and Limiting Its Movement
Moving an IK Armature
Using Springs
Using IK Events

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

Chapter 17: Working in three dimensions (3D)
Basics of 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Understanding the 3D features of Flash Player and the AIR runtime

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

Last updated 3/21/2011

vi

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Creating and moving 3D objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

Projecting 3D objects onto a 2D view
Example: Perspective projection

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

Performing complex 3D transformations
Using triangles for 3D effects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

Chapter 18: Basics of Working with text
Chapter 19: Using the TextField class
Displaying text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Selecting and manipulating text

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

Capturing text input

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356

Restricting text input

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357

Formatting text

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

Advanced text rendering

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Working with static text

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

TextField Example: Newspaper-style text formatting

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

Chapter 20: Using the Flash Text Engine
Creating and displaying text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Handling Events in FTE
Formatting text

Working with fonts
Controlling text

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

Flash Text Engine example: News layout

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

Chapter 21: Using the Text Layout Framework
Overview of the Text Layout Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Using the Text Layout Framework

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405

Structuring text with TLF

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

Formatting text with TLF

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414

Importing and exporting text with TLF
Managing text containers with TLF

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415

Enabling text selection, editing, and undo with TLF
Event handling with TLF

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417

Positioning images within text

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417

Chapter 22: Working with sound
Basics of working with sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Understanding the sound architecture
Loading external sound files

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421

Working with embedded sounds

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

Working with streaming sound files

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

Working with dynamically generated audio
Playing sounds

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428

Security considerations when loading and playing sounds
Controlling sound volume and panning
Working with sound metadata

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434

Last updated 3/21/2011

vii

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Accessing raw sound data
Capturing sound input

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

Sound example: Podcast Player

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

Chapter 23: Working with video
Basics of video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Understanding video formats

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

Understanding the Video class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456

Loading video files

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

Controlling video playback

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

Playing video in full-screen mode
Streaming video files

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

Understanding cue points

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

Writing callback methods for metadata and cue points

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

Using cue points and metadata

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

Advanced topics for video files

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

Video example: Video Jukebox

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481

Using the StageVideo class for hardware-accelerated rendering

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

Chapter 24: Working with cameras
Understanding the Camera class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Displaying camera content on screen
Designing your camera application
Connecting to a user’s camera

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497

Verifying that cameras are installed

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497

Detecting permissions for camera access
Maximizing camera video quality
Monitoring camera status

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

Chapter 25: Using digital rights management
Understanding the protected content workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
DRM-related members and events of the NetStream class
Using the DRMStatusEvent class

Using the DRMAuthenticateEvent class
Using the DRMErrorEvent class
Using the DRMManager class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

Using the DRMContentData class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516

Updating Flash Player to support Flash Access

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

Chapter 26: Adding PDF content in AIR
Detecting PDF Capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Loading PDF content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Scripting PDF content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

Known limitations for PDF content in AIR

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

Last updated 3/21/2011

viii

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 27: Basics of user interaction
Capturing user input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Managing focus

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524

Discovering input types

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

Chapter 28: Keyboard input
Capturing keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Using the IME class
Virtual keyboards

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534

Chapter 29: Mouse input
Capturing mouse input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Mouse input example: WordSearch

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544

Chapter 30: Touch, multitouch and gesture input
Basics of touch input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Touch support discovery
Touch event handling
Touch and drag

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

Gesture event handling
Troubleshooting

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560

Chapter 31: Copy and paste
Basics of copy-and-paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Reading from and writing to the system clipboard
HTML copy and paste in AIR
Clipboard data formats

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566

Chapter 32: Accelerometer input
Checking accelerometer support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Detecting accelerometer changes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572

Chapter 33: Drag and drop in AIR
Basics of drag and drop in AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Supporting the drag-out gesture
Supporting the drag-in gesture
Drag and drop in HTML

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

Dragging data out of an HTML element
Dragging data into an HTML element

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586

Example: Overriding the default HTML drag-in behavior

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587

Handling file drops in non-application HTML sandboxes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589

Dropping file promises

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590

Chapter 34: Working with menus
Menu basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Creating native menus (AIR)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606

About context menus in HTML (AIR)
Displaying pop-up native menus (AIR)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609

Last updated 3/21/2011

ix

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Handling menu events

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610

Native menu example: Window and application menu (AIR)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611

Chapter 35: Taskbar icons in AIR
About taskbar icons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Dock icons

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616

System Tray icons

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616

Window taskbar icons and buttons

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

Chapter 36: Working with the file system
Using the FileReference class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Using the AIR file system API

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634

Chapter 37: Storing local data
Shared objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Encrypted local storage

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677

Chapter 38: Working with local SQL databases in AIR
About local SQL databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
Creating and modifying a database
Manipulating SQL database data

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692

Using synchronous and asynchronous database operations
Using encryption with SQL databases

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725

Strategies for working with SQL databases

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742

Chapter 39: Working with byte arrays
Reading and writing a ByteArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745
ByteArray example: Reading a .zip file

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

Chapter 40: Basics of networking and communication
Network interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758
Network connectivity changes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759

Domain Name System (DNS) records

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761

Chapter 41: Sockets
TCP sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
UDP sockets (AIR)
IPv6 addresses

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

Chapter 42: HTTP communications
Loading external data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Web service requests

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787

Opening a URL in another application

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794

Chapter 43: Communicating with other Flash Player and AIR instances
About the LocalConnection class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Sending messages between two applications

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799

Connecting to content in different domains and to AIR applications

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801

Last updated 3/21/2011

x

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 44: Communicating with native processes in AIR
Overview of native process communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Launching and closing a native process

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805

Communicating with a native process

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806

Security considerations for native process communication

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807

Chapter 45: Using the external API
Basics of using the external API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
External API requirements and advantages
Using the ExternalInterface class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

External API example: Communicating between ActionScript and JavaScript in a web browser

. . . . . . . . . . . . . . . . . . . . . . . . 817

External API example: Communicating between ActionScript and a desktop application that uses the ActiveX control

. . 823

Chapter 46: XML signature validation in AIR
Basics of XML signature validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829
About XML signatures

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834

Implementing the IURIDereferencer interface

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836

Chapter 47: Client system environment
Basics of the client system environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844
Using the System class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845

Using the Capabilities class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846

Capabilities example: Detecting system capabilities

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847

Chapter 48: AIR application invocation and termination
Application invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Capturing command line arguments

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852

Invoking an AIR application on user login

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855

Invoking an AIR application from the browser
Application termination

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858

Chapter 49: Working with AIR runtime and operating system information
Managing file associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860
Getting the runtime version and patch level
Detecting AIR capabilities
Tracking user presence

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861

Chapter 50: Working with AIR native windows
Basics of native windows in AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Creating windows
Managing windows

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879

Listening for window events
Displaying full-screen windows

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889

Chapter 51: Display screens in AIR
Basics of display screens in AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
Enumerating the screens

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893

Last updated 3/21/2011

xi

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 52: Printing
Basics of printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896
Printing a page

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897

Flash runtime tasks and system printing
Setting size, scale, and orientation
Advanced printing techniques

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 902

Printing example: Multiple-page printing

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904

Printing example: Scaling, cropping, and responding
Printing example: Page setup and print options
Chapter 53: Geolocation
Detecting geolocation changes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910

Chapter 54: Internationalizing applications
Basics of internationalizing applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Overview of the flash.globalization package
Determining the locale
Formatting numbers

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 914

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917

Formatting currency values

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919

Formatting dates and times

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921

Sorting and comparing strings
Case conversion

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924

Example: Internationalizing a stock ticker application

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925

Chapter 55: Localizing applications
Choosing a locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Localizing Flex content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

Localizing Flash content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

Localizing AIR applications

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

Localizing dates, times, and currencies

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931

Chapter 56: About the HTML environment
Overview of the HTML environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934
AIR and WebKit

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937

Chapter 57: Programming HTML and JavaScript in AIR
About the HTMLLoader class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
Avoiding security-related JavaScript errors

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 954

Accessing AIR API classes from JavaScript

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958

About URLs in AIR

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960

Making ActionScript objects available to JavaScript

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960

Accessing HTML DOM and JavaScript objects from ActionScript
Embedding SWF content in HTML

Using ActionScript libraries within an HTML page
Converting Date and RegExp objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968

Manipulating an HTML stylesheet from ActionScript
Cross-scripting content in different security sandboxes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 970

Last updated 3/21/2011

xii

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 58: Scripting the AIR HTML Container
Display properties of HTMLLoader objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
Scrolling HTML content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977

Accessing the HTML history list

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978

Setting the user agent used when loading HTML content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978

Setting the character encoding to use for HTML content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979

Defining browser-like user interfaces for HTML content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979

Creating subclasses of the HTMLLoader class

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990

Chapter 59: Handling HTML-related events in AIR
HTMLLoader events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
Handling DOM events with ActionScript

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991

Responding to uncaught JavaScript exceptions
Handling runtime events with JavaScript

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994

Chapter 60: Displaying HTML content in mobile apps
StageWebView objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
Content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998

Navigation events

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999

History

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000

Focus

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001

Bitmap capture

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003

Displaying ads

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005

Chapter 61: Security
Flash Platform security overview

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008

Security sandboxes

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010

Permission controls

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014

Restricting networking APIs
Full-screen mode security
Loading content
Cross-scripting

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1027

Accessing loaded media as data
Loading data

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1030

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033

Loading embedded content from SWF files imported into a security domain
Working with legacy content

Setting LocalConnection permissions
Controlling outbound URL access
Shared objects

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039

Camera, microphone, clipboard, mouse, and keyboard access
AIR security

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1040

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041

Chapter 62: How to Use ActionScript Examples
Types of Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
Running ActionScript 3.0 examples in Flash Professional
Running ActionScript 3.0 examples in Flash Builder

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063

Running ActionScript 3.0 examples on mobile devices

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064

Last updated 3/21/2011

xiii

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Contents

Chapter 63: SQL support in local databases
Supported SQL syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Data type support

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089

Chapter 64: SQL error detail messages, ids, and arguments

Last updated 3/21/2011

1

Chapter 1: Working with dates and times
Flash Player 9 and later, Adobe AIR 1.0 and later
Timing might not be everything, but it's usually a key factor in software applications. ActionScript 3.0 provides
powerful ways to manage calendar dates, times, and time intervals. Two main classes provide most of this timing
functionality: the Date class and the new Timer class in the flash.utils package.
Dates and times are a common type of information used in ActionScript programs. For instance, you might need to
know the current day of the week or to measure how much time a user spends on a particular screen, among many
other possibilities. In ActionScript, you can use the Date class to represent a single moment in time, including date and
time information. Within a Date instance are values for the individual date and time units, including year, month, date,
day of the week, hour, minutes, seconds, milliseconds, and time zone. For more advanced uses, ActionScript also
includes the Timer class, which you can use to perform actions after a certain delay or at repeated intervals.

More Help topics
Date
flash.utils.Timer

Managing calendar dates and times
Flash Player 9 and later, Adobe AIR 1.0 and later
All of the calendar date and time management functions in ActionScript 3.0 are concentrated in the top-level Date
class. The Date class contains methods and properties that let you handle dates and times in either Coordinated
Universal Time (UTC) or in local time specific to a time zone. UTC is a standard time definition that is essentially the
same as Greenwich Mean Time (GMT).

Creating Date objects
Flash Player 9 and later, Adobe AIR 1.0 and later
The Date class boasts one of the most versatile constructor methods of all the core classes. You can invoke it four
different ways.
First, if given no parameters, the Date() constructor returns a Date object containing the current date and time, in
local time based on your time zone. Here’s an example:
var now:Date = new Date();

Second, if given a single numeric parameter, the Date() constructor treats that as the number of milliseconds since
January 1, 1970, and returns a corresponding Date object. Note that the millisecond value you pass in is treated as
milliseconds since January 1, 1970, in UTC. However, the Date object shows values in your local time zone, unless you
use the UTC-specific methods to retrieve and display them. If you create a new Date object using a single milliseconds
parameter, make sure you account for the time zone difference between your local time and UTC. The following
statements create a Date object set to midnight on the day of January 1, 1970, in UTC:

Last updated 3/21/2011

2

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

var millisecondsPerDay:int = 1000 * 60 * 60 * 24;
// gets a Date one day after the start date of 1/1/1970
var startTime:Date = new Date(millisecondsPerDay);

Third, you can pass multiple numeric parameters to the Date() constructor. It treats those parameters as the year,
month, day, hour, minute, second, and millisecond, respectively, and returns a corresponding Date object. Those input
parameters are assumed to be in local time rather than UTC. The following statements get a Date object set to midnight
at the start of January 1, 2000, in local time:
var millenium:Date = new Date(2000, 0, 1, 0, 0, 0, 0);

Fourth, you can pass a single string parameter to the Date() constructor. It will try to parse that string into date or
time components and then return a corresponding Date object. If you use this approach, it’s a good idea to enclose the
Date() constructor in a try..catch block to trap any parsing errors. The Date() constructor accepts a number of
different string formats (which are listed in the ActionScript 3.0 Reference for the Adobe Flash Platform). The
following statement initializes a new Date object using a string value:
var nextDay:Date = new Date("Mon May 1 2006 11:30:00 AM");

If the Date() constructor cannot successfully parse the string parameter, it will not raise an exception. However, the
resulting Date object will contain an invalid date value.

Getting time unit values
Flash Player 9 and later, Adobe AIR 1.0 and later
You can extract the values for various units of time within a Date object using properties or methods of the Date class.
Each of the following properties gives you the value of a time unit in the Date object:

• The

fullYear property

• The

month property, which is in a numeric format with 0 for January up to 11 for December

• The

date property, which is the calendar number of the day of the month, in the range of 1 to 31

• The

day property, which is the day of the week in numeric format, with 0 standing for Sunday

• The

hours property, in the range of 0 to 23

• The

minutes property

• The

seconds property

• The

milliseconds property

In fact, the Date class gives you a number of ways to get each of these values. For example, you can get the month
value of a Date object in four different ways:

• The month property
• The getMonth() method
• The monthUTC property
• The getMonthUTC() method
All four ways are essentially equivalent in terms of efficiency, so you can use whichever approach suits your
application best.
The properties just listed all represent components of the total date value. For example, the milliseconds property
will never be greater than 999, since when it reaches 1000 the seconds value increases by 1 and the milliseconds
property resets to 0.

Last updated 3/21/2011

3

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

If you want to get the value of the Date object in terms of milliseconds since January 1, 1970 (UTC), you can use
the getTime() method. Its counterpart, the setTime() method, lets you change the value of an existing Date
object using milliseconds since January 1, 1970 (UTC).

Performing date and time arithmetic
Flash Player 9 and later, Adobe AIR 1.0 and later
You can perform addition and subtraction on dates and times with the Date class. Date values are kept internally in
terms of milliseconds, so you should convert other values to milliseconds before adding them to or subtracting them
from Date objects.
If your application will perform a lot of date and time arithmetic, you might find it useful to create constants that hold
common time unit values in terms of milliseconds, like the following:
public static const millisecondsPerMinute:int = 1000 * 60;
public static const millisecondsPerHour:int = 1000 * 60 * 60;
public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

Now it is easy to perform date arithmetic using standard time units. The following code sets a date value to one hour
from the current time using the getTime() and setTime() methods:
var oneHourFromNow:Date = new Date();
oneHourFromNow.setTime(oneHourFromNow.getTime() + millisecondsPerHour);

Another way to set a date value is to create a new Date object using a single milliseconds parameter. For example, the
following code adds 30 days to one date to calculate another:
// sets the invoice date to today's date
var invoiceDate:Date = new Date();
// adds 30 days to get the due date
var dueDate:Date = new Date(invoiceDate.getTime() + (30 * millisecondsPerDay));

Next, the millisecondsPerDay constant is multiplied by 30 to represent 30 days’ time and the result is added to the
invoiceDate value and used to set the dueDate value.

Converting between time zones
Flash Player 9 and later, Adobe AIR 1.0 and later
Date and time arithmetic comes in handy when you want to convert dates from one time zone to another. So does the
getTimezoneOffset() method, which returns the value in minutes by which the Date object’s time zone differs from
UTC. It returns a value in minutes because not all time zones are set to even-hour increments—some have half-hour
offsets from neighboring zones.
The following example uses the time zone offset to convert a date from local time to UTC. It does the conversion by
first calculating the time zone value in milliseconds and then adjusting the Date value by that amount:
// creates a Date in local time
var nextDay:Date = new Date("Mon May 1 2006 11:30:00 AM");
// converts the Date to UTC by adding or subtracting the time zone offset
var offsetMilliseconds:Number = nextDay.getTimezoneOffset() * 60 * 1000;
nextDay.setTime(nextDay.getTime() + offsetMilliseconds);

Last updated 3/21/2011

4

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

Controlling time intervals
Flash Player 9 and later, Adobe AIR 1.0 and later
When you develop applications using Adobe Flash CS4 Professional, you have access to the timeline, which provides
a steady, frame-by-frame progression through your application. In pure ActionScript projects, however, you must rely
on other timing mechanisms.

Loops versus timers
Flash Player 9 and later, Adobe AIR 1.0 and later
In some programming languages, you must devise your own timing schemes using loop statements like for or
do..while.

Loop statements generally execute as fast as the local machine allows, which means that the application runs faster on
some machines and slower on others. If your application needs a consistent timing interval, you need to tie it to an
actual calendar or clock time. Many applications, such as games, animations, and real-time controllers, need regular,
time-driven ticking mechanisms that are consistent from machine to machine.
The ActionScript 3.0 Timer class provides a powerful solution. Using the ActionScript 3.0 event model, the Timer class
dispatches timer events whenever a specified time interval is reached.

The Timer class
Flash Player 9 and later, Adobe AIR 1.0 and later
The preferred way to handle timing functions in ActionScript 3.0 is to use the Timer class (flash.utils.Timer), which
can be used to dispatch events whenever an interval is reached.
To start a timer, you first create an instance of the Timer class, telling it how often to generate a timer event and how
many times to do so before stopping.
For example, the following code creates a Timer instance that dispatches an event every second and continues for 60
seconds:
var oneMinuteTimer:Timer = new Timer(1000, 60);

The Timer object dispatches a TimerEvent object each time the given interval is reached. A TimerEvent object’s event
type is timer (defined by the constant TimerEvent.TIMER). A TimerEvent object contains the same properties as a
standard Event object.
If the Timer instance is set to a fixed number of intervals, it will also dispatch a timerComplete event (defined by the
constant TimerEvent.TIMER_COMPLETE) when it reaches the final interval.
Here is a small sample application showing the Timer class in action:

Last updated 3/21/2011

5

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class ShortTimer extends Sprite
{
public function ShortTimer()
{
// creates a new five-second Timer
var minuteTimer:Timer = new Timer(1000, 5);
// designates listeners for the interval and completion events
minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
// starts the timer ticking
minuteTimer.start();
}
public function onTick(event:TimerEvent):void
{
// displays the tick count so far
// The target of this event is the Timer instance itself.
trace("tick " + event.target.currentCount);
}
public function onTimerComplete(event:TimerEvent):void
{
trace("Time's Up!");
}
}
}

When the ShortTimer class is created, it creates a Timer instance that will tick once per second for five seconds. Then
it adds two listeners to the timer: one that listens to each tick, and one that listens for the timerComplete event.
Next, it starts the timer ticking, and from that point forward, the onTick() method executes at one-second intervals.
The onTick() method simply displays the current tick count. After five seconds have passed, the
onTimerComplete() method executes, telling you that the time is up.
When you run this sample, you should see the following lines appear in your console or trace window at the rate of
one line per second:
tick 1
tick 2
tick 3
tick 4
tick 5
Time's Up!

Last updated 3/21/2011

6

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

Timing functions in the flash.utils package
Flash Player 9 and later, Adobe AIR 1.0 and later
ActionScript 3.0 contains a number of timing functions similar to those that were available in ActionScript 2.0. These
functions are provided as package-level functions in the flash.utils package, and they operate just as they did in
ActionScript 2.0.
Function

Description

clearInterval(id:uint):void

Cancels a specified setInterval() call.

clearTimeout(id:uint):void

Cancels a specified setTimeout() call.

getTimer():int

Returns the number of milliseconds that have elapsed since Adobe® Flash® Player
or Adobe® AIR™ was initialized.

setInterval(closure:Function,
delay:Number, ... arguments):uint

Runs a function at a specified interval (in milliseconds).

setTimeout(closure:Function,
delay:Number, ... arguments):uint

Runs a specified function after a specified delay (in milliseconds).

These functions remain in ActionScript 3.0 for backward compatibility. Adobe does not recommend that you use them
in new ActionScript 3.0 applications. In general, it is easier and more efficient to use the Timer class in your
applications.

Date and time example: Simple analog clock
Flash Player 9 and later, Adobe AIR 1.0 and later
A simple analog clock example illustrates these two date and time concepts:

• Getting the current date and time and extracting values for the hours, minutes, and seconds
• Using a Timer to set the pace of an application
To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The
SimpleClock application files can be found in the folder Samples/SimpleClock. The application consists of the
following files:
File

Description

SimpleClockApp.mxml

The main application file in Flash (FLA) or Flex (MXML).

or
SimpleClockApp.fla
com/example/programmingas3/simpleclock/SimpleClock.as

The main application file.

com/example/programmingas3/simpleclock/AnalogClockFace.as

Draws a round clock face and hour, minute, and seconds
hands based on the time.

Last updated 3/21/2011

7

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

Defining the SimpleClock class
Flash Player 9 and later, Adobe AIR 1.0 and later
The clock example is simple, but it’s a good idea to organize even simple applications well so you could easily expand
them in the future. To that end, the SimpleClock application uses the SimpleClock class to handle the startup and timekeeping tasks, and then uses another class named AnalogClockFace to actually display the time.
Here is the code that defines and initializes the SimpleClock class (note that in the Flash version, SimpleClock extends
the Sprite class instead):
public class SimpleClock extends UIComponent
{
/**
* The time display component.
*/
private var face:AnalogClockFace;
/**
* The Timer that acts like a heartbeat for the application.
*/
private var ticker:Timer;

The class has two important properties:

•

The face property, which is an instance of the AnalogClockFace class

•

The ticker property, which is an instance of the Timer class

The SimpleClock class uses a default constructor. The initClock() method takes care of the real setup work,
creating the clock face and starting the Timer instance ticking.

Creating the clock face
Flash Player 9 and later, Adobe AIR 1.0 and later
The next lines in the SimpleClock code create the clock face that is used to display the time:
/**
* Sets up a SimpleClock instance.
*/
public function initClock(faceSize:Number = 200)
{
// creates the clock face and adds it to the display list
face = new AnalogClockFace(Math.max(20, faceSize));
face.init();
addChild(face);
// draws the initial clock display
face.draw();

The size of the face can be passed in to the initClock() method. If no faceSize value is passed, a default size of 200
pixels is used.
Next, the application initializes the face and then adds it to the display list using the addChild() method inherited
from the DisplayObjectContainer class. Then it calls the AnalogClockFace.draw() method to display the clock face
once, showing the current time.

Last updated 3/21/2011

8

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

Starting the timer
Flash Player 9 and later, Adobe AIR 1.0 and later
After creating the clock face, the initClock() method sets up a timer:
// creates a Timer that fires an event once per second
ticker = new Timer(1000);
// designates the onTick() method to handle Timer events
ticker.addEventListener(TimerEvent.TIMER, onTick);
// starts the clock ticking
ticker.start();

First this method instantiates a Timer instance that will dispatch an event once per second (every 1000 milliseconds).
Since no second repeatCount parameter is passed to the Timer() constructor, the Timer will keep repeating
indefinitely.
The SimpleClock.onTick() method will execute once per second when the timer event is received:
public function onTick(event:TimerEvent):void
{
// updates the clock display
face.draw();
}

The AnalogClockFace.draw() method simply draws the clock face and hands.

Displaying the current time
Flash Player 9 and later, Adobe AIR 1.0 and later
Most of the code in the AnalogClockFace class involves setting up the clock face’s display elements. When the
AnalogClockFace is initialized, it draws a circular outline, places a numeric text label at each hour mark, and then
creates three Shape objects, one each for the hour hand, the minute hand, and the second hand on the clock.
Once the SimpleClock application is running, it calls the AnalogClockFace.draw() method each second, as follows:
/**
* Called by the parent container when the display is being drawn.
*/
public override function draw():void
{
// stores the current date and time in an instance variable
currentTime = new Date();
showTime(currentTime);
}

This method saves the current time in a variable, so the time can’t change in the middle of drawing the clock hands.
Then it calls the showTime() method to display the hands, as the following shows:

Last updated 3/21/2011

9

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with dates and times

/**
* Displays the given Date/Time in that good old analog clock style.
*/
public function showTime(time:Date):void
{
// gets the time values
var seconds:uint = time.getSeconds();
var minutes:uint = time.getMinutes();
var hours:uint = time.getHours();
// multiplies by 6 to get degrees
this.secondHand.rotation = 180 + (seconds * 6);
this.minuteHand.rotation = 180 + (minutes * 6);
// Multiply by 30 to get basic degrees, then
// add up to 29.5 degrees (59 * 0.5)
// to account for the minutes.
this.hourHand.rotation = 180 + (hours * 30) + (minutes * 0.5);
}

First, this method extracts the values for the hours, minutes, and seconds of the current time. Then it uses these values
to calculate the angle for each hand. Since the second hand makes a full rotation in 60 seconds, it rotates 6 degrees each
second (360/60). The minute hand rotates the same amount each minute.
The hour hand updates every minute, too, so it can show some progress as the minutes tick by. It rotates 30 degrees
each hour (360/12), but it also rotates half a degree each minute (30 degrees divided by 60 minutes).

Last updated 3/21/2011

10

Chapter 2: Working with strings
Flash Player 9 and later, Adobe AIR 1.0 and later
The String class contains methods that let you work with text strings. Strings are important in working with many
objects. The methods described here are useful for working with strings used in objects such as TextField, StaticText,
XML, ContextMenu, and FileReference objects.
Strings are sequences of characters. ActionScript 3.0 supports ASCII and Unicode characters.

More Help topics
String
RegExp
parseFloat()
parseInt()

Basics of strings
Flash Player 9 and later, Adobe AIR 1.0 and later
In programming parlance, a string is a text value—a sequence of letters, numbers, or other characters strung together
into a single value. For instance, this line of code creates a variable with the data type String and assigns a literal string
value to that variable:
var albumName:String = "Three for the money";

As this example shows, in ActionScript you can denote a string value by surrounding text with double or single
quotation marks. Here are several more examples of strings:
"Hello"
"555-7649"
"http://www.adobe.com/"

Any time you manipulate a piece of text in ActionScript, you are working with a string value. The ActionScript String
class is the data type you can use to work with text values. String instances are frequently used for properties, method
parameters, and so forth in many other ActionScript classes.
Important concepts and terms
The following reference list contains important terms related to strings that you will encounter:
ASCII A system for representing text characters and symbols in computer programs. The ASCII system supports the

26-letter English alphabet, plus a limited set of additional characters.
Character The smallest unit of text data (a single letter or symbol).
Concatenation Joining multiple string values together by adding one to the end of the other, creating a new string

value.

Last updated 3/21/2011

11

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Empty string A string that contains no text, white space, or other characters, written as "". An empty string value is

different from a String variable with a null value—a null String variable is a variable that does not have a String instance
assigned to it, whereas an empty string has an instance with a value that contains no characters.
String A textual value (sequence of characters).
String literal (or “literal string”) A string value written explicitly in code, written as a text value surrounded by double
quotation marks or single quotation marks.
Substring A string that is a portion of another string.
Unicode A standard system for representing text characters and symbols in computer programs. The Unicode system

allows for the use of any character in any writing system.

Creating strings
Flash Player 9 and later, Adobe AIR 1.0 and later
The String class is used to represent string (textual) data in ActionScript 3.0. ActionScript strings support both ASCII
and Unicode characters. The simplest way to create a string is to use a string literal. To declare a string literal, use
straight double quotation mark (") or single quotation mark (') characters. For example, the following two strings are
equivalent:
var str1:String = "hello";
var str2:String = 'hello';

You can also declare a string by using the new operator, as follows:
var str1:String = new String("hello");
var str2:String = new String(str1);
var str3:String = new String();
// str3 == ""

The following two strings are equivalent:
var str1:String = "hello";
var str2:String = new String("hello");

To use single quotation marks (') within a string literal defined with single quotation mark (') delimiters, use the
backslash escape character (\). Similarly, to use double quotation marks (") within a string literal defined with double
quotation marks (") delimiters, use the backslash escape character (\). The following two strings are equivalent:
var str1:String = "That's \"A-OK\"";
var str2:String = 'That\'s "A-OK"';

You may choose to use single quotation marks or double quotation marks based on any single or double quotation
marks that exist in a string literal, as in the following:
var str1:String = "ActionScript 3.0";
var str2:String = 'banana';

Keep in mind that ActionScript distinguishes between a straight single quotation mark (') and a left or right single
quotation mark (' or ' ). The same is true for double quotation marks. Use straight quotation marks to delineate
string literals. When pasting text from another source into ActionScript, be sure to use the correct characters.
As the following table shows, you can use the backslash escape character (\) to define other characters in string literals:

Last updated 3/21/2011

12

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Escape sequence

Character

\b

Backspace

\f

Form feed

\n

Newline

\r

Carriage return

\t

Tab

\unnnn

The Unicode character with the character code specified by the hexadecimal number nnnn; for
example, \u263a is the smiley character.

\\xnn

The ASCII character with the character code specified by the hexadecimal number nn

\'

Single quotation mark

\"

Double quotation mark

\\

Single backslash character

The length property
Flash Player 9 and later, Adobe AIR 1.0 and later
Every string has a length property, which is equal to the number of characters in the string:
var str:String = "Adobe";
trace(str.length);

// output: 5

An empty string and a null string both have a length of 0, as the following example shows:
var str1:String = new String();
trace(str1.length);
// output: 0
str2:String = '';
trace(str2.length);

// output: 0

Working with characters in strings
Flash Player 9 and later, Adobe AIR 1.0 and later
Every character in a string has an index position in the string (an integer). The index position of the first character is
0. For example, in the following string, the character y is in position 0 and the character w is in position 5:
"yellow"

You can examine individual characters in various positions in a string using the charAt() method and the
charCodeAt() method, as in this example:
var str:String = "hello world!";
for (var i:int = 0; i < str.length; i++)
{
trace(str.charAt(i), "-", str.charCodeAt(i));
}

Last updated 3/21/2011

13

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

When you run this code, the following output is produced:
h
e
l
l
o
w
o
r
l
d
!

- 104
- 101
- 108
- 108
- 111
32
- 119
- 111
- 114
- 108
- 100
- 33

You can also use character codes to define a string using the fromCharCode() method, as the following example
shows:
var myStr:String = String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100,33);
// Sets myStr to "hello world!"

Comparing strings
Flash Player 9 and later, Adobe AIR 1.0 and later
You can use the following operators to compare strings: <, <=, !=, ==, =>, and >. These operators can be used with
conditional statements, such as if and while, as the following example shows:
var str1:String = "Apple";
var str2:String = "apple";
if (str1 < str2)
{
trace("A < a, B < b, C < c, ...");
}

When using these operators with strings, ActionScript considers the character code value of each character in the
string, comparing characters from left to right, as in the following:
trace("A" < "B"); // true
trace("A" < "a"); // true
trace("Ab" < "az"); // true
trace("abc" < "abza"); // true

Use the == and != operators to compare strings with each other and to compare strings with other types of objects, as
the following example shows:
var str1:String = "1";
var str1b:String = "1";
var str2:String = "2";
trace(str1 == str1b); // true
trace(str1 == str2); // false
var total:uint = 1;
trace(str1 == total); // true

Last updated 3/21/2011

14

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Obtaining string representations of other objects
Flash Player 9 and later, Adobe AIR 1.0 and later
You can obtain a String representation for any kind of object. All objects have a toString() method for this purpose:
var n:Number = 99.47;
var str:String = n.toString();
// str == "99.47"

When using the + concatenation operator with a combination of String objects and objects that are not strings, you do
not need to use the toString() method. For details on concatenation, see the next section.
The String() global function returns the same value for a given object as the value returned by the object calling the
toString() method.

Concatenating strings
Flash Player 9 and later, Adobe AIR 1.0 and later
Concatenation of strings means taking two strings and joining them sequentially into one. For example, you can use
the + operator to concatenate two strings:
var str1:String = "green";
var str2:String = "ish";
var str3:String = str1 + str2; // str3 == "greenish"

You can also use the += operator to the produce the same result, as the following example shows:
var str:String = "green";
str += "ish"; // str == "greenish"

Additionally, the String class includes a concat() method, which can be used as follows:
var str1:String = "Bonjour";
var str2:String = "from";
var str3:String = "Paris";
var str4:String = str1.concat(" ", str2, " ", str3);
// str4 == "Bonjour from Paris"

If you use the + operator (or the += operator) with a String object and an object that is not a string, ActionScript
automatically converts the nonstring object to a String object in order to evaluate the expression, as shown in this
example:
var str:String = "Area = ";
var area:Number = Math.PI * Math.pow(3, 2);
str = str + area; // str == "Area = 28.274333882308138"

However, you can use parentheses for grouping to provide context for the + operator, as the following example shows:
trace("Total: $" + 4.55 + 1.45); // output: Total: $4.551.45
trace("Total: $" + (4.55 + 1.45)); // output: Total: $6

Last updated 3/21/2011

15

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Finding substrings and patterns in strings
Flash Player 9 and later, Adobe AIR 1.0 and later
Substrings are sequential characters within a string. For example, the string "abc" has the following substrings: "",
"a", "ab", "abc", "b", "bc", "c". You can use ActionScript methods to locate substrings of a string.
Patterns are defined in ActionScript by strings or by regular expressions. For example, the following regular expression
defines a specific pattern—the letters A, B, and C followed by a digit character (the forward slashes are regular
expression delimiters):
/ABC\d/

ActionScript includes methods for finding patterns in strings and for replacing found matches with replacement
substrings. These methods are described in the following sections.
Regular expressions can define intricate patterns. For more information, see “Using regular expressions” on page 76.

Finding a substring by character position
Flash Player 9 and later, Adobe AIR 1.0 and later
The substr() and substring() methods are similar. Both return a substring of a string. Both take two parameters.
In both methods, the first parameter is the position of the starting character in the given string. However, in the
substr() method, the second parameter is the length of the substring to return, and in the substring() method, the
second parameter is the position of the character at the end of the substring (which is not included in the returned
string). This example shows the difference between these two methods:
var str:String = "Hello from Paris, Texas!!!";
trace(str.substr(11,15)); // output: Paris, Texas!!!
trace(str.substring(11,15)); // output: Pari

The slice() method functions similarly to the substring() method. When given two non-negative integers as
parameters, it works exactly the same. However, the slice() method can take negative integers as parameters, in
which case the character position is taken from the end of the string, as shown in the following example:
var str:String = "Hello from Paris,
trace(str.slice(11,15)); // output:
trace(str.slice(-3,-1)); // output:
trace(str.slice(-3,26)); // output:
trace(str.slice(-3,str.length)); //
trace(str.slice(-8,-3)); // output:

Texas!!!";
Pari
!!
!!!
output: !!!
Texas

You can combine non-negative and negative integers as the parameters of the slice() method.

Finding the character position of a matching substring
Flash Player 9 and later, Adobe AIR 1.0 and later
You can use the indexOf() and lastIndexOf() methods to locate matching substrings within a string, as the
following example shows:
var str:String = "The moon, the stars, the sea, the land";
trace(str.indexOf("the")); // output: 10

Notice that the indexOf() method is case-sensitive.

Last updated 3/21/2011

16

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

You can specify a second parameter to indicate the index position in the string from which to start the search, as
follows:
var str:String = "The moon, the stars, the sea, the land"
trace(str.indexOf("the", 11)); // output: 21

The lastIndexOf() method finds the last occurrence of a substring in the string:
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the")); // output: 30

If you include a second parameter with the lastIndexOf() method, the search is conducted from that index position
in the string working backward (from right to left):
var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the", 29)); // output: 21

Creating an array of substrings segmented by a delimiter
Flash Player 9 and later, Adobe AIR 1.0 and later
You can use the split() method to create an array of substrings, which is divided based on a delimiter. For example,
you can segment a comma-delimited or tab-delimited string into multiple strings.
The following example shows how to split an array into substrings with the ampersand (&) character as the delimiter:
var queryStr:String = "first=joe&last=cheng&title=manager&StartDate=3/6/65";
var params:Array = queryStr.split("&", 2); // params == ["first=joe","last=cheng"]

The second parameter of the split() method, which is optional, defines the maximum size of the array that is
returned.
You can also use a regular expression as the delimiter character:
var str:String = "Give me\t5."
var a:Array = str.split(/\s+/); // a == ["Give","me","5."]

For more information, see “Using regular expressions” on page 76 and the ActionScript 3.0 Reference for the Adobe
Flash Platform.

Finding patterns in strings and replacing substrings
Flash Player 9 and later, Adobe AIR 1.0 and later
The String class includes the following methods for working with patterns in strings:

• Use the match() and search() methods to locate substrings that match a pattern.
• Use the replace() method to find substrings that match a pattern and replace them with a specified substring.
These methods are described in the following sections.
You can use strings or regular expressions to define patterns used in these methods. For more information on regular
expressions, see “Using regular expressions” on page 76.
Finding matching substrings
The search() method returns the index position of the first substring that matches a given pattern, as shown in this
example:

Last updated 3/21/2011

17

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

var str:String = "The more the merrier.";
// (This search is case-sensitive.)
trace(str.search("the")); // output: 9

You can also use regular expressions to define the pattern to match, as this example shows:
var pattern:RegExp = /the/i;
var str:String = "The more the merrier.";
trace(str.search(pattern)); // 0

The output of the trace() method is 0, because the first character in the string is index position 0. The i flag is set in
the regular expression, so the search is not case-sensitive.
The search() method finds only one match and returns its starting index position, even if the g (global) flag is set in
the regular expression.
The following example shows a more intricate regular expression, one that matches a string in double quotation marks:
var pattern:RegExp = /"[^"]*"/;
var str:String = "The \"more\" the merrier.";
trace(str.search(pattern)); // output: 4
str = "The \"more the merrier.";
trace(str.search(pattern)); // output: -1
// (Indicates no match, since there is no closing double quotation mark.)

The match() method works similarly. It searches for a matching substring. However, when you use the global flag in
a regular expression pattern, as in the following example, match() returns an array of matching substrings:
var str:String = "bob@example.com, omar@example.org";
var pattern:RegExp = /\w*@\w*\.[org|com]+/g;
var results:Array = str.match(pattern);

The results array is set to the following:
["bob@example.com","omar@example.org"]

For more information on regular expressions, see “Using regular expressions” on page 76.
Replacing matched substrings
You can use the replace() method to search for a specified pattern in a string and replace matches with the specified
replacement string, as the following example shows:
var str:String = "She sells seashells by the seashore.";
var pattern:RegExp = /sh/gi;
trace(str.replace(pattern, "sch")); //sche sells seaschells by the seaschore.

Note that in this example, the matched strings are not case-sensitive because the i (ignoreCase) flag is set in the
regular expression, and multiple matches are replaced because the g (global) flag is set. For more information, see
“Using regular expressions” on page 76.
You can include the following $ replacement codes in the replacement string. The replacement text shown in the
following table is inserted in place of the $ replacement code:

Last updated 3/21/2011

18

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

$ Code

Replacement Text

$$

$

$&

The matched substring.

$`

The portion of the string that precedes the matched substring. This code uses the straight left single quotation
mark character (`), not the straight single quotation mark (') or the left curly single quotation mark (' ).

$'

The portion of the string that follows the matched substring. This code uses the straight single quotation mark (' ).

$n

The nth captured parenthetical group match, where n is a single digit, 1-9, and $n is not followed by a decimal digit.

$nn

The nnth captured parenthetical group match, where nn is a two-digit decimal number, 01–99. If the nnth capture
is undefined, the replacement text is an empty string.

For example, the following shows the use of the $2 and $1 replacement codes, which represent the first and second
capturing group matched:
var str:String = "flip-flop";
var pattern:RegExp = /(\w+)-(\w+)/g;
trace(str.replace(pattern, "$2-$1")); // flop-flip

You can also use a function as the second parameter of the replace() method. The matching text is replaced by the
returned value of the function.
var str:String = "Now only $9.95!";
var price:RegExp = /\$([\d,]+.\d+)+/i;
trace(str.replace(price, usdToEuro));
function usdToEuro(matchedSubstring:String, capturedMatch1:String,
str:String):String
{
var usd:String = capturedMatch1;
usd = usd.replace(",", "");
var exchangeRate:Number = 0.853690;
var euro:Number = parseFloat(usd) * exchangeRate;
const euroSymbol:String = String.fromCharCode(8364);
return euro.toFixed(2) + " " + euroSymbol;
}

index:int,

When you use a function as the second parameter of the replace() method, the following arguments are passed to
the function:

• The matching portion of the string.
• Any capturing parenthetical group matches. The number of arguments passed this way will vary depending on the
number of parenthetical matches. You can determine the number of parenthetical matches by checking
arguments.length - 3 within the function code.

• The index position in the string where the match begins.
• The complete string.

Last updated 3/21/2011

19

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Converting strings between uppercase and lowercase
Flash Player 9 and later, Adobe AIR 1.0 and later
As the following example shows, the toLowerCase() method and the toUpperCase() method convert alphabetical
characters in the string to lowercase and uppercase, respectively:
var str:String = "Dr. Bob Roberts, #9."
trace(str.toLowerCase()); // dr. bob roberts, #9.
trace(str.toUpperCase()); // DR. BOB ROBERTS, #9.

After these methods are executed, the source string remains unchanged. To transform the source string, use the
following code:
str = str.toUpperCase();

These methods work with extended characters, not simply a–z and A–Z:
var str:String = "José Barça";
trace(str.toUpperCase(), str.toLowerCase()); // JOSÉ BARÇA josé barça

Strings example: ASCII art
Flash Player 9 and later, Adobe AIR 1.0 and later
This ASCII Art example shows a number of features of working with the String class in ActionScript 3.0, including the
following:

• The split() method of the String class is used to extract values from a character-delimited string (image
information in a tab-delimited text file).

• Several string-manipulation techniques, including split(), concatenation, and extracting a portion of the string
using substring() and substr(), are used to capitalize the first letter of each word in the image titles.

• The getCharAt() method is used to get a single character from a string (to determine the ASCII character
corresponding to a grayscale bitmap value).

• String concatenation is used to build up the ASCII art representation of an image one character at a time.
The term ASCII art refers to a text representations of an image, in which a grid of monospaced font characters, such
as Courier New characters, plots the image. The following image shows an example of ASCII art produced by the
application:

Last updated 3/21/2011

20

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

The ASCII art version of the graphic is shown on the right.

To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The
ASCIIArt application files can be found in the folder Samples/AsciiArt. The application consists of the following files:
File

Description

AsciiArtApp.mxml

The main application file in Flash (FLA) or Flex (MXML)

or
AsciiArtApp.fla
com/example/programmingas3/asciiArt/AsciiArtBuilder.as

The class that provides the main functionality of the
application, including extracting image metadata from a text
file, loading the images, and managing the image-to-text
conversion process.

com/example/programmingas3/asciiArt/BitmapToAsciiConverter.as

A class that provides the parseBitmapData() method for
converting image data into a String version.

com/example/programmingas3/asciiArt/Image.as

A class which represents a loaded bitmap image.

com/example/programmingas3/asciiArt/ImageInfo.as

A class representing metadata for an ASCII art image (such as
title, image file URL, and so on).

image/

A folder containing images used by the application.

txt/ImageData.txt

A tab-delimited text file, containing information on the
images to be loaded by the application.

Extracting tab-delimited values
Flash Player 9 and later, Adobe AIR 1.0 and later
This example uses the common practice of storing application data separate from the application itself; that way, if the
data changes (for example, if another image is added or an image’s title changes), there is no need to recreate the SWF
file. In this case, the image metadata, including the image title, the URL of the actual image file, and some values that
are used to manipulate the image, are stored in a text file (the txt/ImageData.txt file in the project). The contents of the
text file are as follows:

Last updated 3/21/2011

21

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

FILENAMETITLEWHITE_THRESHHOLDBLACK_THRESHHOLD
FruitBasket.jpgPear, apple, orange, and bananad810
Banana.jpgA picture of a bananaC820
Orange.jpgorangeFF20
Apple.jpgpicture of an apple6E10

The file uses a specific tab-delimited format. The first line (row) is a heading row. The remaining lines contain the
following data for each bitmap to be loaded:

• The filename of the bitmap.
• The display name of the bitmap.
• The white-threshold and black-threshold values for the bitmaps. These are hex values above which and below
which a pixel is to be considered completely white or completely black.
As soon as the application starts, the AsciiArtBuilder class loads and parses the contents of the text file in order to
create the “stack” of images that it will display, using the following code from the AsciiArtBuilder class’s
parseImageInfo() method:
var lines:Array = _imageInfoLoader.data.split("\n");
var numLines:uint = lines.length;
for (var i:uint = 1; i < numLines; i++)
{
var imageInfoRaw:String = lines[i];
...
if (imageInfoRaw.length > 0)
{
// Create a new image info record and add it to the array of image info.
var imageInfo:ImageInfo = new ImageInfo();
// Split the current line into values (separated by tab (\t)
// characters) and extract the individual properties:
var imageProperties:Array = imageInfoRaw.split("\t");
imageInfo.fileName = imageProperties[0];
imageInfo.title = normalizeTitle(imageProperties[1]);
imageInfo.whiteThreshold = parseInt(imageProperties[2], 16);
imageInfo.blackThreshold = parseInt(imageProperties[3], 16);
result.push(imageInfo);
}
}

The entire contents of the text file are contained in a single String instance, the _imageInfoLoader.data property.
Using the split() method with the newline character ("\n") as a parameter, the String instance is divided into an
Array (lines) whose elements are the individual lines of the text file. Next, the code uses a loop to work with each of
the lines (except the first, because it contains only headers rather than actual content). Inside the loop, the split()
method is used once again to divide the contents of the single line into a set of values (the Array object named
imageProperties). The parameter used with the split() method in this case is the tab ("\t") character, because the
values in each line are delineated by tab characters.

Last updated 3/21/2011

22

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

Using String methods to normalize image titles
Flash Player 9 and later, Adobe AIR 1.0 and later
One of the design decisions for this application is that all the image titles are displayed using a standard format, with
the first letter of each word capitalized (except for a few words that are commonly not capitalized in English titles).
Rather than assume that the text file contains properly formatted titles, the application formats the titles while they’re
being extracted from the text file.
In the previous code listing, as part of extracting individual image metadata values, the following line of code is used:
imageInfo.title = normalizeTitle(imageProperties[1]);

In that code, the image’s title from the text file is passed through the normalizeTitle() method before it is stored in
the ImageInfo object:
private
{
var
var
for
{

function normalizeTitle(title:String):String
words:Array = title.split(" ");
len:uint = words.length;
(var i:uint; i < len; i++)
words[i] = capitalizeFirstLetter(words[i]);

}
return words.join(" ");
}

This method uses the split() method to divide the title into individual words (separated by the space character),
passes each word through the capitalizeFirstLetter() method, and then uses the Array class’s join() method
to combine the words back into a single string again.
As its name suggests, the capitalizeFirstLetter() method actually does the work of capitalizing the first letter of
each word:

Last updated 3/21/2011

23

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

/**
* Capitalizes the first letter of a single word, unless it's one of
* a set of words that are normally not capitalized in English.
*/
private function capitalizeFirstLetter(word:String):String
{
switch (word)
{
case "and":
case "the":
case "in":
case "an":
case "or":
case "at":
case "of":
case "a":
// Don't do anything to these words.
break;
default:
// For any other word, capitalize the first character.
var firstLetter:String = word.substr(0, 1);
firstLetter = firstLetter.toUpperCase();
var otherLetters:String = word.substring(1);
word = firstLetter + otherLetters;
}
return word;
}

In English, the initial character of each word in a title is not capitalized if it is one of the following words: “and,” “the,”
“in,” “an,” “or,” “at,” “of,” or “a.” (This is a simplified version of the rules.) To execute this logic, the code first uses a
switch statement to check if the word is one of the words that should not be capitalized. If so, the code simply jumps
out of the switch statement. On the other hand, if the word should be capitalized, that is done in several steps, as
follows:
1 The first letter of the word is extracted using substr(0, 1), which extracts a substring starting with the character

at index 0 (the first letter in the string, as indicated by the first parameter 0). The substring will be one character in
length (indicated by the second parameter 1).
2 That character is capitalized using the toUpperCase() method.
3 The remaining characters of the original word are extracted using substring(1), which extracts a substring

starting at index 1 (the second letter) through the end of the string (indicated by leaving off the second parameter
of the substring() method).
4 The final word is created by combining the newly capitalized first letter with the remaining letters using string

concatenation: firstLetter + otherLetters.

Generating the ASCII art text
Flash Player 9 and later, Adobe AIR 1.0 and later
The BitmapToAsciiConverter class provides the functionality of converting a bitmap image to its ASCII text
representation. This process is performed by the parseBitmapData() method, which is partially shown here:

Last updated 3/21/2011

24

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with strings

var result:String = "";
// Loop through the rows of pixels top to bottom:
for (var y:uint = 0; y < _data.height; y += verticalResolution)
{
// Within each row, loop through pixels left to right:
for (var x:uint = 0; x < _data.width; x += horizontalResolution)
{
...
// Convert the gray value in the 0-255 range to a value
// in the 0-64 range (since that's the number of "shades of
// gray" in the set of available characters):
index = Math.floor(grayVal / 4);
result += palette.charAt(index);
}
result += "\n";
}
return result;

This code first defines a String instance named result that will be used to build up the ASCII art version of the bitmap
image. Next, it loops through individual pixels of the source bitmap image. Using several color-manipulation
techniques (omitted here for brevity), it converts the red, green, and blue color values of an individual pixel to a single
grayscale value (a number from 0 to 255). The code then divides that value by 4 (as shown) to convert it to a value in
the 0-63 scale, which is stored in the variable index. (The 0-63 scale is used because the “palette” of available ASCII
characters used by this application contains 64 values.) The palette of characters is defined as a String instance in the
BitmapToAsciiConverter class:
// The characters are in order from darkest to lightest, so that their
// position (index) in the string corresponds to a relative color value
// (0 = black).
private static const palette:String =
"@#$%&8BMW*mwqpdbkhaoQ0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+_~-;,. ";

Since the index variable defines which ASCII character in the palette corresponds to the current pixel in the bitmap
image, that character is retrieved from the palette String using the charAt() method. It is then appended to the
result String instance using the concatenation assignment operator (+=). In addition, at the end of each row of pixels,
a newline character is concatenated to the end of the result String, forcing the line to wrap to create a new row of
character “pixels.”

Last updated 3/21/2011

25

Chapter 3: Working with arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
Arrays allow you to store multiple values in a single data structure. You can use simple indexed arrays that store values
using fixed ordinal integer indexes or complex associative arrays that store values using arbitrary keys. Arrays can also
be multidimensional, containing elements that are themselves arrays. Finally, you can use a Vector for an array whose
elements are all instances of the same data type.

More Help topics
Array
Vector

Basics of arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
Often in programming you’ll need to work with a set of items rather than a single object. For example, in a music player
application, you might want to have a list of songs waiting to be played. You wouldn’t want to have to create a separate
variable for each song on that list. It would be preferable to have all the Song objects together in a bundle, and be able
to work with them as a group.
An array is a programming element that acts as a container for a set of items, such as a list of songs. Most commonly
all the items in an array are instances of the same class, but that is not a requirement in ActionScript. The individual
items in an array are known as the array’s elements. You can think of an array as a file drawer for variables. Variables
can be added as elements in the array, which is like placing a folder into the file drawer. You can work with the array
as a single variable (like carrying the whole drawer to a different location). You can work with the variables as a group
(like flipping through the folders one by one searching for a piece of information). You can also access them
individually (like opening the drawer and selecting a single folder).
For example, imagine you’re creating a music player application where a user can select multiple songs and add them
to a playlist. In your ActionScript code, you have a method named addSongsToPlaylist(), which accepts a single
array as a parameter. No matter how many songs you want to add to the list (a few, a lot, or even only one), you call
the addSongsToPlaylist() method only one time, passing it the array containing the Song objects. Inside the
addSongsToPlaylist() method, you can use a loop to go through the array’s elements (the songs) one by one and
actually add them to the playlist.
The most common type of ActionScript array is an indexed array. In an indexed array each item is stored in a
numbered slot (known as an index). Items are accessed using the number, like an address. Indexed arrays work well
for most programming needs. The Array class is one common class that’s used to represent an indexed array.
Often, an indexed array is used to store multiple items of the same type (objects that are instances of the same class).
The Array class doesn’t have any means for restricting the type of items it contains. The Vector class is a type of indexed
array in which all the items in a single array are the same type. Using a Vector instance instead of an Array instance
can also provide performance improvements and other benefits. The Vector class is available starting with Flash Player
10 and Adobe AIR 1.5.

Last updated 3/21/2011

26

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

A special use of an indexed array is a multidimensional array. A multidimensional array is an indexed array whose
elements are indexed arrays (which in turn contain other elements).
Another type of array is an associative array, which uses a string key instead of a numeric index to identify individual
elements. Finally, ActionScript 3.0 also includes the Dictionary class, which represents a dictionary. A dictionary is an
array that allows you to use any type of object as a key to distinguish between elements.
Important concepts and terms
The following reference list contains important terms that you will encounter when programming array and vector
handling routines:
Array An object that serves as a container to group multiple objects.
Array access ([]) operator A pair of square brackets surrounding an index or key that uniquely identifies an array

element. This syntax is used after an array variable name to specify a single element of the array rather than the entire
array.
Associative array An array that uses string keys to identify individual elements.
Base type The data type of the objects that a Vector instance is allowed to store.
Dictionary An array whose items consist of pairs of objects, known as the key and the value. The key is used instead
of a numeric index to identify a single element.
Element A single item in an array.
Index The numeric “address” used to identify a single element in an indexed array.
Indexed array The standard type of array that stores each element in a numbered position, and uses the number

(index) to identify individual elements.
Key The string or object used to identify a single element in an associative array or a dictionary.
Multidimensional array An array containing items that are arrays rather than single values.
T The standard convention that’s used in this documentation to represent the base type of a Vector instance, whatever
that base type happens to be. The T convention is used to represent a class name, as shown in the Type parameter
description. (“T” stands for “type,” as in “data type.”).
Type parameter The syntax that’s used with the Vector class name to specify the Vector’s base type (the data type of
the objects that it stores). The syntax consists of a period (.), then the data type name surrounded by angle brackets
(<>). Put together, it looks like this: Vector.. In this documentation, the class specified in the type parameter is
represented generically as T.
Vector A type of array whose elements are all instances of the same data type.

Indexed arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
Indexed arrays store a series of one or more values organized such that each value can be accessed using an unsigned
integer value. The first index is always the number 0, and the index increments by 1 for each subsequent element added
to the array. In ActionScript 3.0, two classes are used as indexed arrays: the Array class and the Vector class.
Indexed arrays use an unsigned 32-bit integer for the index number. The maximum size of an indexed array is 232 - 1
or 4,294,967,295. An attempt to create an array that is larger than the maximum size results in a run-time error.

Last updated 3/21/2011

27

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

To access an individual element of an indexed array, you use the array access ([]) operator to specify the index position
of the element you wish to access. For example, the following code represents the first element (the element at index
0) in an indexed array named songTitles:
songTitles[0]

The combination of the array variable name followed by the index in square brackets functions as a single identifier.
(In other words, it can be used in any way a variable name can). You can assign a value to an indexed array element by
using the name and index on the left side of an assignment statement:
songTitles[1] = "Symphony No. 5 in D minor";

Likewise, you can retrieve the value of an indexed array element by using the name and index on the right side of an
assignment statement:
var nextSong:String = songTitles[2];

You can also use a variable in the square brackets rather than providing an explicit value. (The variable must contain
a non-negative integer value such as a uint, a positive int, or a positive integer Number instance). This technique is
commonly used to “loop over” the elements in an indexed array and perform an operation on some or all the elements.
The following code listing demonstrates this technique. The code uses a loop to access each value in an Array object
named oddNumbers. It uses the trace() statement to print each value in the form “oddNumber[index] = value”:
var oddNumbers:Array = [1, 3, 5, 7, 9, 11];
var len:uint = oddNumbers.length;
for (var i:uint = 0; i < len; i++)
{
trace("oddNumbers[" + i.toString() + "] = " + oddNumbers[i].toString());
}

The Array class
The first type of indexed array is the Array class. An Array instance can hold a value of any data type. The same Array
object can hold objects that are of different data types. For example, a single Array instance can have a String value in
index 0, a Number instance in index 1, and an XML object in index 2.
The Vector class
Another type of indexed array that’s available in ActionScript 3.0 is the Vector class. A Vector instance is a typed array,
which means that all the elements in a Vector instance always have the same data type.
Note: The Vector class is available starting with Flash Player 10 and Adobe AIR 1.5.
When you declare a Vector variable or instantiate a Vector object, you explicitly specify the data type of the objects
that the Vector can contain. The specified data type is known as the Vector’s base type. At run time and at compile time
(in strict mode), any code that sets the value of a Vector element or retrieves a value from a Vector is checked. If the
data type of the object being added or retrieved doesn’t match the Vector’s base type, an error occurs.
In addition to the data type restriction, the Vector class has other restrictions that distinguish it from the Array class:

• A Vector is a dense array. An Array object may have values in indices 0 and 7 even if it has no values in positions 1
through 6. However, a Vector must have a value (or null) in each index.

• A Vector can optionally be fixed-length. This means that the number of elements the Vector contains can’t change.
• Access to a Vector’s elements is bounds-checked. You can never read a value from an index greater than the final
element (length - 1). You can never set a value with an index more than one beyond the current final index. (In
other words, you can only set a value at an existing index or at index [length].)

Last updated 3/21/2011

28

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

As a result of its restrictions, a Vector has three primary benefits over an Array instance whose elements are all
instances of a single class:

• Performance: array element access and iteration are much faster when using a Vector instance than when using an
Array instance.

• Type safety: in strict mode the compiler can identify data type errors. Examples of such errors include assigning a
value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector.
At run time, data types are also checked when adding data to or reading data from a Vector object. Note, however,
that when you use the push() method or unshift() method to add values to a Vector, the arguments’ data types
are not checked at compile time. When using those methods the values are still checked at run time.

• Reliability: runtime range checking (or fixed-length checking) increases reliability significantly over Arrays.
Aside from the additional restrictions and benefits, the Vector class is very much like the Array class. The properties
and methods of a Vector object are similar—for the most part identical—to the properties and methods of an Array.
In most situations where you would use an Array in which all the elements have the same data type, a Vector instance
is preferable.

Creating arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
You can use several techniques to create an Array instance or a Vector instance. However, the techniques to create each
type of array are somewhat different.

Creating an Array instance
Flash Player 9 and later, Adobe AIR 1.0 and later
You create an Array object by calling the Array() constructor or by using Array literal syntax.
The Array() constructor function can be used in three ways. First, if you call the constructor with no arguments, you
get an empty array. You can use the length property of the Array class to verify that the array has no elements. For
example, the following code calls the Array() constructor with no arguments:
var names:Array = new Array();
trace(names.length); // output: 0

Second, if you use a number as the only parameter to the Array() constructor, an array of that length is created, with
each element’s value set to undefined. The argument must be an unsigned integer between the values 0 and
4,294,967,295. For example, the following code calls the Array() constructor with a single numeric argument:
var names:Array = new Array(3);
trace(names.length); // output: 3
trace(names[0]); // output: undefined
trace(names[1]); // output: undefined
trace(names[2]); // output: undefined

Third, if you call the constructor and pass a list of elements as parameters, an array is created, with elements
corresponding to each of the parameters. The following code passes three arguments to the Array() constructor:
var names:Array = new Array("John", "Jane", "David");
trace(names.length); // output: 3
trace(names[0]); // output: John
trace(names[1]); // output: Jane
trace(names[2]); // output: David

Last updated 3/21/2011

29

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

You can also create arrays with Array literals. An Array literal can be assigned directly to an array variable, as shown
in the following example:
var names:Array = ["John", "Jane", "David"];

Creating a Vector instance
Flash Player 10 and later, Adobe AIR 1.5 and later
You create a Vector instance by calling the Vector.() constructor. You can also create a Vector by calling the
Vector.() global function. That function converts a specified object to a Vector instance. In Flash
Professional CS5 and later, Flash Builder 4 and later, and Flex 4 and later, you can also create a vector instance by using
Vector literal syntax.
Any time you declare a Vector variable (or similarly, a Vector method parameter or method return type) you specify
the base type of the Vector variable. You also specify the base type when you create a Vector instance by calling the
Vector.() constructor. Put another way, any time you use the term Vector in ActionScript, it is accompanied by
a base type.
You specify the Vector’s base type using type parameter syntax. The type parameter immediately follows the word
Vector in the code. It consists of a dot (.), then the base class name surrounded by angle brackets (<>), as shown in
this example:
var v:Vector.;
v = new Vector.();

In the first line of the example, the variable v is declared as a Vector. instance. In other words, it represents
an indexed array that can only hold String instances. The second line calls the Vector() constructor to create an
instance of the same Vector type (that is, a Vector whose elements are all String objects). It assigns that object to v.
Using the Vector.() constructor
If you use the Vector.() constructor without any arguments, it creates an empty Vector instance. You can test
that a Vector is empty by checking its length property. For example, the following code calls the Vector.()
constructor with no arguments:
var names:Vector. = new Vector.();
trace(names.length); // output: 0

If you know ahead of time how many elements a Vector initially needs, you can pre-define the number of elements in
the Vector. To create a Vector with a certain number of elements, pass the number of elements as the first parameter
(the length parameter). Because Vector elements can’t be empty, the elements are filled with instances of the base
type. If the base type is a reference type that allows null values, the elements all contain null. Otherwise, the elements
all contain the default value for the class. For example, a uint variable can’t be null. Consequently, in the following
code listing the Vector named ages is created with seven elements, each containing the value 0:
var ages:Vector. = new Vector.(7);
trace(ages); // output: 0,0,0,0,0,0,0

Finally, using the Vector.() constructor you can also create a fixed-length Vector by passing true for the second
parameter (the fixed parameter). In that case the Vector is created with the specified number of elements and the
number of elements can’t be changed. Note, however, that you can still change the values of the elements of a fixedlength Vector.

Last updated 3/21/2011

30

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Using the Vector literal syntax constructor
In Flash Professional CS5 and later, Flash Builder 4 and later, and Flex 4 and later, you can pass a list of values to the
Vector.() constructor to specify the Vector’s initial values:
// var v:Vector. = new [E0, ..., En-1 ,];
// For example:
var v:Vector. = new [0,1,2,];

The following information applies to this syntax:

• The trailing comma is optional.
• Empty items in the array are not supported; a statement such as var

v:Vector. = new [0,,2,]

throws a compiler error.

• You can't specify a default length for the Vector instance. Instead, the length is the same as the number of elements
in the initialization list.

• You can't specify whether the Vector instance has a fixed length. Instead, use the fixed property.
• Data loss or errors can occur if items passed as values don't match the specified type. For example:
var v:Vector. = new [4.2]; // compiler error when running in strict mode
trace(v[0]); //returns 4 when not running in strict mode

Using the Vector.() global function
In addition to the Vector.() and Vector literal syntax constructors, you can also use the Vector.() global
function to create a Vector object. The Vector.() global function is a conversion function. When you call the
Vector.() global function you specify the base type of the Vector that the method returns. You pass a single
indexed array (Array or Vector instance) as an argument. The method then returns a Vector with the specified base
type, containing the values in the source array argument. The following code listing shows the syntax for calling the
Vector.() global function:
var friends:Vector. = Vector.(["Bob", "Larry", "Sarah"]);

The Vector.() global function performs data type conversion on two levels. First, when an Array instance is
passed to the function, a Vector instance is returned. Second, whether the source array is an Array or Vector instance
the function attempts to convert the source array’s elements to values of the base type. The conversion uses standard
ActionScript data type conversion rules. For example, the following code listing converts the String values in the source
Array to integers in the result Vector. The decimal portion of the first value ("1.5") is truncated, and the non-numeric
third value ("Waffles") is converted to 0 in the result:
var numbers:Vector. = Vector.(["1.5", "17", "Waffles"]);
trace(numbers); // output: 1,17,0

If any of the source elements can’t be converted, an error occurs.
When code calls the Vector.() global function, if an element in the source array is an instance of a subclass of the
specified base type, the element is added to the result Vector (no error occurs). Using the Vector.() global
function is the only way to convert a Vector with base type T to a Vector with a base type that’s a superclass of T.

Inserting array elements
Flash Player 9 and later, Adobe AIR 1.0 and later
The most basic way to add an element to an indexed array is to use the array access ([]) operator. To set the value of
an indexed array element, use the Array or Vector object name and index number on the left side of an assignment
statement:

Last updated 3/21/2011

31

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

songTitles[5] = "Happy Birthday";

If the Array or Vector doesn’t already have an element at that index, the index is created and the value is stored there.
If a value exists at that index, the new value replaces the existing one.
An Array object allows you to create an element at any index. However, with a Vector object you can only assign a
value to an existing index or to the next available index. The next available index corresponds to the Vector object’s
length property. The safest way to add a new element to a Vector object is to use code like this listing:
myVector[myVector.length] = valueToAdd;

Three of the Array and Vector class methods—push(), unshift(), and splice()—allow you to insert elements into
an indexed array. The push() method appends one or more elements to the end of an array. In other words, the last
element inserted into the array using the push() method will have the highest index number. The unshift() method
inserts one or more elements at the beginning of an array, which is always at index number 0. The splice() method
will insert any number of items at a specified index in the array.
The following example demonstrates all three methods. An array named planets is created to store the names of the
planets in order of proximity to the Sun. First, the push() method is called to add the initial item, Mars. Second, the
unshift() method is called to insert the item that belongs at the front of the array, Mercury. Finally, the splice()
method is called to insert the items Venus and Earth after Mercury, but before Mars. The first argument sent to
splice(), the integer 1, directs the insertion to begin at index 1. The second argument sent to splice(), the integer
0, indicates that no items should be deleted. Finally, the third and fourth arguments sent to splice(), Venus and
Earth, are the items to be inserted.
var planets:Array = new Array();
planets.push("Mars"); // array contents: Mars
planets.unshift("Mercury"); // array contents: Mercury,Mars
planets.splice(1, 0, "Venus", "Earth");
trace(planets); // array contents: Mercury,Venus,Earth,Mars

The push() and unshift() methods both return an unsigned integer that represents the length of the modified array.
The splice() method returns an empty array when used to insert elements, which may seem strange, but makes more
sense in light of the splice() method’s versatility. You can use the splice() method not only to insert elements into
an array, but also to remove elements from an array. When used to remove elements, the splice() method returns
an array containing the elements removed.
Note: If a Vector object’s fixed property is true, the total number of elements in the Vector can’t change. If you try to
add a new element to a fixed-length Vector using the techniques described here, an error occurs.

Retrieving values and removing array elements
Flash Player 9 and later, Adobe AIR 1.0 and later
The simplest way to retrieve the value of an element from an indexed array is to use the array access ([]) operator. To
retrieve the value of an indexed array element, use the Array or Vector object name and index number on the right
side of an assignment statement:
var myFavoriteSong:String = songTitles[3];

It’s possible to attempt to retrieve a value from an Array or Vector using an index where no element exists. In that case,
an Array object returns the value undefined and a Vector throws a RangeError exception.

Last updated 3/21/2011

32

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Three methods of the Array and Vector classes—pop(), shift(), and splice()—allow you to remove elements. The
pop() method removes an element from the end of the array. In other words, it removes the element at the highest
index number. The shift() method removes an element from the beginning of the array, which means that it always
removes the element at index number 0. The splice() method, which can also be used to insert elements, removes
an arbitrary number of elements starting at the index number specified by the first argument sent to the method.
The following example uses all three methods to remove elements from an Array instance. An Array named oceans
is created to store the names of large bodies of water. Some of the names in the Array are lakes rather than oceans, so
they need to be removed.
First, the splice() method is used to remove the items Aral and Superior, and insert the items Atlantic and
Indian. The first argument sent to splice(), the integer 2, indicates that the operation should start with the third
item in the list, which is at index 2. The second argument, 2, indicates that two items should be removed. The
remaining arguments, Atlantic and Indian, are values to be inserted at index 2.
Second, the pop() method is used to remove last element in the array, Huron. And third, the shift() method is used
to remove the first item in the array, Victoria.
var oceans:Array = ["Victoria", "Pacific", "Aral", "Superior", "Indian", "Huron"];
oceans.splice(2, 2, "Arctic", "Atlantic"); // replaces Aral and Superior
oceans.pop(); // removes Huron
oceans.shift(); // removes Victoria
trace(oceans);// output: Pacific,Arctic,Atlantic,Indian

The pop() and shift() methods both return the item that was removed. For an Array instance, the data type of the
return value is Object because arrays can hold values of any data type. For a Vector instance, the data type of the return
value is the base type of the Vector. The splice() method returns an Array or Vector containing the values removed.
You can change the oceans Array example so that the call to splice() assigns the returned Array to a new Array
variable, as shown in the following example:
var lakes:Array = oceans.splice(2, 2, "Arctic", "Atlantic");
trace(lakes); // output: Aral,Superior

You may come across code that uses the delete operator on an Array object element. The delete operator sets the
value of an Array element to undefined, but it does not remove the element from the Array. For example, the
following code uses the delete operator on the third element in the oceans Array, but the length of the Array remains
5:
var oceans:Array = ["Arctic", "Pacific", "Victoria", "Indian", "Atlantic"];
delete oceans[2];
trace(oceans);// output: Arctic,Pacific,,Indian,Atlantic
trace(oceans[2]); // output: undefined
trace(oceans.length); // output: 5

You can truncate an Array or Vector using an array’s length property. If you set the length property of an indexed
array to a length that is less than the current length of the array, the array is truncated, removing any elements stored
at index numbers higher than the new value of length minus 1. For example, if the oceans array were sorted such that
all valid entries were at the beginning of the array, you could use the length property to remove the entries at the end
of the array, as shown in the following code:
var oceans:Array = ["Arctic", "Pacific", "Victoria", "Aral", "Superior"];
oceans.length = 2;
trace(oceans); // output: Arctic,Pacific

Note: If a Vector object’s fixed property is true, the total number of elements in the Vector can’t change. If you try to
remove an element from or truncate a fixed-length Vector using the techniques described here, an error occurs.

Last updated 3/21/2011

33

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Sorting an array
Flash Player 9 and later, Adobe AIR 1.0 and later
There are three methods—reverse(), sort(), and sortOn()—that allow you to change the order of an indexed
array, either by sorting or reversing the order. All of these methods modify the existing array. The following table
summarizes these methods and their behavior for Array and Vector objects:
Method

Array behavior

Vector behavior

reverse()

Changes the order of the elements so that the last element becomes the Identical to Array behavior
first element, the penultimate element becomes the second, and so on

sort()

Allows you to sort the Array’s elements in a variety of predefined ways,
such as alphabetical or numeric order. You can also specify a custom
sorting algorithm.

Sorts the elements according to the custom
sorting algorithm that you specify

sortOn()

Allows you to sort objects that have one or more common properties,
specifying the property or properties to use as the sort keys

Not available in the Vector class

The reverse() method
The reverse() method takes no parameters and does not return a value, but allows you to toggle the order of your
array from its current state to the reverse order. The following example reverses the order of the oceans listed in the
oceans array:
var oceans:Array = ["Arctic", "Atlantic", "Indian", "Pacific"];
oceans.reverse();
trace(oceans); // output: Pacific,Indian,Atlantic,Arctic

Basic sorting with the sort() method (Array class only)
For an Array instance, the sort() method rearranges the elements in an array using the default sort order. The default
sort order has the following characteristics:

• The sort is case-sensitive, which means that uppercase characters precede lowercase characters. For example, the
letter D precedes the letter b.

• The sort is ascending, which means that lower character codes (such as A) precede higher character codes (such as B).
• The sort places identical values adjacent to each other but in no particular order.
• The sort is string-based, which means that elements are converted to strings before they are compared (for example,
10 precedes 3 because the string "1" has a lower character code than the string "3" has).
You may find that you need to sort your Array without regard to case, or in descending order, or perhaps your array
contains numbers that you want to sort numerically instead of alphabetically. The Array class’s sort() method has an
options parameter that allows you to alter each characteristic of the default sort order. The options are defined by a
set of static constants in the Array class, as shown in the following list:

•

Array.CASEINSENSITIVE: This option makes the sort disregard case. For example, the lowercase letter b precedes

the uppercase letter D.

•

Array.DESCENDING: This reverses the default ascending sort. For example, the letter B precedes the letter A.

•

Array.UNIQUESORT: This causes the sort to abort if two identical values are found.

•

Array.NUMERIC: This causes numerical sorting, so that 3 precedes 10.

The following example highlights some of these options. An Array named poets is created that is sorted using several
different options.

Last updated 3/21/2011

34

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

var poets:Array = ["Blake", "cummings", "Angelou", "Dante"];
poets.sort(); // default sort
trace(poets); // output: Angelou,Blake,Dante,cummings
poets.sort(Array.CASEINSENSITIVE);
trace(poets); // output: Angelou,Blake,cummings,Dante
poets.sort(Array.DESCENDING);
trace(poets); // output: cummings,Dante,Blake,Angelou
poets.sort(Array.DESCENDING | Array.CASEINSENSITIVE); // use two options
trace(poets); // output: Dante,cummings,Blake,Angelou

Custom sorting with the sort() method (Array and Vector classes)
In addition to the basic sorting that’s available for an Array object, you can also define a custom sorting rule. This
technique is the only form of the sort() method that is available for the Vector class. To define a custom sort, you
write a custom sort function and pass it as an argument to the sort() method.
For example, if you have a list of names in which each list element contains a person’s full name, but you want to sort
the list by last name, you must use a custom sort function to parse each element and use the last name in the sort
function. The following code shows how this can be done with a custom function that is used as a parameter to the
Array.sort() method:
var names:Array = new Array("John Q. Smith", "Jane Doe", "Mike Jones");
function orderLastName(a, b):int
{
var lastName:RegExp = /\b\S+$/;
var name1 = a.match(lastName);
var name2 = b.match(lastName);
if (name1 < name2)
{
return -1;
}
else if (name1 > name2)
{
return 1;
}
else
{
return 0;
}
}
trace(names); // output: John Q. Smith,Jane Doe,Mike Jones
names.sort(orderLastName);
trace(names); // output: Jane Doe,Mike Jones,John Q. Smith

The custom sort function orderLastName() uses a regular expression to extract the last name from each element to
use for the comparison operation. The function identifier orderLastName is used as the sole parameter when calling
the sort() method on the names array. The sort function accepts two parameters, a and b, because it works on two
array elements at a time. The sort function’s return value indicates how the elements should be sorted:

• A return value of -1 indicates that the first parameter, a, precedes the second parameter, b.
• A return value of 1 indicates that the second parameter, b, precedes the first, a.
• A return value of 0 indicates that the elements have equal sorting precedence.

Last updated 3/21/2011

35

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

The sortOn() method (Array class only)
The sortOn() method is designed for Array objects with elements that contain objects. These objects are expected to
have at least one common property that can be used as the sort key. The use of the sortOn() method for arrays of any
other type yields unexpected results.
Note: The Vector class does not include a sortOn() method. This method is only available for Array objects.
The following example revises the poets Array so that each element is an object instead of a string. Each object holds
both the poet’s last name and year of birth.
var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});

You can use the sortOn() method to sort the Array by the born property. The sortOn() method defines two
parameters, fieldName and options. The fieldName argument must be specified as a string. In the following
example, sortOn() is called with two arguments, "born" and Array.NUMERIC. The Array.NUMERIC argument is used
to ensure that the sort is done numerically instead of alphabetically. This is a good practice even when all the numbers
have the same number of digits because it ensures that the sort will continue to behave as expected if a number with
fewer or more digits is later added to the array.
poets.sortOn("born", Array.NUMERIC);
for (var i:int = 0; i < poets.length; ++i)
{
trace(poets[i].name, poets[i].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/

Sorting without modifying the original array (Array class only)
Generally, the sort() and sortOn() methods modify an Array. If you wish to sort an Array without modifying the
existing array, pass the Array.RETURNINDEXEDARRAY constant as part of the options parameter. This option directs
the methods to return a new Array that reflects the sort and to leave the original Array unmodified. The Array returned
by the methods is a simple Array of index numbers that reflects the new sort order and does not contain any elements
from the original Array. For example, to sort the poets Array by birth year without modifying the Array, include the
Array.RETURNINDEXEDARRAY constant as part of the argument passed for the options parameter.
The following example stores the returned index information in an Array named indices and uses the indices array
in conjunction with the unmodified poets array to output the poets in order of birth year:

Last updated 3/21/2011

36

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

var indices:Array;
indices = poets.sortOn("born", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
for (var i:int = 0; i < indices.length; ++i)
{
var index:int = indices[i];
trace(poets[index].name, poets[index].born);
}
/* output:
Wang 701
Dante 1265
Blake 1757
cummings 1894
Angelou 1928
*/

Querying an array
Flash Player 9 and later, Adobe AIR 1.0 and later
Four methods of the Array and Vector classes—concat(), join(), slice(), and toString()—all query the array
for information, but do not modify the array. The concat() and slice() methods both return new arrays, while the
join() and toString() methods both return strings. The concat() method takes a new array or list of elements as
arguments and combines it with the existing array to create a new array. The slice() method has two parameters,
aptly named startIndex and an endIndex, and returns a new array containing a copy of the elements “sliced” from
the existing array. The slice begins with the element at startIndex and ends with the element just before endIndex.
That bears repeating: the element at endIndex is not included in the return value.
The following example uses concat() and slice() to create new arrays using elements of other arrays:
var array1:Array = ["alpha", "beta"];
var array2:Array = array1.concat("gamma", "delta");
trace(array2); // output: alpha,beta,gamma,delta
var array3:Array = array1.concat(array2);
trace(array3); // output: alpha,beta,alpha,beta,gamma,delta
var array4:Array = array3.slice(2,5);
trace(array4); // output: alpha,beta,gamma

You can use the join() and toString() methods to query the array and return its contents as a string. If no
parameters are used for the join() method, the two methods behave identically—they return a string containing a
comma-delimited list of all elements in the array. The join() method, unlike the toString() method, accepts a
parameter named delimiter, which allows you to choose the symbol to use as a separator between each element in
the returned string.
The following example creates an Array called rivers and calls both join() and toString() to return the values in
the Array as a string. The toString() method is used to return comma-separated values (riverCSV), while the
join() method is used to return values separated by the + character.
var rivers:Array = ["Nile", "Amazon", "Yangtze", "Mississippi"];
var riverCSV:String = rivers.toString();
trace(riverCSV); // output: Nile,Amazon,Yangtze,Mississippi
var riverPSV:String = rivers.join("+");
trace(riverPSV); // output: Nile+Amazon+Yangtze+Mississippi

Last updated 3/21/2011

37

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

One issue to be aware of with the join() method is that any nested Array or Vector instances are always returned with
comma-separated values, no matter what separator you specify for the main array elements, as the following example
shows:
var nested:Array = ["b","c","d"];
var letters:Array = ["a",nested,"e"];
var joined:String = letters.join("+");
trace(joined); // output: a+b,c,d+e

Associative arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
An associative array, sometimes called a hash or map, uses keys instead of a numeric index to organize stored values.
Each key in an associative array is a unique string that is used to access a stored value. An associative array is an
instance of the Object class, which means that each key corresponds to a property name. Associative arrays are
unordered collections of key and value pairs. Your code should not expect the keys of an associative array to be in a
specific order.
ActionScript 3.0 also includes an advanced type of associative array called a dictionary. Dictionaries, which are
instances of the Dictionary class in the flash.utils package, use keys that can be of any data type. In other words,
dictionary keys are not limited to values of type String.

Associative arrays with string keys
Flash Player 9 and later, Adobe AIR 1.0 and later
There are two ways to create associative arrays in ActionScript 3.0. The first way is to use an Object instance. By using
an Object instance you can initialize your array with an object literal. An instance of the Object class, also called a
generic object, is functionally identical to an associative array. Each property name of the generic object serves as the
key that provides access to a stored value.
The following example creates an associative array named monitorInfo, using an object literal to initialize the array
with two key and value pairs:
var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
trace(monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200

If you do not need to initialize the array at declaration time, you can use the Object constructor to create the array, as
follows:
var monitorInfo:Object = new Object();

After the array is created using either an object literal or the Object class constructor, you can add new values to the
array using either the array access ([]) operator or the dot operator (.). The following example adds two new values
to monitorArray:
monitorInfo["aspect ratio"] = "16:10"; // bad form, do not use spaces
monitorInfo.colors = "16.7 million";
trace(monitorInfo["aspect ratio"], monitorInfo.colors);
// output: 16:10 16.7 million

Last updated 3/21/2011

38

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Note that the key named aspect ratio contains a space character. This is possible with the array access ([]) operator,
but generates an error if attempted with the dot operator. Using spaces in your key names is not recommended.
The second way to create an associative array is to use the Array constructor (or the constructor of any dynamic class)
and then use either the array access ([]) operator or the dot operator (.) to add key and value pairs to the array. If you
declare your associative array to be of type Array, you cannot use an object literal to initialize the array. The following
example creates an associative array named monitorInfo using the Array constructor and adds a key called type and
a key called resolution, along with their values:
var monitorInfo:Array = new Array();
monitorInfo["type"] = "Flat Panel";
monitorInfo["resolution"] = "1600 x 1200";
trace(monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200

There is no advantage in using the Array constructor to create an associative array. You cannot use the Array.length
property or any of the methods of the Array class with associative arrays, even if you use the Array constructor or the
Array data type. The use of the Array constructor is best left for the creation of indexed arrays.

Associative arrays with object keys (Dictionaries)
Flash Player 9 and later, Adobe AIR 1.0 and later
You can use the Dictionary class to create an associative array that uses objects for keys rather than strings. Such arrays
are sometimes called dictionaries, hashes, or maps. For example, consider an application that determines the location
of a Sprite object based on its association with a specific container. You can use a Dictionary object to map each Sprite
object to a container.
The following code creates three instances of the Sprite class that serve as keys for the Dictionary object. Each key is
assigned a value of either GroupA or GroupB. The values can be of any data type, but in this example both GroupA and
GroupB are instances of the Object class. Subsequently, you can access the value associated with each key with the array
access ([]) operator, as shown in the following code:

Last updated 3/21/2011

39

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

import flash.display.Sprite;
import flash.utils.Dictionary;
var groupMap:Dictionary = new Dictionary();
// objects to use
var spr1:Sprite =
var spr2:Sprite =
var spr3:Sprite =

as keys
new Sprite();
new Sprite();
new Sprite();

// objects to use as values
var groupA:Object = new Object();
var groupB:Object = new Object();
// Create new key-value pairs in dictionary.
groupMap[spr1] = groupA;
groupMap[spr2] = groupB;
groupMap[spr3] = groupB;
if (groupMap[spr1]
{
trace("spr1 is
}
if (groupMap[spr2]
{
trace("spr2 is
}
if (groupMap[spr3]
{
trace("spr3 is
}

== groupA)
in groupA");
== groupB)
in groupB");
== groupB)
in groupB");

Iterating with object keys
You can iterate through the contents of a Dictionary object with either a for..in loop or a for each..in loop. A
for..in loop allows you to iterate based on the keys, whereas a for each..in loop allows you to iterate based on the
values associated with each key.
Use the for..in loop for direct access to the object keys of a Dictionary object. You can also access the values of the
Dictionary object with the array access ([]) operator. The following code uses the previous example of the groupMap
dictionary to show how to iterate through a Dictionary object with the for..in loop:
for (var key:Object in groupMap)
{
trace(key, groupMap[key]);
}
/* output:
[object Sprite] [object Object]
[object Sprite] [object Object]
[object Sprite] [object Object]
*/

Use the for each..in loop for direct access to the values of a Dictionary object. The following code also uses the
groupMap dictionary to show how to iterate through a Dictionary object with the for each..in loop:

Last updated 3/21/2011

40

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

for each (var item:Object in groupMap)
{
trace(item);
}
/* output:
[object Object]
[object Object]
[object Object]
*/

Object keys and memory management
Adobe® Flash® Player and Adobe® AIR™ use a garbage collection system to recover memory that is no longer used.
When an object has no references pointing to it, the object becomes eligible for garbage collection, and the memory is
recovered the next time the garbage collection system executes. For example, the following code creates a new object
and assigns a reference to the object to the variable myObject:
var myObject:Object = new Object();

As long as any reference to the object exists, the garbage collection system will not recover the memory that the object
occupies. If the value of myObject is changed such that it points to a different object or is set to the value null, the
memory occupied by the original object becomes eligible for garbage collection, but only if there are no other
references to the original object.
If you use myObject as a key in a Dictionary object, you are creating another reference to the original object. For
example, the following code creates two references to an object—the myObject variable, and the key in the myMap
object:
import flash.utils.Dictionary;
var myObject:Object = new Object();
var myMap:Dictionary = new Dictionary();
myMap[myObject] = "foo";

To make the object referenced by myObject eligible for garbage collection, you must remove all references to it. In this
case, you must change the value of myObject and delete the myObject key from myMap, as shown in the following code:
myObject = null;
delete myMap[myObject];

Alternatively, you can use the useWeakReference parameter of the Dictionary constructor to make all of the
dictionary keys weak references. The garbage collection system ignores weak references, which means that an object
that has only weak references is eligible for garbage collection. For example, in the following code, you do not need to
delete the myObject key from myMap in order to make the object eligible for garbage collection:
import flash.utils.Dictionary;
var myObject:Object = new Object();
var myMap:Dictionary = new Dictionary(true);
myMap[myObject] = "foo";
myObject = null; // Make object eligible for garbage collection.

Last updated 3/21/2011

41

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Multidimensional arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
Multidimensional arrays contain other arrays as elements. For example, consider a list of tasks that is stored as an
indexed array of strings:
var tasks:Array = ["wash dishes", "take out trash"];

If you want to store a separate list of tasks for each day of the week, you can create a multidimensional array with one
element for each day of the week. Each element contains an indexed array, similar to the tasks array, that stores the
list of tasks. You can use any combination of indexed or associative arrays in multidimensional arrays. The examples
in the following sections use either two indexed arrays or an associative array of indexed arrays. You might want to try
the other combinations as exercises.

Two indexed arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
When you use two indexed arrays, you can visualize the result as a table or spreadsheet. The elements of the first array
represent the rows of the table, while the elements of the second array represent the columns.
For example, the following multidimensional array uses two indexed arrays to track task lists for each day of the week.
The first array, masterTaskList, is created using the Array class constructor. Each element of the array represents a
day of the week, with index 0 representing Monday, and index 6 representing Sunday. These elements can be thought
of as the rows in the table. You can create each day’s task list by assigning an array literal to each of the seven elements
that you create in the masterTaskList array. The array literals represent the columns in the table.
var masterTaskList:Array = new Array();
masterTaskList[0] = ["wash dishes", "take out trash"];
masterTaskList[1] = ["wash dishes", "pay bills"];
masterTaskList[2] = ["wash dishes", "dentist", "wash dog"];
masterTaskList[3] = ["wash dishes"];
masterTaskList[4] = ["wash dishes", "clean house"];
masterTaskList[5] = ["wash dishes", "wash car", "pay rent"];
masterTaskList[6] = ["mow lawn", "fix chair"];

You can access individual items on any of the task lists using the array access ([]) operator. The first set of brackets
represents the day of the week, and the second set of brackets represents the task list for that day. For example, to
retrieve the second task from Wednesday’s list, first use index 2 for Wednesday, and then use index 1 for the second
task in the list.
trace(masterTaskList[2][1]); // output: dentist

To retrieve the first task from Sunday’s list, use index 6 for Sunday and index 0 for the first task on the list.
trace(masterTaskList[6][0]); // output: mow lawn

Last updated 3/21/2011

42

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Associative array with an indexed array
Flash Player 9 and later, Adobe AIR 1.0 and later
To make the individual arrays easier to access, you can use an associative array for the days of the week and an indexed
array for the task lists. Using an associative array allows you to use dot syntax when referring to a particular day of the
week, but at the cost of extra run-time processing to access each element of the associative array. The following
example uses an associative array as the basis of a task list, with a key and value pair for each day of the week:
var masterTaskList:Object = new Object();
masterTaskList["Monday"] = ["wash dishes", "take out trash"];
masterTaskList["Tuesday"] = ["wash dishes", "pay bills"];
masterTaskList["Wednesday"] = ["wash dishes", "dentist", "wash dog"];
masterTaskList["Thursday"] = ["wash dishes"];
masterTaskList["Friday"] = ["wash dishes", "clean house"];
masterTaskList["Saturday"] = ["wash dishes", "wash car", "pay rent"];
masterTaskList["Sunday"] = ["mow lawn", "fix chair"];

Dot syntax makes the code more readable by making it possible to avoid multiple sets of brackets.
trace(masterTaskList.Wednesday[1]); // output: dentist
trace(masterTaskList.Sunday[0]);// output: mow lawn

You can iterate through the task list using a for..in loop, but you must use the array access ([]) operator instead of
dot syntax to access the value associated with each key. Because masterTaskList is an associative array, the elements
are not necessarily retrieved in the order that you may expect, as the following example shows:
for (var day:String in masterTaskList)
{
trace(day + ": " + masterTaskList[day])
}
/* output:
Sunday: mow lawn,fix chair
Wednesday: wash dishes,dentist,wash dog
Friday: wash dishes,clean house
Thursday: wash dishes
Monday: wash dishes,take out trash
Saturday: wash dishes,wash car,pay rent
Tuesday: wash dishes,pay bills
*/

Cloning arrays
Flash Player 9 and later, Adobe AIR 1.0 and later
The Array class has no built-in method for making copies of arrays. You can create a shallowcopy of an array by calling
either the concat() or slice() methods with no arguments. In a shallow copy, if the original array has elements that
are objects, only the references to the objects are copied rather than the objects themselves. The copy points to the same
objects as the original does. Any changes made to the objects are reflected in both arrays.
In a deep copy, any objects found in the original array are also copied so that the new array does not point to the same
objects as does the original array. Deep copying requires more than one line of code, which usually calls for the creation
of a function. Such a function could be created as a global utility function or as a method of an Array subclass.

Last updated 3/21/2011

43

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

The following example defines a function named clone() that does deep copying. The algorithm is borrowed from a
common Java programming technique. The function creates a deep copy by serializing the array into an instance of
the ByteArray class, and then reading the array back into a new array. This function accepts an object so that it can be
used with both indexed arrays and associative arrays, as shown in the following code:
import flash.utils.ByteArray;
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}

Extending the Array class
Flash Player 9 and later, Adobe AIR 1.0 and later
The Array class is one of the few core classes that is not final, which means that you can create your own subclass of
Array. This section provides an example of how to create a subclass of Array and discusses some of the issues that can
arise during the process.
As mentioned previously, arrays in ActionScript are not typed, but you can create a subclass of Array that accepts
elements of only a specific data type. The example in the following sections defines an Array subclass named
TypedArray that limits its elements to values of the data type specified in the first parameter. The TypedArray class is
presented merely as an example of how to extend the Array class and may not be suitable for production purposes for
several reasons. First, type checking occurs at run time rather than at compile time. Second, when a TypedArray
method encounters a mismatch, the mismatch is ignored and no exception is thrown, although the methods can be
easily modified to throw exceptions. Third, the class cannot prevent the use of the array access operator to insert values
of any type into the array. Fourth, the coding style favors simplicity over performance optimization.
Note: You can use the technique described here to create a typed array. However, a better approach is to use a Vector
object. A Vector instance is a true typed array, and provides performance and other improvements over the Array class
or any subclass. The purpose of this discussion is to demonstrate how to create an Array subclass.
Declaring the subclass
Use the extends keyword to indicate that a class is a subclass of Array. A subclass of Array should use the dynamic
attribute, just as the Array class does. Otherwise, your subclass will not function properly.
The following code shows the definition of the TypedArray class, which contains a constant to hold the data type, a
constructor method, and the four methods that are capable of adding elements to the array. The code for each method
is omitted in this example, but is delineated and explained fully in the sections that follow:

Last updated 3/21/2011

44

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

public dynamic class TypedArray extends Array
{
private const dataType:Class;
public function TypedArray(...args) {}
AS3 override function concat(...args):Array {}
AS3 override function push(...args):uint {}
AS3 override function splice(...args) {}
AS3 override function unshift(...args):uint {}
}

The four overridden methods all use the AS3 namespace instead of the public attribute because this example assumes
that the compiler option -as3 is set to true and the compiler option -es is set to false. These are the default settings
for Adobe Flash Builder and for AdobeFlashProfessional.
If you are an advanced developer who prefers to use prototype inheritance, you can make two minor changes to the
TypedArray class to make it compile with the compiler option -es set to true. First, remove all occurrences of the
override attribute and replace the AS3 namespace with the public attribute. Second, substitute Array.prototype for
all four occurrences of super.
TypedArray constructor
The subclass constructor poses an interesting challenge because the constructor must accept a list of arguments of
arbitrary length. The challenge is how to pass the arguments on to the superconstructor to create the array. If you pass
the list of arguments as an array, the superconstructor considers it a single argument of type Array and the resulting
array is always 1 element long. The traditional way to handle pass-through argument lists is to use the
Function.apply() method, which takes an array of arguments as its second parameter but converts it to a list of
arguments when executing the function. Unfortunately, the Function.apply() method cannot be used with
constructors.
The only option left is to recreate the logic of the Array constructor in the TypedArray constructor. The following code
shows the algorithm used in the Array class constructor, which you can reuse in your Array subclass constructor:

Last updated 3/21/2011

45

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

public dynamic class Array
{
public function Array(...args)
{
var n:uint = args.length
if (n == 1 && (args[0] is Number))
{
var dlen:Number = args[0];
var ulen:uint = dlen;
if (ulen != dlen)
{
throw new RangeError("Array index is not a 32-bit unsigned integer ("+dlen+")");
}
length = ulen;
}
else
{
length = n;
for (var i:int=0; i < n; i++)
{
this[i] = args[i]
}
}
}
}

The TypedArray constructor shares most of the code from the Array constructor, with only four changes to the code.
First, the parameter list includes a new required parameter of type Class that allows specification of the array’s data
type. Second, the data type passed to the constructor is assigned to the dataType variable. Third, in the else
statement, the value of the length property is assigned after the for loop so that length includes only arguments that
are the proper type. Fourth, the body of the for loop uses the overridden version of the push() method so that only
arguments of the correct data type are added to the array. The following example shows the TypedArray constructor
function:

Last updated 3/21/2011

46

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

public dynamic class TypedArray extends Array
{
private var dataType:Class;
public function TypedArray(typeParam:Class, ...args)
{
dataType = typeParam;
var n:uint = args.length
if (n == 1 && (args[0] is Number))
{
var dlen:Number = args[0];
var ulen:uint = dlen
if (ulen != dlen)
{
throw new RangeError("Array index is not a 32-bit unsigned integer ("+dlen+")")
}
length = ulen;
}
else
{
for (var i:int=0; i < n; i++)
{
// type check done in push()
this.push(args[i])
}
length = this.length;
}
}
}

TypedArray overridden methods
The TypedArray class overrides the four methods of the Array class that are capable of adding elements to an array. In
each case, the overridden method adds a type check that prevents the addition of elements that are not the correct data
type. Subsequently, each method calls the superclass version of itself.
The push() method iterates through the list of arguments with a for..in loop and does a type check on each
argument. Any argument that is not the correct type is removed from the args array with the splice() method. After
the for..in loop ends, the args array contains values only of type dataType. The superclass version of push() is then
called with the updated args array, as the following code shows:
AS3 override function push(...args):uint
{
for (var i:* in args)
{
if (!(args[i] is dataType))
{
args.splice(i,1);
}
}
return (super.push.apply(this, args));
}

The concat() method creates a temporary TypedArray named passArgs to store the arguments that pass the type
check. This allows the reuse of the type check code that exists in the push() method. A for..in loop iterates through
the args array, and calls push() on each argument. Because passArgs is typed as TypedArray, the TypedArray
version of push() is executed. The concat() method then calls its own superclass version, as the following code
shows:

Last updated 3/21/2011

47

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

AS3 override function concat(...args):Array
{
var passArgs:TypedArray = new TypedArray(dataType);
for (var i:* in args)
{
// type check done in push()
passArgs.push(args[i]);
}
return (super.concat.apply(this, passArgs));
}

The splice() method takes an arbitrary list of arguments, but the first two arguments always refer to an index
number and the number of elements to delete. This is why the overridden splice() method does type checking only
for args array elements in index positions 2 or higher. One point of interest in the code is that there appears to be a
recursive call to splice() inside the for loop, but this is not a recursive call because args is of type Array rather than
TypedArray, which means that the call to args.splice() is a call to the superclass version of the method. After the
for..in loop concludes, the args array contains only values of the correct type in index positions 2 or higher, and
splice() calls its own superclass version, as shown in the following code:
AS3 override function splice(...args):*
{
if (args.length > 2)
{
for (var i:int=2; i< args.length; i++)
{
if (!(args[i] is dataType))
{
args.splice(i,1);
}
}
}
return (super.splice.apply(this, args));
}

The unshift() method, which adds elements to the beginning of an array, also accepts an arbitrary list of arguments.
The overridden unshift() method uses an algorithm very similar to that used by the push() method, as shown in
the following example code:
AS3 override function unshift(...args):uint
{
for (var i:* in args)
{
if (!(args[i] is dataType))
{
args.splice(i,1);
}
}
return (super.unshift.apply(this, args));
}
}

Last updated 3/21/2011

48

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Arrays example: PlayList
Flash Player 9 and later, Adobe AIR 1.0 and later
The PlayList example demonstrates techniques for working with arrays, in the context of a music playlist application
that manages a list of songs. These techniques are:

• Creating an indexed array
• Adding items to an indexed array
• Sorting an array of objects by different properties, using different sorting options
• Converting an array to a character-delimited string
To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The
PlayList application files can be found in the Samples/PlayList folder. The application consists of the following files:
File

Description

PlayList.mxml

The main application file in Flash (FLA) or Flex (MXML).

or
PlayList.fla
com/example/programmingas3/playlist/PlayList.as

A class representing a list of songs. It uses an Array to store the list,
and manages the sorting of the list’s items..

com/example/programmingas3/playlist/Song.as

A value object representing information about a single song. The
items that are managed by the PlayList class are Song instances.

com/example/programmingas3/playlist/SortProperty.as

A pseudo-enumeration whose available values represent the
properties of the Song class by which a list of Song objects can be
sorted.

PlayList class overview
Flash Player 9 and later, Adobe AIR 1.0 and later
The PlayList class manages a set of Song objects. It has public methods with functionality for adding a song to the
playlist (the addSong() method) and sorting the songs in the list (the sortList() method). In addition, the class
includes a read-only accessor property, songList, which provides access to the actual set of songs in the playlist.
Internally, the PlayList class keeps track of its songs using a private Array variable:
public class PlayList
{
private var _songs:Array;
private var _currentSort:SortProperty = null;
private var _needToSort:Boolean = false;
...
}

In addition to the _songs Array variable, which is used by the PlayList class to keep track of its list of songs, two other
private variables keep track of whether the list needs to be sorted (_needToSort) and which property the song list is
sorted by at a given time (_currentSort).
As with all objects, declaring an Array instance is only half the job of creating an Array. Before accessing an Array
instance’s properties or methods, it must be instantiated, which is done in the PlayList class’s constructor.

Last updated 3/21/2011

49

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

public function PlayList()
{
this._songs = new Array();
// Set the initial sorting.
this.sortList(SortProperty.TITLE);
}

The first line of the constructor instantiates the _songs variable, so that it is ready to be used. In addition, the
sortList() method is called to set the initial sort-by property.

Adding a song to the list
Flash Player 9 and later, Adobe AIR 1.0 and later
When a user enters a new song into the application, the code in the data entry form calls the PlayList class’s addSong()
method.
/**
* Adds a song to the playlist.
*/
public function addSong(song:Song):void
{
this._songs.push(song);
this._needToSort = true;
}

Inside addSong(), the _songs array’s push() method is called, adding the Song object that was passed to addSong()
as a new element in that array. With the push() method, the new element is added to the end of the array, regardless
of any sorting that might have been applied previously. This means that after the push() method has been called, the
list of songs is likely to no longer be sorted correctly, so the _needToSort variable is set to true. In theory, the
sortList() method could be called immediately, removing the need to keep track of whether the list is sorted or not
at a given time. In practice, however, there is no need for the list of songs to be sorted until immediately before it is
retrieved. By deferring the sorting operation, the application doesn’t perform sorting that is unnecessary if, for
example, several songs are added to the list before it is retrieved.

Sorting the list of songs
Flash Player 9 and later, Adobe AIR 1.0 and later
Because the Song instances that are managed by the playlist are complex objects, users of the application may wish to
sort the playlist according to different properties, such as song title or year of publication. In the PlayList application,
the task of sorting the list of songs has three parts: identifying the property by which the list should be sorted, indicating
what sorting options need to be used when sorting by that property, and performing the actual sort operation.
Properties for sorting
A Song object keeps track of several properties, including song title, artist, publication year, filename, and a userselected set of genres in which the song belongs. Of these, only the first three are practical for sorting. As a matter of
convenience for developers, the example includes the SortProperty class, which acts as an enumeration with values
representing the properties available for sorting.
public static const TITLE:SortProperty = new SortProperty("title");
public static const ARTIST:SortProperty = new SortProperty("artist");
public static const YEAR:SortProperty = new SortProperty("year");

Last updated 3/21/2011

50

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

The SortProperty class contain three constants, TITLE, ARTIST, and YEAR, each of which stores a String containing the
actual name of the associated Song class property that can be used for sorting. Throughout the rest of the code,
whenever a sort property is indicated, it is done using the enumeration member. For instance, in the PlayList
constructor, the list is sorted initially by calling the sortList() method, as follows:
// Set the initial sorting.
this.sortList(SortProperty.TITLE);

Because the property for sorting is specified as SortProperty.TITLE, the songs are sorted according to their title.
Sorting by property and specifying sort options
The work of actually sorting the list of songs is performed by the PlayList class in the sortList() method, as follows:
/**
* Sorts the list of songs according to the specified property.
*/
public function sortList(sortProperty:SortProperty):void
{
...
var sortOptions:uint;
switch (sortProperty)
{
case SortProperty.TITLE:
sortOptions = Array.CASEINSENSITIVE;
break;
case SortProperty.ARTIST:
sortOptions = Array.CASEINSENSITIVE;
break;
case SortProperty.YEAR:
sortOptions = Array.NUMERIC;
break;
}
// Perform the actual sorting of the data.
this._songs.sortOn(sortProperty.propertyName, sortOptions);
// Save the current sort property.
this._currentSort = sortProperty;
// Record that the list is sorted.
this._needToSort = false;
}

When sorting by title or artist, it makes sense to sort alphabetically, but when sorting by year, it’s most logical to
perform a numeric sort. The switch statement is used to define the appropriate sorting option, stored in the variable
sortOptions, according to the value specified in the sortProperty parameter. Here again the named enumeration
members are used to distinguish between properties, rather than hard-coded values.
With the sort property and sort options determined, the _songs array is actually sorted by calling its sortOn()
method, passing those two values as parameters. The current sort property is recorded, as is the fact that the song list
is currently sorted.

Last updated 3/21/2011

51

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Working with arrays

Combining array elements into a character-delimited string
Flash Player 9 and later, Adobe AIR 1.0 and later
In addition to using an array to maintain the song list in the PlayList class, in this example arrays are also used in the
Song class to help manage the list of genres to which a given song belongs. Consider this snippet from the Song class’s
definition:
private var _genres:String;
public function Song(title:String, artist:String, year:uint, filename:String, genres:Array)
{
...
// Genres are passed in as an array
// but stored as a semicolon-separated string.
this._genres = genres.join(";");
}

When creating a new Song instance, the genres parameter that is used to specify the genre (or genres) the song
belongs to is defined as an Array instance. This makes it convenient to group multiple genres together into a single
variable that can be passed to the constructor. However, internally the Song class maintains the genres in the private
_genres variable as a semicolon-separated String instance. The Array parameter is converted into a semicolonseparated string by calling its join() method with the literal string value ";" as the specified delimiter.
By the same token, the genres accessors allow genres to be set or retrieved as an Array:
public function get genres():Array
{
// Genres are stored as a semicolon-separated String,
// so they need to be transformed into an Array to pass them back out.
return this._genres.split(";");
}
public function set genres(value:Array):void
{
// Genres are passed in as an array,
// but stored as a semicolon-separated string.
this._genres = value.join(";");
}

The genresset accessor behaves exactly the same as the constructor; it accepts an Array and calls the join() method
to convert it to a semicolon-separated String. The get accessor performs the opposite operation: the _genres
variable’s split() method is called, splitting the String into an array of values using the specified delimiter (the literal
string value ";" as before).

Last updated 3/21/2011

52

Chapter 4: Handling errors
Flash Player 9 and later, Adobe AIR 1.0 and later
To “handle” an error means that you build logic into your application to respond to, or fix, an error. Errors are
generated either when an application is compiled or when a compiled application is running. When your application
handles errors, something occurs as a response when the error is encountered, as opposed to no response (when
whatever process created the error fails silently). Used correctly, error handling helps shield your application and its
users from otherwise unexpected behavior.
However, error handling is a broad category that includes responding to many kinds of errors that are thrown during
compilation or while an application is running. This discussion focuses on how to handle run-time errors (thrown
while an application is running), the different types of errors that can be generated, and the advantages of the errorhandling system in ActionScript 3.0.

Basics of error handling
Flash Player 9 and later, Adobe AIR 1.0 and later
A run-time error is something that goes wrong in your ActionScript code that stops the ActionScript content from
running as intended. To ensure that your ActionScript code runs smoothly for users, write code in your application
that handles the error—that fixes it, works around it, or at least lets the user know that it has happened. This process
is called error handling.
Error handling is a broad category that includes responding to many kinds of errors that are thrown during
compilation or while an application is running. Errors that happen at compile time are often easier to identify— fix
them to complete the process of creating a SWF file.
Run-time errors can be more difficult to detect, because in order for them to occur the erroneous code must actually
be run. If a segment of your program has several branches of code, like an if..then..else statement, test every
possible condition, with all the possible input values that real users might use, to confirm that your code is error-free.
Run-time errors can be divided into two categories: program errors are mistakes in your ActionScript code, such as
specifying the wrong data type for a method parameter; logical errors are mistakes in the logic (the data checking and
value manipulation) of your program, such as using the wrong formula to calculate interest rates in a banking
application. Again, both of these types of errors can often be detected and corrected ahead of time by diligently testing
your application.
Ideally, you’ll want to identify and remove all errors from your application before it is released to end users. However,
not all errors can be foreseen or prevented. For example, suppose your ActionScript application loads information
from a particular website that is outside your control. If at some point that website isn’t available, the part of your
application that depends on that external data won’t behave correctly. The most important aspect of error handling
involves preparing for these unknown cases and handling them gracefully. Users need to continue to use your
application, or at least get a friendly error message explaining why it isn’t working.

Last updated 3/21/2011

53

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

Run-time errors are represented in two ways in ActionScript:

• Error classes: Many errors have an error class associated with them. When an error occurs, the Flash runtime (such
as Flash Player or Adobe AIR) creates an instance of the specific error class that is associated with that particular
error. Your code can use the information contained in that error object to make an appropriate response to the
error.

• Error events: Sometimes an error occurs when the Flash runtime would normally trigger an event. In those cases,
an error event is triggered instead. Each error event has a class associated with it, and the Flash runtime passes an
instance of that class to the methods that are subscribed to the error event.
To determine whether a particular method can trigger an error or error event, see the method’s entry in the
ActionScript 3.0 Reference for the Adobe Flash Platform.
Important concepts and terms
The following reference list contains important terms for programming error handling routines:
Asynchronous A program command such as a method call that doesn’t provide an immediate result; instead it gives
a result (or error) in the form of an event.
Catch When an exception (a run-time error) occurs and your code becomes aware of the exception, that code is said

to catch the exception. Once an exception is caught, the Flash runtime stops notifying other ActionScript code of the
exception.
Debugger version A special version of the Flash runtime, such as the Flash Player dubugger version or the AIR Debug
Launcher (ADL), that contains code for notifying users of run-time errors. In the standard version of Flash Player or
Adobe AIR (the one that most users have), errors that aren’t handled by your ActionScript code are ignored. In the
debugger versions (which are included with Adobe Flash CS4 Professional and Adobe Flash Builder), a warning
message appears when an unhandled error happens.
Exception An error that happens while an application is running and that the Flash runtime can’t resolve on its own.
Re-throw When your code catches an exception, the Flash runtime no longer notifies other objects of the exception.

If it’s important for other objects to receive the exception, your code must re-throw the exception to start the
notification process again.
Synchronous A program command, such as a method call, that provides an immediate result (or immediately throws

an error), meaning that the response can be used within the same code block.
Throw The act of notifying a Flash runtime (and consequently, notifying other objects and ActionScript code) that an

error has occurred is known as throwing an error.

Types of errors
Flash Player 9 and later, Adobe AIR 1.0 and later
When you develop and run applications, you encounter different types of errors and error terminology. The following
list introduces the major error types and terms:

• Compile-time errors are raised by the ActionScript compiler during code compilation. Compile-time errors occur
when syntactical problems in your code prevent your application from being built.

Last updated 3/21/2011

54

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

• Run-time errors occur when you run your application after you compile it. Run-time errors represent errors that
are caused while a SWF file plays in a Flash runtime (such as Adobe Flash Player or Adobe AIR). In most cases, you
handle run-time errors as they occur, reporting them to the user and taking steps to keep your application running.
If the error is a fatal error, such as not being able to connect to a remote website or load required data, you can use
error handling to allow your application to finish, gracefully.

• Synchronous errors are run-time errors that occur at the time a function is called—for example, when you try to use
a specific method and the argument you pass to the method is invalid, so the Flash runtime throws an exception.
Most errors occur synchronously—at the time the statement executes—and the flow of control passes immediately
to the most applicable catch statement.
For example, the following code excerpt throws a run-time error because the browse() method is not called before
the program attempts to upload a file:
var fileRef:FileReference = new FileReference();
try
{
fileRef.upload(new URLRequest("http://www.yourdomain.com/fileupload.cfm"));
}
catch (error:IllegalOperationError)
{
trace(error);
// Error #2037: Functions called in incorrect sequence, or earlier
// call was unsuccessful.
}

In this case, a run-time error is thrown synchronously because Flash Player determined that the browse() method
was not called before the file upload was attempted.
For detailed information on synchronous error handling, see “Handling synchronous errors in an application” on
page 58.

• Asynchronouserrors are run-time errors that occur at various points during runtime; they generate events and event
listeners catch them. An asynchronous operation is one in which a function initiates an operation, but doesn’t wait
for it to complete. You can create an error event listener to wait for the application or user to try some operation.
If the operation fails, you catch the error with an event listener and respond to the error event. Then, the event
listener calls an event handler function to respond to the error event in a useful manner. For example, the event
handler could launch a dialog box that prompts the user to resolve the error.
Consider the file-upload synchronous error example presented earlier. If you successfully call the browse()
method before beginning a file upload, Flash Player would dispatch several events. For example, when an upload
starts, the open event is dispatched. When the file upload operation completes successfully, the complete event is
dispatched. Because event handling is asynchronous (that is, it does not occur at specific, known, predesignated
times), use the addEventListener() method to listen for these specific events, as the following code shows:

Last updated 3/21/2011

55

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

var fileRef:FileReference = new FileReference();
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.OPEN, openHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.browse();
function selectHandler(event:Event):void
{
trace("...select...");
var request:URLRequest = new URLRequest("http://www.yourdomain.com/fileupload.cfm");
request.method = URLRequestMethod.POST;
event.target.upload(request);
}
function openHandler(event:Event):void
{
trace("...open...");
}
function completeHandler(event:Event):void
{
trace("...complete...");
}

For detailed information on asynchronous error handling, see “Responding to error events and status” on page 63.

• Uncaught exceptions are errors thrown with no corresponding logic (like a catch statement) to respond to them.
If your application throws an error, and no appropriate catch statement or event handler can be found at the
current or higher level to handle the error, the error is considered an uncaught exception.
When an uncaught error happens, the runtime dispatches an uncaughtError event. This event is also known as a
“global error handler.” This event is dispatched by the SWF’s UncaughtErrorEvents object, which is available
through the LoaderInfo.uncaughtErrorEvents property. If no listeners are registered for the uncaughtError
event, the runtime ignores uncaught errors and tries to continue running, as long as the error doesn’t stop the SWF.
In addition to dispatching the uncaughtError event, debugger versions of the Flash runtime respond to uncaught
errors by terminating the current script. Then, they display the uncaught error in trace statement output or
writing the error message to a log file. If the exception object is an instance of the Error class or one of its subclasses,
the getStackTrace() method is called. The stack trace information is also displayed in trace statement output or
in a log file. For more information about using the debugger version of Flash runtimes, see “Working with the
debugger versions of Flash runtimes” on page 57.
Note: While processing an uncaughtError event, if an error event is thrown from an uncaughtError handler, the event
handler is called multiple times. This results in an infinite loop of exceptions. It is recommended that you avoid such
a scenario.

Error handling in ActionScript 3.0
Flash Player 9 and later, Adobe AIR 1.0 and later
Since many applications can run without building the logic to handle errors, developers are tempted to postpone
building error handling into their applications. However, without error handling, an application can easily stall or
frustrate the user if something doesn’t work as expected. ActionScript 2.0 has an Error class that allows you to build
logic into custom functions to throw an exception with a specific message. Because error handling is critical for making
a user-friendly application, ActionScript 3.0 includes an expanded architecture for catching errors.

Last updated 3/21/2011

56

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

Note: While the ActionScript 3.0 Reference for the Adobe Flash Platform documents the exceptions thrown by many
methods, it might not include all possible exceptions for each method. A method might throw an exception for syntax
errors or other problems that are not noted explicitly in the method description, even when the description does list some
of the exceptions a method throws.

ActionScript 3.0 error-handling elements
Flash Player 9 and later, Adobe AIR 1.0 and later
ActionScript 3.0 includes many tools for error handling, including:

• Error classes. ActionScript 3.0 includes a broad range of Error classes to expand the scope of situations that can
produce error objects. Each Error class helps applications handle and respond to specific error conditions, whether
they are related to system errors (like a MemoryError condition), coding errors (like an ArgumentError condition),
networking and communication errors (like a URIError condition), or other situations. For more information on
each class, see “Comparing the Error classes” on page 66.

• Fewer silent failures. In earlier versions of Flash Player, errors were generated and reported only if you explicitly
used the throw statement. For Flash Player 9 and later Flash runtimes, native ActionScript methods and properties
throw run-time errors. These errors allow you to handle these exceptions more effectively when they occur, then
react to each exception, individually.

• Clear error messages displayed during debugging. When you are using the debugger version of a Flash runtime,
problematic code or situations generate robust error messages, which help you easily identify reasons why a
particular block of code fails. These messages make fixing errors more efficient. For more information, see
“Working with the debugger versions of Flash runtimes” on page 57.

• Precise errors allow for clear error messages displayed to users. In previous versions of Flash Player, the
FileReference.upload() method returned a Boolean value of false if the upload() call was unsuccessful,
indicating one of five possible errors. If an error occurs when you call the upload() method in ActionScript 3.0,
four specific errors help you display more accurate error messages to end users.

• Refined error handling. Distinct errors are thrown for many common situations. For example, in ActionScript 2.0,
before a FileReference object has been populated, the name property has the value null (so, before you can use or
display the name property, ensure that the value is set and not null). In ActionScript 3.0, if you attempt to access
the name property before it has been populated, Flash Player or AIR throws an IllegalOperationError, which
informs you that the value has not been set, and you can use try..catch..finally blocks to handle the error. For
more information see “Using try..catch..finally statements” on page 58.

• No significant performance drawbacks. Using try..catch..finally blocks to handle errors takes little or no
additional resources compared to previous versions of ActionScript.

• An ErrorEvent class that allows you to build listeners for specific asynchronous error events. For more information
see “Responding to error events and status” on page 63.

Error-handling strategies
Flash Player 9 and later, Adobe AIR 1.0 and later
As long as your application doesn’t encounter a problematic condition, it can still run successfully if you don’t build
error-handling logic into your code. However, if you don’t actively handle errors and your application does encounter
a problem, your users will never know why your application fails when it does.

Last updated 3/21/2011

57

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

There are different ways you can approach error handling in your application. The following list summarizes the three
major options for handling errors:

• Use try..catch..finally statements. These statements catch synchronous errors as they occur. You can nest
your statements into a hierarchy to catch exceptions at various levels of code execution. For more information, see
“Using try..catch..finally statements” on page 58.

• Create your own custom error objects. You can use the Error class to create your own custom error objects to track
specific operations in your application that are not covered by built-in error types. Then you can use
try..catch..finally statements on your custom error objects. For more information see “Creating custom
error classes” on page 62.

• Write event listeners and handlers to respond to error events. By using this strategy, you can create global error
handlers that let you handle similar events without duplicating much code in try..catch..finally blocks. You
are also more likely to catch asynchronous errors using this approach. For more information, see “Responding to
error events and status” on page 63.

Working with the debugger versions of Flash runtimes
Flash Player 9 and later, Adobe AIR 1.0 and later
Adobe provides developers with special editions of the Flash runtimes to assist debugging efforts. You obtain a copy
of the debugger version of Flash Player when you install Adobe Flash Professional or Adobe Flash Builder. You also
obtain a utility for the debugging of Adobe AIR applications, which is called ADL, when you install either of those
tools, or as part of the Adobe AIR SDK.
There is a notable difference in how the debugger versions and the release versions of Flash Player and Adobe AIR
indicate errors. The debugger versions shows the error type (such as a generic Error, IOError, or EOFError), error
number, and a human-readable error message. The release versions shows only the error type and error number. For
example, consider the following code:
try
{
tf.text = myByteArray.readBoolean();
}
catch (error:EOFError)
{
tf.text = error.toString();
}

If the readBoolean() method throws an EOFError in the debugger version of Flash Player, the following message
displays in the tf text field: “EOFError: Error #2030: End of file was encountered.”
The same code in a release version of Flash Player or Adobe AIR would display the following text: “EOFError: Error
#2030.”
Note: The debugger players broadcast an event named "allComplete"; avoid creating custom events with the name
“allComplete”. Otherwise, you will encounter unpredictable behavior when debugging.
To keep resources and size to a minimum in the release versions, error message strings are not present. You can look
up the error number in the documentation (the appendixes of the ActionScript 3.0 Reference for the Adobe Flash
Platform) to correlate to an error message. Alternatively, you can reproduce the error using the debugger versions of
Flash Player and AIR to see the full message.

Last updated 3/21/2011

58

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

Handling synchronous errors in an application
Flash Player 9 and later, Adobe AIR 1.0 and later
The most common error handling is synchronous error-handling logic, where you insert statements into your code to
catch synchronous errors while an application is running. This type of error handling lets your application notice and
recover from run-time errors when functions fail. The logic for catching a synchronous error includes
try..catch..finally statements, which literally try an operation, catch any error response from the Flash runtime,
and finally execute some other operation to handle the failed operation.

Using try..catch..finally statements
Flash Player 9 and later, Adobe AIR 1.0 and later
When you work with synchronous run-time errors, use the try..catch..finally statements to catch errors. When
a run-time error occurs, the Flash runtime throws an exception, which means that it suspends normal execution and
creates a special object of type Error. The Error object is then thrown to the first available catch block.
The try statement encloses statements that have the potential to create errors. You always use the catch statement
with a try statement. If an error is detected in one of the statements in the try statement block, the catch statements
that are attached to that try statement run.
The finally statement encloses statements that run whether an error occurs in the try block. If there is no error, the
statements within the finally block execute after the try block statements complete. If there is an error, the
appropriate catch statement executes first, followed by the statements in the finally block.
The following code demonstrates the syntax for using the try..catch..finally statements:
try
{
// some code that could throw an error
}
catch (err:Error)
{
// code to react to the error
}
finally
{
// Code that runs whether an error was thrown. This code can clean
// up after the error, or take steps to keep the application running.
}

Each catch statement identifies a specific type of exception that it handles. The catch statement can specify only error
classes that are subclasses of the Error class. Each catch statement is checked in order. Only the first catch statement
that matches the type of error thrown runs. In other words, if you first check the higher-level Error class and then a
subclass of the Error class, only the higher-level Error class matches. The following code illustrates this point:

Last updated 3/21/2011

59

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

try
{
throw new ArgumentError("I am an ArgumentError");
}
catch (error:Error)
{
trace(" " + error.message);
}
catch (error:ArgumentError)
{
trace(" " + error.message);
}

The previous code displays the following output:
 I am an ArgumentError

To correctly catch the ArgumentError, make sure that the most specific error types are listed first and the more generic
error types are listed later, as the following code shows:
try
{
throw new ArgumentError("I am an ArgumentError");
}
catch (error:ArgumentError)
{
trace(" " + error.message);
}
catch (error:Error)
{
trace(" " + error.message);
}

Several methods and properties in the ActionScript API throw run-time errors if they encounter errors while they
execute. For example, the close() method in the Sound class throws an IOError if the method is unable to close the
audio stream, as demonstrated in the following code:
var mySound:Sound = new Sound();
try
{
mySound.close();
}
catch (error:IOError)
{
// Error #2029: This URLStream object does not have an open stream.
}

As you become more familiar with the ActionScript 3.0 Reference for the Adobe Flash Platform, you’ll notice which
methods throw exceptions, as detailed in each method’s description.

Last updated 3/21/2011

60

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

The throw statement
Flash Player 9 and later, Adobe AIR 1.0 and later
Flash runtimes throw exceptions when they encounter errors in your running application. In addition, you can
explicitly throw exceptions yourself using the throw statement. When explicitly throwing errors, Adobe recommends
that you throw instances of the Error class or its subclasses. The following code demonstrates a throw statement that
throws an instance of the Error class, MyErr, and eventually calls a function, myFunction(), to respond after the error
is thrown:
var MyError:Error = new Error("Encountered an error with the numUsers value", 99);
var numUsers:uint = 0;
try
{
if (numUsers == 0)
{
trace("numUsers equals 0");
}
}
catch (error:uint)
{
throw MyError; // Catch unsigned integer errors.
}
catch (error:int)
{
throw MyError; // Catch integer errors.
}
catch (error:Number)
{
throw MyError; // Catch number errors.
}
catch (error:*)
{
throw MyError; // Catch any other error.
}
finally
{
myFunction(); // Perform any necessary cleanup here.
}

Notice that the catch statements are ordered so that the most specific data types are listed first. If the catch statement
for the Number data type is listed first, neither the catch statement for the uint data type nor the catch statement for
the int data type is ever run.
Note: In the Java programming language, each function that can throw an exception must declare this fact, listing the
exception classes it can throw in a throws clause attached to the function declaration. ActionScript does not require you
to declare the exceptions thrown by a function.

Displaying a simple error message
Flash Player 9 and later, Adobe AIR 1.0 and later
One of the biggest benefits of the new exception and error event model is that it allows you to tell users when and why
an action has failed. Your part is to write the code to display the message and offer options in response.
The following code shows a simple try..catch statement to display the error in a text field:

Last updated 3/21/2011

61

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

package
{
import flash.display.Sprite;
import flash.text.TextField;
public class SimpleError extends Sprite
{
public var employee:XML =

1234
1-234
;
public function SimpleError()
{
try
{
if (employee.costCenter.length() != 1)
{
throw new Error("Error, employee must have exactly one cost center assigned.");
}
}
catch (error:Error)
{
var errorMessage:TextField = new TextField();
errorMessage.autoSize = TextFieldAutoSize.LEFT;
errorMessage.textColor = 0xFF0000;
errorMessage.text = error.message;
addChild(errorMessage);
}
}
}
}

Using a wider range of error classes and built-in compiler errors, ActionScript 3.0 offers more information than
previous versions of ActionScript about why something has failed. This information enables you to build more stable
applications with better error handling.

Rethrowing errors
Flash Player 9 and later, Adobe AIR 1.0 and later
When you build applications, there are several occasions in which you need to rethrow an error if you are unable to
handle the error properly. For example, the following code shows a nested try..catch block, which rethrows a
custom ApplicationError if the nested catch block is unable to handle the error:

Last updated 3/21/2011

62

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

try
{
try
{
trace("<< try >>");
throw new ApplicationError("some error which will be rethrown");
}
catch (error:ApplicationError)
{
trace("<< catch >> " + error);
trace("<< throw >>");
throw error;
}
catch (error:Error)
{
trace("<< Error >> " + error);
}
}
catch (error:ApplicationError)
{
trace("<< catch >> " + error);
}

The output from the previous snippet would be the following:
<<
<<
<<
<<

try >>
catch >> ApplicationError: some error which will be rethrown
throw >>
catch >> ApplicationError: some error which will be rethrown

The nested try block throws a custom ApplicationError error that is caught by the subsequent catch block. This
nested catch block can try to handle the error, and if unsuccessful, throw the ApplicationError object to the enclosing
try..catch block.

Creating custom error classes
Flash Player 9 and later, Adobe AIR 1.0 and later
You can extend one of the standard Error classes to create your own specialized error classes in ActionScript. There
are a number of reasons to create your own error classes:

• To identify specific errors or groups of errors that are unique to your application.
For example, take different actions for errors thrown by your own code, in addition to those errors trapped by a
Flash runtime. You can create a subclass of the Error class to track the new error data type in try..catch blocks.

• To provide unique error display capabilities for errors generated by your application.
For example, you can create a new toString() method that formats your error messages in a certain way. You can
also define a lookupErrorString() method that takes an error code and retrieves the proper message based on
the user’s language preference.
A specialized error class must extend the core ActionScript Error class. Here is an example of a specialized AppError
class that extends the Error class:

Last updated 3/21/2011

63

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

public class AppError extends Error
{
public function AppError(message:String, errorID:int)
{
super(message, errorID);
}
}

The following shows an example of using AppError in your project:
try
{
throw new AppError("Encountered Custom AppError", 29);
}
catch (error:AppError)
{
trace(error.errorID + ": " + error.message)
}

Note: If you want to override the Error.toString() method in your subclass, give it one ...(rest) parameter. The
ECMAScript language specification on which ActionScript 3.0 is based defines the Error.toString() method that way,
and ActionScript 3.0 defines it the same way for backward compatibility. Therefore, when you override the
Error.toString() method, match the parameters exactly. You do not want to pass any parameters to your
toString() method at runtime, because those parameters are ignored.

Responding to error events and status
Flash Player 9 and later, Adobe AIR 1.0 and later
One of the most noticeable improvements to error handling in ActionScript 3.0 is the support for error event handling
for responding to asynchronous errors while an application is running. (For a definition of asynchronous errors, see
“Types of errors” on page 53.)
You can create event listeners and event handlers to respond to the error events. Many classes dispatch error events
the same way they dispatch other events. For example, an instance of the XMLSocket class normally dispatches three
types of events: Event.CLOSE, Event.CONNECT, and DataEvent.DATA. However, when a problem occurs, the
XMLSocket class can dispatch the IOErrorEvent.IOError or the SecurityErrorEvent.SECURITY_ERROR. For
more information about event listeners and event handlers, see “Handling events” on page 117.
Error events fit into one of two categories:

• Error events that extend the ErrorEvent class
The flash.events.ErrorEvent class contains the properties and methods for managing errors related to networking
and communication operations in a running application. The AsyncErrorEvent, IOErrorEvent, and
SecurityErrorEvent classes extend the ErrorEvent class. If you’re using the debugger version of a Flash runtime, a
dialog box informs you at run-time of any error events without listener functions that the player encounters.

• Status-based error events
The status-based error events are related to the netStatus and status properties of the networking and
communication classes. If a Flash runtime encounters a problem when reading or writing data, the value of the
netStatus.info.level or status.level properties (depending on the class object you’re using) is set to the
value "error". You respond to this error by checking if the level property contains the value "error" in your
event handler function.

Last updated 3/21/2011

64

ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE
Handling errors

Working with error events
Flash Player 9 and later, Adobe AIR 1.0 and later
The ErrorEvent class and its subclasses contain error types for handling errors dispatched by Flash runtimes as they
try to read or write data.
The following example uses both a try..catch statement and error event handlers to display any errors detected
while trying to read a local file. You can add more sophisticated handling code to provide a user with options or
otherwise handle the error automatically in the places indicated by the comment “your error-handling code here”:
package
{
import
import
import
import
import
import
import
import
import

flash.display.Sprite;
flash.errors.IOError;
flash.events.IOErrorEvent;
flash.events.TextEvent;
flash.media.Sound;
flash.media.SoundChannel;
flash.net.URLRequest;
flash.text.TextField;
flash.text.TextFieldAutoSize;

public class LinkEventExample extends Sprite
{
private var myMP3:Sound;
public function LinkEventExample()
{
myMP3 = new Sound();
var list:TextField = new TextField();
list.autoSize = TextFieldAutoSize.LEFT;
list.multiline = true;
list.htmlText = "Track 1
"; list.htmlText += "Track 2
"; addEventListener(TextEvent.LINK, linkHandler); addChild(list); } private function playMP3(mp3:String):void { try { myMP3.load(new URLRequest(mp3)); myMP3.play(); } catch (err:Error) Last updated 3/21/2011 65 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors { trace(err.message); // your error-handling code here } myMP3.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); } private function linkHandler(linkEvent:TextEvent):void { playMP3(linkEvent.text); // your error-handling code here } private function errorHandler(errorEvent:IOErrorEvent):void { trace(errorEvent.text); // your error-handling code here } } } Working with status change events Flash Player 9 and later, Adobe AIR 1.0 and later Flash runtimes dynamically change the value of the netStatus.info.level or status.level properties for the classes that support the level property while an application is running. The classes that have the netStatus.info.level property are NetConnection, NetStream, and SharedObject. The classes that have the status.level property are HTTPStatusEvent, Camera, Microphone, and LocalConnection. You can write a handler function to respond to the change in level value and track communication errors. The following example uses a netStatusHandler() function to test the value of the level property. If the level property indicates that an error has been encountered, the code traces the message “Video stream failed”. package { import import import import import import flash.display.Sprite; flash.events.NetStatusEvent; flash.events.SecurityErrorEvent; flash.media.Video; flash.net.NetConnection; flash.net.NetStream; public class VideoExample extends Sprite { private var videoUrl:String = "Video.flv"; private var connection:NetConnection; private var stream:NetStream; public function VideoExample() { connection = new NetConnection(); connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); connection.connect(null); } Last updated 3/21/2011 66 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors private function netStatusHandler(event:NetStatusEvent):void { if (event.info.level == "error") { trace("Video stream failed") } else { connectStream(); } } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } private function connectStream():void { var stream:NetStream = new NetStream(connection); var video:Video = new Video(); video.attachNetStream(stream); stream.play(videoUrl); addChild(video); } } } Comparing the Error classes Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript provides a number of predefined Error classes. But, you can also use the same Error classes in your own code. There are two main types of Error classes in ActionScript 3.0: ActionScript core Error classes and flash.error package Error classes. The flash.error package contains additional classes to aid ActionScript 3.0 application development and debugging. Core Error classes Flash Player 9 and later, Adobe AIR 1.0 and later The core error classes include the Error, ArgumentError, EvalError, RangeError, ReferenceError, SecurityError, SyntaxError, TypeError, URIError, and VerifyError classes. Each of these classes are located in the top-level namespace. Last updated 3/21/2011 67 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors Class name Description Notes Error The Error class is for throwing exceptions, and is the base class for the other exception classes defined in ECMAScript: EvalError, RangeError, ReferenceError, SyntaxError, TypeError, and URIError. The Error class serves as the base class for all run-time errors, and is the recommended base class for any custom error classes. ArgumentError The ArgumentError class represents an error that Some examples of argument errors include the following: occurs when the parameter values supplied during a function call do not match the parameters defined for • Too few or too many arguments are supplied to a method. that function. • An argument was expected to be a member of an enumeration and was not. EvalError An EvalError exception is thrown if any parameters are passed to the Function class’s constructor or if user code calls the eval() function. In ActionScript 3.0, support for the eval() function has been removed and attempts to use the function result in an error. Earlier versions of Flash Player used the eval() function to access variables, properties, objects, or movie clips by name. RangeError A RangeError exception is thrown if a numeric value falls outside an acceptable range. For example, a RangeError is thrown by the Timer class if a delay was either negative or was not finite. A RangeError could also be thrown if you attempted to add a display object at an invalid depth. ReferenceError A ReferenceError exception is thrown when a reference to an undefined property is attempted on a sealed (nondynamic) object. Versions of the ActionScript compiler before ActionScript 3.0 did not throw an error when access was attempted to a property that was undefined. However ActionScript 3.0 throws the ReferenceError exception in this condition. Exceptions for undefined variables point to potential bugs, helping you improve software quality. However, if you are not used to having to initialize your variables, this new ActionScript behavior requires some changes in your coding habits. SecurityError The SecurityError exception is thrown when a security Some examples of security errors include the following: violation takes place and access is denied. • An unauthorized property access or method call is made across a security sandbox boundary. SyntaxError A SyntaxError exception is thrown when a parsing error occurs in your ActionScript code. • An attempt was made to access a URL not permitted by the security sandbox. • A socket connection was attempted to a port but the necessary socket policy file wasn’t present. • An attempt was made to access the user's camera or microphone, and the user denide the access to the device . A SyntaxError can be thrown under the following circumstances: • ActionScript throws SyntaxError exceptions when the RegExp class parses an invalid regular expression. • ActionScript throws SyntaxError exceptions when the XMLDocument class parses invalid XML. Last updated 3/21/2011 68 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors Class name Description Notes TypeError The TypeError exception is thrown when the actual type of an operand is different from the expected type. A TypeError can be thrown under the following circumstances: URIError VerifyError • An actual parameter of a function or method could not be coerced to the formal parameter type. • A value is assigned to a variable and cannot be coerced to the variable’s type. • The right side of the is or instanceof operator is not a valid type. • The super keyword is used illegally. • A property lookup results in more than one binding, and is therefore ambiguous. • A method is called on an incompatible object. For example, a TypeError exception is thrown if a method in the RegExp class is “grafted” onto a generic object and then called. The URIError exception is thrown when one of the global URI handling functions is used in a way that is incompatible with its definition. A URIError can be thrown under the following circumstances: A VerifyError exception is thrown when a malformed or corrupted SWF file is encountered. When a SWF file loads another SWF file, the parent SWF file can catch a VerifyError generated by the loaded SWF file. An invalid URI is specified for a Flash Player API function that expects a valid URI, such as Socket.connect(). flash.error package Error classes Flash Player 9 and later, Adobe AIR 1.0 and later The flash.error package contains Error classes that are considered part of the Flash runtime API. In contrast to the Error classes described, the flash.error package communicates errors events that are specific to Flash runtimes (such as Flash Player and Adobe AIR). Last updated 3/21/2011 69 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors Class name Description Notes EOFError An EOFError exception is thrown when you For example, an EOFError is thrown when one of the attempt to read past the end of the available data. read methods in the IDataInput interface is called and there is insufficient data to satisfy the read request. IllegalOperationError An IllegalOperationError exception is thrown when a method is not implemented or the implementation doesn't cover the current usage. Examples of illegal operation error exceptions include the following: • A base class, such as DisplayObjectContainer, provides more functionality than the Stage can support. For example, if you attempt to get or set a mask on the Stage (using stage.mask), the Flash runtime throws an IllegalOperationError with the message “The Stage class does not implement this property or method.” • A subclass inherits a method it does not require and does not want to support. • Certain accessibility methods are called when Flash Player is compiled without accessibility support. • Authoring-only features are called from a run-time version of Flash Player. • You attempt to set the name of an object placed on the timeline. IOError An IOError exception is thrown when some type of You get this error, for example, when a read-write I/O exception occurs. operation is attempted on a socket that is not connected or that has become disconnected. MemoryError A MemoryError exception is thrown when a memory allocation request fails. By default, ActionScript Virtual Machine 2 does not impose a limit on how much memory an ActionScript program allocates. On a desktop system, memory allocation failures are infrequent. You see an error thrown when the system is unable to allocate the memory required for an operation. So, on a desktop system, this exception is rare unless an allocation request is extremely large; for example, a request for 3 billion bytes is impossible because a 32-bit Microsoft® Windows® program can access only 2 GB of address space. ScriptTimeoutError A ScriptTimeoutError exception is thrown when a script timeout interval of 15 seconds is reached. By catching a ScriptTimeoutError exception, you can handle the script timeout more gracefully. If there is no exception handler, the uncaught exception handler displays a dialog box with an error message. To prevent a malicious developer from catching the exception and staying in an infinite loop, only the first ScriptTimeoutError exception thrown in the course of a particular script can be caught. A subsequent ScriptTimeoutError exception cannot be caught by your code and immediately goes to the uncaught exception handler. StackOverflowError The StackOverflowError exception is thrown when A StackOverflowError exception might indicate that the stack available to the script has been infinite recursion has occurred. exhausted. Last updated 3/21/2011 70 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors Handling errors example: CustomErrors application Flash Player 9 and later, Adobe AIR 1.0 and later The CustomErrors application demonstrates techniques for working with custom errors when building an application. These techniques are: • Validating an XML packet • Writing a custom error • Throwing custom errors • Notifying users when an error is thrown To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The CustomErrors application files can be found in the Samples/CustomError folder. The application consists of the following files: File Description CustomErrors.mxml The main application file in Flash (FLA) or Flex (MXML) or CustomErrors.fla com/example/programmingas3/errors/ApplicationError.as A class that serves as the base error class for both the FatalError and WarningError classes. com/example/programmingas3/errors/FatalError.as A class that defines a FatalError errorthrown by the application. This class extends the custom ApplicationError class. com/example/programmingas3/errors/Validator.as A class that defines a single method that validates a user-supplied employee XML packet. com/example/programmingas3/errors/WarningError.as A class that defines a WarningError error thrown by the application. This class extends the custom ApplicationError class. CustomErrors application overview Flash Player 9 and later, Adobe AIR 1.0 and later When the application loads, the initApp() method is called for Flex applications or the timeline (non-function) code is executed for Flash Professional applications. This code defines a sample XML packet to be verified by the Validator class. The following code is run: employeeXML = John Doe 12345 67890 ; } The XML packet is later displayed in a TextArea component instance on the Stage. This step allows you to modify the XML packet before attempting to revalidate it. Last updated 3/21/2011 71 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors When the user clicks the Validate button, the validateData() method is called. This method validates the employee XML packet using the validateEmployeeXML() method in the Validator class. The following code shows the validateData() method: function validateData():void { try { var tempXML:XML = XML(xmlText.text); Validator.validateEmployeeXML(tempXML); status.text = "The XML was successfully validated."; } catch (error:FatalError) { showFatalError(error); } catch (error:WarningError) { showWarningError(error); } catch (error:Error) { showGenericError(error); } } First, a temporary XML object is created using the contents of the TextArea component instance xmlText. Next, the validateEmployeeXML() method in the custom Validator class (com.example.programmingas3/errors/Validator.as) is called and passes the temporary XML object as a parameter. If the XML packet is valid, the status Label component instance displays a success message and the application exits. If the validateEmployeeXML() method throws a custom error (that is, a FatalError, WarningError, or a generic Error occurs), the appropriate catch statement executes and calls either the showFatalError(), showWarningError(), or showGenericError() methods. Each of these methods displays an appropriate message in a text area named statusText to notify the user of the specific error that occurred. Each method also updates the status Label component instance with a specific message. If a fatal error occurs during an attempt to validate the employee XML packet, the error message is displayed in the statusText text area, and the xmlText TextArea component instance and validateBtn Button component instance are disabled, as the following code shows: function showFatalError(error:FatalError):void { var message:String = error.message + "\n\n"; var title:String = error.getTitle(); statusText.text = message + " " + title + "\n\nThis application has ended."; this.xmlText.enabled = false; this.validateBtn.enabled = false; hideButtons(); } If a warning error instead of a fatal error occurs, the error message is displayed in the statusText TextArea instance, but the xmlText TextField and Button component instances aren’t disabled. The showWarningError() method displays the custom error message in the statusText text area. The message also asks the user to decide if they want to proceed with validating the XML or cancel the script. The following excerpt shows the showWarningError() method: Last updated 3/21/2011 72 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors function showWarningError(error:WarningError):void { var message:String = error.message + "\n\n" + "Do you want to exit this application?"; showButtons(); var title:String = error.getTitle(); statusText.text = message; } When the user clicks either the Yes or No button, the closeHandler() method is called. The following excerpt shows the closeHandler() method: function closeHandler(event:CloseEvent):void { switch (event.detail) { case yesButton: showFatalError(new FatalError(9999)); break; case noButton: statusText.text = ""; hideButtons(); break; } } If the user chooses to cancel the script by clicking Yes, a FatalError is thrown, causing the application to terminate. Building a custom validator Flash Player 9 and later, Adobe AIR 1.0 and later The custom Validator class contains a single method, validateEmployeeXML(). The validateEmployeeXML() method takes a single argument, employee, which is the XML packet that you want to validate. The validateEmployeeXML() method is as follows: public static function validateEmployeeXML(employee:XML):void { // checks for the integrity of items in the XML if (employee.costCenter.length() < 1) { throw new FatalError(9000); } if (employee.costCenter.length() > 1) { throw new WarningError(9001); } if (employee.ssn.length() != 1) { throw new FatalError(9002); } } To be validated, an employee must belong to one (and only one) cost center. If the employee doesn’t belong to any cost centers, the method throws a FatalError, which bubbles up to the validateData() method in the main application file. If the employee belongs to more than one cost center, a WarningError is thrown. The final check in the XML validator is that the user has exactly one social security number defined (the ssn node in the XML packet). If there is not exactly one ssn node, a FatalError error is thrown. Last updated 3/21/2011 73 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors You can add additional checks to the validateEmployeeXML() method—for example, to ensure that the ssn node contains a valid number, or that the employee has at least one phone number and e-mail address defined, and that both values are valid. You can also modify the XML so that each employee has a unique ID and specifies the ID of their manager. Defining the ApplicationError class Flash Player 9 and later, Adobe AIR 1.0 and later The ApplicationError class serves as the base class for both the FatalError and WarningError classes. The ApplicationError class extends the Error class, and defines its own custom methods and properties, including defining an error ID, severity, and an XML object that contains the custom error codes and messages. This class also defines two static constants that are used to define the severity of each error type. The ApplicationError class’s constructor method is as follows: public function ApplicationError() { messages = ; } Each error node in the XML object contains a unique numeric code and an error message. Error messages can be easily looked up by their error code using E4X, as seen in the following getMessageText() method: public function getMessageText(id:int):String { var message:XMLList = messages.error.(@code == id); return message[0].text(); } The getMessageText() method takes a single integer argument, id, and returns a string. The id argument is the error code for the error to look up. For example, passing an id of 9001 retrieves the error saying that employees must be assigned to only one cost center. If more than one error has the same error code, ActionScript returns the error message only for the first result found (message[0] in the returned XMLList object). The next method in this class, getTitle(), doesn’t take any parameters and returns a string value that contains the error ID for this specific error. This value is used to help you easily identify the exact error that occurred during validation of the XML packet. The following excerpt shows the getTitle() method: public function getTitle():String { return "Error #" + id; } Last updated 3/21/2011 74 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors The final method in the ApplicationError class is toString(). This method overrides the function defined in the Error class so that you can customize the presentation of the error message. The method returns a string that identifies the specific error number and message that occurred. public override function toString():String { return "[APPLICATION ERROR #" + id + "] " + message; } Defining the FatalError class Flash Player 9 and later, Adobe AIR 1.0 and later The FatalError class extends the custom ApplicationError class and defines three methods: the FatalError constructor, getTitle(), and toString(). The first method, the FatalError constructor, takes a single integer argument, errorID, and sets the error’s severity using the static constant values defined in the ApplicationError class, and gets the specific error’s error message by calling the getMessageText() method in the ApplicationError class. The FatalError constructor is as follows: public function FatalError(errorID:int) { id = errorID; severity = ApplicationError.FATAL; message = getMessageText(errorID); } The next method in the FatalError class, getTitle(), overrides the getTitle() method defined earlier in the ApplicationError class, and appends the text “-- FATAL” in the title to inform the user that a fatal error has occurred. The getTitle() method is as follows: public override function getTitle():String { return "Error #" + id + " -- FATAL"; } The final method in this class, toString(), overrides the toString() method defined in the ApplicationError class. The toString() method is public override function toString():String { return "[FATAL ERROR #" + id + "] " + message; } Defining the WarningError class Flash Player 9 and later, Adobe AIR 1.0 and later The WarningError class extends the ApplicationError class and is nearly identical to the FatalError class, except for a couple minor string changes and sets the error severity to ApplicationError.WARNING instead of ApplicationError.FATAL, as seen in the following code: Last updated 3/21/2011 75 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling errors public function WarningError(errorID:int) { id = errorID; severity = ApplicationError.WARNING; message = super.getMessageText(errorID); } Last updated 3/21/2011 76 Chapter 5: Using regular expressions Flash Player 9 and later, Adobe AIR 1.0 and later A regular expression describes a pattern that is used to find and manipulate matching text in strings. Regular expressions resemble strings, but they can include special codes to describe patterns and repetition. For example, the following regular expression matches a string that starts with the character A followed by one or more sequential digits: /A\d+/ The following topics describe the basic syntax for constructing regular expressions. However, regular expressions can have many complexities and nuances. You can find detailed resources on regular expressions on the web and in bookstores. Keep in mind that different programming environments implement regular expressions in different ways. ActionScript 3.0 implements regular expressions as defined in the ECMAScript edition 3 language specification (ECMA-262). More Help topics RegExp Basics of regular expressions Flash Player 9 and later, Adobe AIR 1.0 and later A regular expression describes a pattern of characters. Regular expressions are typically used to verify that a text value conforms to a particular pattern (such as verifying that a user-entered phone number has the proper number of digits) or to replace portions of a text value that matches a particular pattern. Regular expressions can be simple. For example, suppose you wanted to confirm that a particular string matches “ABC,” or wanted to replace every occurrence of “ABC” in a string with some other text. In that case, you could use the following regular expression, which defines the pattern consisting of the letters A, B, and C in sequence: /ABC/ Note that the regular expression literal is delineated with the forward slash (/) character. Regular expression patterns can also be complex, and sometimes cryptic in appearance, such as the following expression to match a valid e-mail address: /([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/ Most commonly you will use regular expressions to search for patterns in strings and to replace characters. In those cases, you will create a regular expression object and use it as a parameter for one of several String class methods. The following methods of the String class take regular expressions as parameters: match(), replace(), search(), and split(). For more information on these methods, see “Finding patterns in strings and replacing substrings” on page 16. The RegExp class includes the following methods: test() and exec(). For more information, see “Methods for using regular expressions with strings” on page 90. Last updated 3/21/2011 77 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Important concepts and terms The following reference list contains important terms that are relevant to this feature: Escape character A character indicating that the character that follows should be treated as a metacharacter rather than a literal character. In regular expression syntax, the backslash character (\) is the escape character, so a backslash followed by another character is a special code rather than just the character itself. Flag A character that specifies some option about how the regular expression pattern should be used, such as whether to distinguish between uppercase and lowercase characters. Metacharacter A character that has special meaning in a regular expression pattern, as opposed to literally representing that character in the pattern. Quantifier A character (or several characters) indicating how many times a part of the pattern should repeat. For example, a quantifier would be used to designate that a United States postal code should contain five or nine numbers. Regular expression A program statement defining a pattern of characters that can be used to confirm whether other strings match that pattern or to replace portions of a string. Regular expression syntax Flash Player 9 and later, Adobe AIR 1.0 and later This section describes all of the elements of ActionScript regular expression syntax. As you’ll see, regular expressions can have many complexities and nuances. You can find detailed resources on regular expressions on the web and in bookstores. Keep in mind that different programming environments implement regular expressions in different ways. ActionScript 3.0 implements regular expressions as defined in the ECMAScript edition 3 language specification (ECMA-262). Generally, you use regular expressions that match more complicated patterns than a simple string of characters. For example, the following regular expression defines the pattern consisting of the letters A, B, and C in sequence followed by any digit: /ABC\d/ The \d code represents “any digit.” The backslash (\) character is called the escape character, and combined with the character that follows it (in this case the letter d), it has special meaning in the regular expression. The following regular expression defines the pattern of the letters ABC followed by any number of digits (note the asterisk): /ABC\d*/ The asterisk character (*) is a metacharacter. A metacharacter is a character that has special meaning in regular expressions. The asterisk is a specific type of metacharacter called a quantifier, which is used to quantify the amount of repetition of a character or group of characters. For more information, see “Quantifiers” on page 82. In addition to its pattern, a regular expression can contain flags, which specify how the regular expression is to be matched. For example, the following regular expression uses the i flag, which specifies that the regular expression ignores case sensitivity in matching strings: /ABC\d*/i For more information, see “Flags and properties” on page 87. Last updated 3/21/2011 78 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions You can use regular expressions with the following methods of the String class: match(), replace(), and search(). For more information on these methods, see “Finding patterns in strings and replacing substrings” on page 16. Creating an instance of a regular expression Flash Player 9 and later, Adobe AIR 1.0 and later There are two ways to create a regular expression instance. One way uses forward slash characters (/) to delineate the regular expression; the other uses the new constructor. For example, the following regular expressions are equivalent: var pattern1:RegExp = /bob/i; var pattern2:RegExp = new RegExp("bob", "i"); Forward slashes delineate a regular expression literal in the same way as quotation marks delineate a string literal. The part of the regular expression within the forward slashes defines the pattern. The regular expression can also include flags after the final delineating slash. These flags are considered to be part of the regular expression, but they are separate from its pattern. When using the new constructor, you use two strings to define the regular expression. The first string defines the pattern, and the second string defines the flags, as in the following example: var pattern2:RegExp = new RegExp("bob", "i"); When including a forward slash within a regular expression that is defined by using the forward slash delineators, you must precede the forward slash with the backslash (\) escape character. For example, the following regular expression matches the pattern 1/2: var pattern:RegExp = /1\/2/; To include quotation marks within a regular expression that is defined with the new constructor, you must add backslash (\) escape character before the quotation marks (just as you would when defining any String literal). For example, the following regular expressions match the pattern eat at "joe's": var pattern1:RegExp = new RegExp("eat at \"joe's\"", ""); var pattern2:RegExp = new RegExp('eat at "joe\'s"', ""); Do not use the backslash escape character with quotation marks in regular expressions that are defined by using the forward slash delineators. Similarly, do not use the escape character with forward slashes in regular expressions that are defined with the new constructor. The following regular expressions are equivalent, and they define the pattern 1/2 "joe's": var pattern1:RegExp = /1\/2 "joe's"/; var pattern2:RegExp = new RegExp("1/2 \"joe's\"", ""); var pattern3:RegExp = new RegExp('1/2 "joe\'s"', ''); Also, in a regular expression that is defined with the new constructor, to use a metasequence that begins with the backslash (\) character, such as \d (which matches any digit), type the backslash character twice: var pattern:RegExp = new RegExp("\\d+", ""); // matches one or more digits You must type the backlash character twice in this case, because the first parameter of the RegExp() constructor method is a string, and in a string literal you must type a backslash character twice to have it recognized as a single backslash character. The sections that follow describe syntax for defining regular expression patterns. For more information on flags, see “Flags and properties” on page 87. Last updated 3/21/2011 79 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Characters, metacharacters, and metasequences Flash Player 9 and later, Adobe AIR 1.0 and later The simplest regular expression is one that matches a sequence of characters, as in the following example: var pattern:RegExp = /hello/; However, the following characters, known as metacharacters, have special meanings in regular expressions: ^ $ \ . * + ? ( ) [ ] { } | For example, the following regular expression matches the letter A followed by zero or more instances of the letter B (the asterisk metacharacter indicates this repetition), followed by the letter C: /AB*C/ To include a metacharacter without its special meaning in a regular expression pattern, you must use the backslash (\) escape character. For example, the following regular expression matches the letter A followed by the letter B, followed by an asterisk, followed by the letter C: var pattern:RegExp = /AB\*C/; A metasequence, like a metacharacter, has special meaning in a regular expression. A metasequence is made up of more than one character. The following sections provide details on using metacharacters and metasequences. About metacharacters The following table summarizes the metacharacters that you can use in regular expressions: Metacharacter Description ^ (caret) Matches at the start of the string. With the m (multiline) flag set, the caret matches the start of a line as well (see “Flags and properties” on page 87). Note that when used at the start of a character class, the caret indicates negation, not the start of a string. For more information, see “Character classes” on page 81. $(dollar sign) Matches at the end of the string. With the m (multiline) flag set, $ matches the position before a newline (\n) character as well. For more information, see “Flags and properties” on page 87. \ (backslash) Escapes the special metacharacter meaning of special characters. Also, use the backslash character if you want to use a forward slash character in a regular expression literal, as in /1\/2/ (to match the character 1, followed by the forward slash character, followed by the character 2). . (dot) Matches any single character. A dot matches a newline character (\n) only if the s (dotall) flag is set. For more information, see “Flags and properties” on page 87. * (star) Matches the previous item repeated zero or more times. For more information, see “Quantifiers” on page 82. + (plus) Matches the previous item repeated one or more times. For more information, see “Quantifiers” on page 82. ? (question mark) Matches the previous item repeated zero times or one time. For more information, see “Quantifiers” on page 82. Last updated 3/21/2011 80 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Metacharacter Description ( and ) Defines groups within the regular expression. Use groups for the following: • To confine the scope of the | alternator: /(a|b|c)d/ • To define the scope of a quantifier: /(walla.){1,2}/ • In backreferences. For example, the \1 in the following regular expression matches whatever matched the first parenthetical group of the pattern: • /(\w*) is repeated: \1/ For more information, see “Groups” on page 84. [ and ] Defines a character class, which defines possible matches for a single character: /[aeiou]/ matches any one of the specified characters. Within character classes, use the hyphen (-) to designate a range of characters: /[A-Z0-9]/ matches uppercase A through Z or 0 through 9. Within character classes, insert a backslash to escape the ] and - characters: /[+\-]\d+/ matches either + or - before one or more digits. Within character classes, other characters, which are normally metacharacters, are treated as normal characters (not metacharacters), without the need for a backslash: /[$]/£ matches either $or £. For more information, see “Character classes” on page 81. | (pipe) Used for alternation, to match either the part on the left side or the part on the right side: /abc|xyz/ matches either abc or xyz. About metasequences Metasequences are sequences of characters that have special meaning in a regular expression pattern. The following table describes these metasequences: Metasequence Description {n} Specifies a numeric quantifier or quantifier range for the previous item: {n,} /A{27}/ matches the character A repeated 27 times. and /A{3,}/ matches the character A repeated 3 or more times. {n,n} /A{3,5}/ matches the character A repeated 3 to 5 times. For more information, see “Quantifiers” on page 82. \b Matches at the position between a word character and a nonword character. If the first or last character in the string is a word character, also matches the start or end of the string. \B Matches at the position between two word characters. Also matches the position between two nonword characters. \d Matches a decimal digit. \D Matches any character other than a digit. \f Matches a form feed character. \n Matches the newline character. Last updated 3/21/2011 81 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Metasequence Description \r Matches the return character. \s Matches any white-space character (a space, tab, newline, or return character). \S Matches any character other than a white-space character. \t Matches the tab character. \unnnn Matches the Unicode character with the character code specified by the hexadecimal number nnnn. For example, \u263a is the smiley character. \v Matches a vertical feed character. \w Matches a word character (AZ–, az–, 0-9, or _). Note that \w does not match non-English characters, such as é , ñ , or ç . \W Matches any character other than a word character. \\xnn Matches the character with the specified ASCII value, as defined by the hexadecimal number nn. Character classes Flash Player 9 and later, Adobe AIR 1.0 and later You use character classes to specify a list of characters to match one position in the regular expression. You define character classes with square brackets ( [ and ] ). For example, the following regular expression defines a character class that matches bag, beg, big, bog, or bug: /b[aeiou]g/ Escape sequences in character classes Most metacharacters and metasequences that normally have special meanings in a regular expression do not have those same meanings inside a character class. For example, in a regular expression, the asterisk is used for repetition, but this is not the case when the asterisk appears in a character class. The following character class matches the asterisk literally, along with any of the other characters listed: /[abc*123]/ However, the three characters listed in the following table do function as metacharacters, with special meaning, in character classes: Metacharacter Meaning in character classes ] Defines the end of the character class. - Defines a range of characters (see the following section “Ranges of characters in character classes”). \ Defines metasequences and undoes the special meaning of metacharacters. For any of these characters to be recognized as literal characters (without the special metacharacter meaning), you must precede the character with the backslash escape character. For example, the following regular expression includes a character class that matches any one of four symbols ($, \, ], or -): /[$\\\]\-]/ In addition to the metacharacters that retain their special meanings, the following metasequences function as metasequences within character classes: Last updated 3/21/2011 82 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Metasequence Meaning in character classes \n Matches a newline character. \r Matches a return character. \t Matches a tab character. \unnnn Matches the character with the specified Unicode code point value (as defined by the hexadecimal number nnnn). \\xnn Matches the character with the specified ASCII value (as defined by the hexadecimal number nn). Other regular expression metasequences and metacharacters are treated as normal characters within a character class. Ranges of characters in character classes Use the hyphen to specify a range of characters, such as A-Z, a-z, or 0-9. These characters must constitute a valid range in the character set. For example, the following character class matches any one of the characters in the range a-z or any digit: /[a-z0-9]/ You can also use the \\xnn ASCII character code to specify a range by ASCII value. For example, the following character class matches any character from a set of extended ASCII characters (such as é and ê ): \\x Negated character classes When you use a caret (^) character at the beginning of a character class, it negates that class—any character not listed is considered a match. The following character class matches any character except for a lowercase letter (az–) or a digit: /[^a-z0-9]/ You must type the caret (^) character at the beginning of a character class to indicate negation. Otherwise, you are simply adding the caret character to the characters in the character class. For example, the following character class matches any one of a number of symbol characters, including the caret: /[!.,#+*%$&^]/ Quantifiers Flash Player 9 and later, Adobe AIR 1.0 and later You use quantifiers to specify repetitions of characters or sequences in patterns, as follows: Quantifier metacharacter Description * (star) Matches the previous item repeated zero or more times. + (plus) Matches the previous item repeated one or more times. ? (question mark) Matches the previous item repeated zero times or one time. {n} Specifies a numeric quantifier or quantifier range for the previous item: {n,} /A{27}/ matches the character A repeated 27 times. and /A{3,}/ matches the character A repeated 3 or more times. {n,n} /A{3,5}/ matches the character A repeated 3 to 5 times. Last updated 3/21/2011 83 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions You can apply a quantifier to a single character, to a character class, or to a group: • /a+/ matches the character a repeated one or more times. • /\d+/ matches one or more digits. • /[abc]+/ matches a repetition of one or more character, each of which is either a, b, or c. • /(very, )*/ matches the word very followed by a comma and a space repeated zero or more times. You can use quantifiers within parenthetical groupings that have quantifiers applied to them. For example, the following quantifier matches strings such as word and word-word-word: /\w+(-\w+)*/ By default, regular expressions perform what is known as greedy matching. Any subpattern in the regular expression (such as .*) tries to match as many characters in the string as possible before moving forward to the next part of the regular expression. For example, consider the following regular expression and string: var pattern:RegExp = /

.*<\/p>/; str:String = "

Paragraph 1

Paragraph 2

"; The regular expression matches the entire string:

Paragraph 1

Paragraph 2

Suppose, however, that you want to match only one

...

grouping. You can do this with the following:

Paragraph 1

Add a question mark (?) after any quantifier to change it to what is known as a lazy quantifier. For example, the following regular expression, which uses the lazy *? quantifier, matches

followed by the minimum number of characters possible (lazy), followed by

: /

.*?<\/p>/ Keep in mind the following points about quantifiers: • The quantifiers {0} and {0,0} do not exclude an item from a match. • Do not combine multiple quantifiers, as in /abc+*/. • The dot (.) does not span lines unless the s (dotall) flag is set, even if it is followed by a * quantifier. For example, consider the following code: var str:String = "

Test\n"; str += "Multiline

"; var re:RegExp = /

.*<\/p>/; trace(str.match(re)); // null; re = /

.*<\/p>/s; trace(str.match(re)); // output:

Test // Multiline

For more information, see “Flags and properties” on page 87. Alternation Flash Player 9 and later, Adobe AIR 1.0 and later Use the | (pipe) character in a regular expression to have the regular expression engine consider alternatives for a match. For example, the following regular expression matches any one of the words cat, dog, pig, rat: Last updated 3/21/2011 84 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions var pattern:RegExp = /cat|dog|pig|rat/; You can use parentheses to define groups to restrict the scope of the | alternator. The following regular expression matches cat followed by nap or nip: var pattern:RegExp = /cat(nap|nip)/; For more information, see “Groups” on page 84. The following two regular expressions, one using the | alternator, the other using a character class (defined with [ and ] ), are equivalent: /1|3|5|7|9/ /[13579]/ For more information, see “Character classes” on page 81. Groups Flash Player 9 and later, Adobe AIR 1.0 and later You can specify a group in a regular expression by using parentheses, as follows: /class-(\d*)/ A group is a subsection of a pattern. You can use groups to do the following things: • Apply a quantifier to more than one character. • Delineate subpatterns to be applied with alternation (by using the | character). • Capture substring matches (for example, by using \1 in a regular expression to match a previously matched group, or by using $1 similarly in the replace() method of the String class). The following sections provide details on these uses of groups. Using groups with quantifiers If you do not use a group, a quantifier applies to the character or character class that precedes it, as the following shows: var pattern:RegExp = /ab*/ ; // matches the character a followed by // zero or more occurrences of the character b pattern = /a\d+/; // matches the character a followed by // one or more digits pattern = /a[123]{1,3}/; // matches the character a followed by // one to three occurrences of either 1, 2, or 3 However, you can use a group to apply a quantifier to more than one character or character class: Last updated 3/21/2011 85 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions var pattern:RegExp = /(ab)*/; // matches zero or more occurrences of the character a // followed by the character b, such as ababab pattern = /(a\d)+/; // matches one or more occurrences of the character a followed by // a digit, such as a1a5a8a3 pattern = /(spam ){1,3}/; // matches 1 to 3 occurrences of the word spam followed by a space For more information on quantifiers, see “Quantifiers” on page 82. Using groups with the alternator (|) character You can use groups to define the group of characters to which you want to apply an alternator (|) character, as follows: var pattern:RegExp = /cat|dog/; // matches cat or dog pattern = /ca(t|d)og/; // matches catog or cadog Using groups to capture substring matches When you define a standard parenthetical group in a pattern, you can later refer to it in the regular expression. This is known as a backreference, and these sorts of groups are known as capturing groups. For example, in the following regular expression, the sequence \1 matches whatever substring matched the capturing parenthetical group: var pattern:RegExp = /(\d+)-by-\1/; // matches the following: 48-by-48 You can specify up to 99 of these backreferences in a regular expression by typing \1, \2, ... , \99. Similarly, in the replace() method of the String class, you can use $1$99– to insert captured group substring matches in the replacement string: var pattern:RegExp = /Hi, (\w+)\./; var str:String = "Hi, Bob."; trace(str.replace(pattern, "$1, hello.")); // output: Bob, hello. Also, if you use capturing groups, the exec() method of the RegExp class and the match() method of the String class return substrings that match the capturing groups: var pattern:RegExp = /(\w+)@(\w+).(\w+)/; var str:String = "bob@example.com"; trace(pattern.exec(str)); // bob@example.com,bob,example,com Using noncapturing groups and lookahead groups A noncapturing group is one that is used for grouping only; it is not “collected,” and it does not match numbered backreferences. Use (?: and ) to define noncapturing groups, as follows: var pattern = /(?:com|org|net); For example, note the difference between putting (com|org) in a capturing versus a noncapturing group (the exec() method lists capturing groups after the complete match): Last updated 3/21/2011 86 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions var pattern:RegExp = /(\w+)@(\w+).(com|org)/; var str:String = "bob@example.com"; trace(pattern.exec(str)); // bob@example.com,bob,example,com //noncapturing: var pattern:RegExp = /(\w+)@(\w+).(?:com|org)/; var str:String = "bob@example.com"; trace(pattern.exec(str)); // bob@example.com,bob,example A special type of noncapturing group is the lookahead group, of which there are two types: the positive lookahead group and the negative lookahead group. Use (?= and ) to define a positive lookahead group, which specifies that the subpattern in the group must match at the position. However, the portion of the string that matches the positive lookahead group can match remaining patterns in the regular expression. For example, because (?=e) is a positive lookahead group in the following code, the character e that it matches can be matched by a subsequent part of the regular expression—in this case, the capturing group, \w*): var pattern:RegExp = /sh(?=e)(\w*)/i; var str:String = "Shelly sells seashells by the seashore"; trace(pattern.exec(str)); // Shelly,elly Use (?! and ) to define a negative lookahead group that specifies that the subpattern in the group must not match at the position. For example: var pattern:RegExp = /sh(?!e)(\w*)/i; var str:String = "She sells seashells by the seashore"; trace(pattern.exec(str)); // shore,ore Using named groups A named group is a type of group in a regular expression that is given a named identifier. Use (?P and ) to define the named group. For example, the following regular expression includes a named group with the identifier named digits: var pattern = /[a-z]+(?P\d+)[a-z]+/; When you use the exec() method, a matching named group is added as a property of the result array: var myPattern:RegExp = /([a-z]+)(?P\d+)[a-z]+/; var str:String = "a123bcd"; var result:Array = myPattern.exec(str); trace(result.digits); // 123 Here is another example, which uses two named groups, with the identifiers name and dom: var emailPattern:RegExp = /(?P(\w|[_.\-])+)@(?P((\w|-)+))+\.\w{2,4}+/; var address:String = "bob@example.com"; var result:Array = emailPattern.exec(address); trace(result.name); // bob trace(result.dom); // example Note: Named groups are not part of the ECMAScript language specification. They are an added feature in ActionScript 3.0. Last updated 3/21/2011 87 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Flags and properties Flash Player 9 and later, Adobe AIR 1.0 and later The following table lists the five flags that you can set for regular expressions. Each flag can be accessed as a property of the regular expression object. Flag Property Description g global Matches more than one match. i ignoreCase Case-insensitive matching. Applies to the A—Z and a—z characters, but not to extended characters such as É and é . m multiline With this flag set, $ and ^ can match the beginning of a line and end of a line, respectively. s dotall With this flag set, . (dot) can match the newline character (\n). x extended Allows extended regular expressions. You can type spaces in the regular expression, which are ignored as part of the pattern. This lets you type regular expression code more legibly. Note that these properties are read-only. You can set the flags (g, i, m, s, x) when you set a regular expression variable, as follows: var re:RegExp = /abc/gimsx; However, you cannot directly set the named properties. For instance, the following code results in an error: var re:RegExp = /abc/; re.global = true; // This generates an error. By default, unless you specify them in the regular expression declaration, the flags are not set, and the corresponding properties are also set to false. Additionally, there are two other properties of a regular expression: • The lastIndex property specifies the index position in the string to use for the next call to the exec() or test() method of a regular expression. • The source property specifies the string that defines the pattern portion of the regular expression. The g (global) flag When the g (global) flag is not included, a regular expression matches no more than one match. For example, with the g flag not included in the regular expression, the String.match() method returns only one matching substring: var str:String = "she sells seashells by the seashore."; var pattern:RegExp = /sh\w*/; trace(str.match(pattern)) // output: she When the g flag is set, the Sting.match() method returns multiple matches, as follows: var str:String = "she sells seashells by the seashore."; var pattern:RegExp = /sh\w*/g; // The same pattern, but this time the g flag IS set. trace(str.match(pattern)); // output: she,shells,shore The i (ignoreCase) flag By default, regular expression matches are case-sensitive. When you set the i (ignoreCase) flag, case sensitivity is ignored. For example, the lowercase s in the regular expression does not match the uppercase letter S, the first character of the string: Last updated 3/21/2011 88 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions var str:String = "She sells seashells by the seashore."; trace(str.search(/sh/)); // output: 13 -- Not the first character With the i flag set, however, the regular expression does match the capital letter S: var str:String = "She sells seashells by the seashore."; trace(str.search(/sh/i)); // output: 0 The i flag ignores case sensitivity only for the A–Z and a–z characters, but not for extended characters such as É and é . The m (multiline) flag If the m (multiline) flag is not set, the ^ matches the beginning of the string and the $ matches the end of the string. If the m flag is set, these characters match the beginning of a line and end of a line, respectively. Consider the following string, which includes a newline character: var str:String = "Test\n"; str += "Multiline"; trace(str.match(/^\w*/g)); // Match a word at the beginning of the string. Even though the g (global) flag is set in the regular expression, the match() method matches only one substring, since there is only one match for the ^—the beginning of the string. The output is: Test Here is the same code with the m flag set: var str:String = "Test\n"; str += "Multiline"; trace(str.match(/^\w*/gm)); // Match a word at the beginning of lines. This time, the output includes the words at the beginning of both lines: Test,Multiline Note that only the \n character signals the end of a line. The following characters do not: • Return (\r) character • Unicode line-separator (\u2028) character • Unicode paragraph-separator (\u2029) character The s (dotall) flag If the s (dotall or “dot all”) flag is not set, a dot (.) in a regular expression pattern does not match a newline character (\n). So for the following example, there is no match: var str:String = "

Test\n"; str += "Multiline

"; var re:RegExp = /

.*?<\/p>/; trace(str.match(re)); However, if the s flag is set, the dot matches the newline character: var str:String = "

Test\n"; str += "Multiline

"; var re:RegExp = /

.*?<\/p>/s; trace(str.match(re)); In this case, the match is the entire substring within the

tags, including the newline character:

Test Multiline

Last updated 3/21/2011 89 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions The x (extended) flag Regular expressions can be difficult to read, especially when they include a lot of metasymbols and metasequences. For example: /|(\s*[^>]*>)).*?<\/p>/gi When you use the x (extended) flag in a regular expression, any blank spaces that you type in the pattern are ignored. For example, the following regular expression is identical to the previous example: /

| (\s* [^>]* >)) .*? <\/p> /gix If you have the x flag set and do want to match a blank space character, precede the blank space with a backslash. For example, the following two regular expressions are equivalent: /foo bar/ /foo \ bar/x The lastIndex property The lastIndex property specifies the index position in the string at which to start the next search. This property affects the exec() and test() methods called on a regular expression that has the g flag set to true. For example, consider the following code: var pattern:RegExp = /p\w*/gi; var str:String = "Pedro Piper picked a peck of pickled peppers."; trace(pattern.lastIndex); var result:Object = pattern.exec(str); while (result != null) { trace(pattern.lastIndex); result = pattern.exec(str); } The lastIndex property is set to 0 by default (to start searches at the beginning of the string). After each match, it is set to the index position following the match. Therefore, the output for the preceding code is the following: 0 5 11 18 25 36 44 If the global flag is set to false, the exec() and test() methods do not use or set the lastIndex property. The match(), replace(), and search() methods of the String class start all searches from the beginning of the string, regardless of the setting of the lastIndex property of the regular expression used in the call to the method. (However, the match() method does set lastIndex to 0.) You can set the lastIndex property to adjust the starting position in the string for regular expression matching. The source property The source property specifies the string that defines the pattern portion of a regular expression. For example: var pattern:RegExp = /foo/gi; trace(pattern.source); // foo Last updated 3/21/2011 90 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions Methods for using regular expressions with strings Flash Player 9 and later, Adobe AIR 1.0 and later The RegExp class includes two methods: exec() and test(). In addition to the exec() and test() methods of the RegExp class, the String class includes the following methods that let you match regular expressions in strings: match(), replace(), search(), and splice(). The test() method Flash Player 9 and later, Adobe AIR 1.0 and later The test() method of the RegExp class simply checks the supplied string to see if it contains a match for the regular expression, as the following example shows: var pattern:RegExp = /Class-\w/; var str = "Class-A"; trace(pattern.test(str)); // output: true The exec() method Flash Player 9 and later, Adobe AIR 1.0 and later The exec() method of the RegExp class checks the supplied string for a match of the regular expression and returns an array with the following: • The matching substring • Substring matches for any parenthetical groups in the regular expression The array also includes an index property, indicating the index position of the start of the substring match. For example, consider the following code: var pattern:RegExp = /\d{3}\-\d{3}-\d{4}/; //U.S phone number var str:String = "phone: 415-555-1212"; var result:Array = pattern.exec(str); trace(result.index, " - ", result); // 7-415-555-1212 Use the exec() method multiple times to match multiple substrings when the g (global) flag is set for the regular expression: var pattern:RegExp = /\w*sh\w*/gi; var str:String = "She sells seashells by the seashore"; var result:Array = pattern.exec(str); while (result != null) { trace(result.index, "\t", pattern.lastIndex, "\t", result); result = pattern.exec(str); } //output: // 0 3 She // 10 19 seashells // 27 35 seashore Last updated 3/21/2011 91 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions String methods that use RegExp parameters Flash Player 9 and later, Adobe AIR 1.0 and later The following methods of the String class take regular expressions as parameters: match(), replace(), search(), and split(). For more information on these methods, see “Finding patterns in strings and replacing substrings” on page 16. Regular expressions example: A Wiki parser Flash Player 9 and later, Adobe AIR 1.0 and later This simple Wiki text conversion example illustrates a number of uses for regular expressions: • Converting lines of text that match a source Wiki pattern to the appropriate HTML output strings. • Using a regular expression to convert URL patterns to HTML hyperlink tags. • Using a regular expression to convert U.S. dollar strings (such as "$9.95") to euro strings (such as "8.24 €"). To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The WikiEditor application files can be found in the folder Samples/WikiEditor. The application consists of the following files: File Description WikiEditor.mxml The main application file in Flash (FLA) or Flex (MXML). or WikiEditor.fla com/example/programmingas3/regExpExamples/WikiParser.as A class that includes methods that use regular expressions to convert Wiki input text patterns to the equivalent HTML output. com/example/programmingas3/regExpExamples/URLParser.as A class that includes methods that use regular expressions to convert URL strings to HTML hyperlink tags. com/example/programmingas3/regExpExamples/CurrencyConverter.as A class that includes methods that use regular expressions to convert U.S. dollar strings to euro strings. Defining the WikiParser class Flash Player 9 and later, Adobe AIR 1.0 and later The WikiParser class includes methods that convert Wiki input text into the equivalent HTML output. This is not a very robust Wiki conversion application, but it does illustrate some good uses of regular expressions for pattern matching and string conversion. The constructor function, along with the setWikiData() method, simply initializes a sample string of Wiki input text, as follows: public function WikiParser() { wikiData = setWikiData(); } Last updated 3/21/2011 92 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions When the user clicks the Test button in the sample application, the application invokes the parseWikiString() method of the WikiParser object. This method calls a number of other methods, which in turn assemble the resulting HTML string. public function parseWikiString(wikiString:String):String { var result:String = parseBold(wikiString); result = parseItalic(result); result = linesToParagraphs(result); result = parseBullets(result); return result; } Each of the methods called—parseBold(), parseItalic(), linesToParagraphs(), and parseBullets()—uses the replace() method of the string to replace matching patterns, defined by a regular expression, in order to transform the input Wiki text into HTML-formatted text. Converting boldface and italic patterns The parseBold() method looks for a Wiki boldface text pattern (such as '''foo''') and transforms it into its HTML equivalent (such as foo), as follows: private function parseBold(input:String):String { var pattern:RegExp = /'''(.*?)'''/g; return input.replace(pattern, "$1"); } Note that the (.?*) portion of the regular expression matches any number of characters (*) between the two defining ''' patterns. The ? quantifier makes the match nongreedy, so that for a string such as '''aaa''' bbb '''ccc''', the first matched string will be '''aaa''' and not the entire string (which starts and ends with the ''' pattern). The parentheses in the regular expression define a capturing group, and the replace() method refers to this group by using the $1 code in the replacement string. The g (global) flag in the regular expression ensures that the replace() method replaces all matches in the string (not simply the first one). The parseItalic() method works similarly to the parseBold() method, except that it checks for two apostrophes ('') as the delimiter for italic text (not three): private function parseItalic(input:String):String { var pattern:RegExp = /''(.*?)''/g; return input.replace(pattern, "$1"); } Converting bullet patterns As the following example shows, the parseBullet() method looks for the Wiki bullet line pattern (such as * foo) and transforms it into its HTML equivalent (such as

  • foo
  • ): private function parseBullets(input:String):String { var pattern:RegExp = /^\*(.*)/gm; return input.replace(pattern, "
  • $1
  • "); } The ^ symbol at the beginning of the regular expression matches the beginning of a line. The m (multiline) flag in the regular expression causes the regular expression to match the ^ symbol against the start of a line, not simply the start of the string. Last updated 3/21/2011 93 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions The \* pattern matches an asterisk character (the backslash is used to signal a literal asterisk instead of a * quantifier). The parentheses in the regular expression define a capturing group, and the replace() method refers to this group by using the $1 code in the replacement string. The g (global) flag in the regular expression ensures that the replace() method replaces all matches in the string (not simply the first one). Converting paragraph Wiki patterns The linesToParagraphs() method converts each line in the input Wiki string to an HTML

    paragraph tag. These lines in the method strip out empty lines from the input Wiki string: var pattern:RegExp = /^$/gm; var result:String = input.replace(pattern, ""); The ^ and $ symbols the regular expression match the beginning and end of a line. The m (multiline) flag in the regular expression causes the regular expression to match the ^ symbol against the start of a line, not simply the start of the string. The replace() method replaces all matching substrings (empty lines) with an empty string (""). The g (global) flag in the regular expression ensures that the replace() method replaces all matches in the string (not simply the first one). Converting URLs to HTML tags Flash Player 9 and later, Adobe AIR 1.0 and later When the user clicks the Test button in the sample application, if the user selected the urlToATag check box, the application calls the URLParser.urlToATag() static method to convert URL strings from the input Wiki string into HTML tags. var var var var var protocol:String = "((?:http|ftp)://)"; urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)"; optionalUrlPart:String = "(\.[a-z0-9_-]*)"; urlPattern:RegExp = new RegExp(protocol + urlPart + optionalUrlPart, "ig"); result:String = input.replace(urlPattern, "$1$2$3"); The RegExp() constructor function is used to assemble a regular expression (urlPattern) from a number of constituent parts. These constituent parts are each strings that define part of the regular expression pattern. The first part of the regular expression pattern, defined by the protocol string, defines an URL protocol: either http:// or ftp://. The parentheses define a noncapturing group, indicated by the ? symbol. This means that the parentheses are simply used to define a group for the | alternation pattern; the group will not match backreference codes ($1, $2, $3) in the replacement string of the replace() method. The other constituent parts of the regular expression each use capturing groups (indicated by parentheses in the pattern), which are then used in the backreference codes ($1, $2, $3) in the replacement string of the replace() method. The part of the pattern defined by the urlPart string matches at least one of the following characters: a-z, 0-9, _, or -. The + quantifier indicates that at least one character is matched. The \. indicates a required dot (.) character. And the remainder matches another string of at least one of these characters: a-z, 0-9, _, or -. The part of the pattern defined by the optionalUrlPart string matches zero or more of the following: a dot (.) character followed by any number of alphanumeric characters (including _ and -). The * quantifier indicates that zero or more characters are matched. The call to the replace() method employs the regular expression and assembles the replacement HTML string, using backreferences. Last updated 3/21/2011 94 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using regular expressions The urlToATag() method then calls the emailToATag() method, which uses similar techniques to replace e-mail patterns with HTML hyperlink strings. The regular expressions used to match HTTP, FTP, and e-mail URLs in this sample file are fairly simple, for the purposes of exemplification; there are much more complicated regular expressions for matching such URLs more correctly. Converting U.S. dollar strings to euro strings Flash Player 9 and later, Adobe AIR 1.0 and later When the user clicks the Test button in the sample application, if the user selected the dollarToEuro check box, the application calls the CurrencyConverter.usdToEuro() static method to convert U.S. dollar strings (such as "$9.95") to euro strings (such as "8.24 €"), as follows: var usdPrice:RegExp = /\$([\d,]+.\d+)+/g; return input.replace(usdPrice, usdStrToEuroStr); The first line defines a simple pattern for matching U.S. dollar strings. Notice that the $ character is preceded with the backslash (\) escape character. The replace() method uses the regular expression as the pattern matcher, and it calls the usdStrToEuroStr() function to determine the replacement string (a value in euros). When a function name is used as the second parameter of the replace() method, the following are passed as parameters to the called function: • The matching portion of the string. • Any captured parenthetical group matches. The number of arguments passed this way varies depending on the number of captured parenthetical group matches. You can determine the number of captured parenthetical group matches by checking arguments.length - 3 within the function code. • The index position in the string where the match begins. • The complete string. The usdStrToEuroStr() method converts U.S. dollar string patterns to euro strings, as follows: private function usdToEuro(...args):String { var usd:String = args[1]; usd = usd.replace(",", ""); var exchangeRate:Number = 0.828017; var euro:Number = Number(usd) * exchangeRate; trace(usd, Number(usd), euro); const euroSymbol:String = String.fromCharCode(8364); // € return euro.toFixed(2) + " " + euroSymbol; } Note that args[1] represents the captured parenthetical group matched by the usdPrice regular expression. This is the numerical portion of the U.S. dollar string: that is, the dollar amount without the $ sign. The method applies an exchange rate conversion and returns the resulting string (with a trailing € symbol instead of a leading $ symbol). Last updated 3/21/2011 95 Chapter 6: Working with XML Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript 3.0 includes a group of classes based on the ECMAScript for XML (E4X) specification (ECMA-357 edition 2). These classes include powerful and easy-to-use functionality for working with XML data. Using E4X, you will be able to develop code with XML data faster than was possible with previous programming techniques. As an added benefit, the code you produce will be easier to read. More Help topics XML class Processing XML with E4X tutorial at mozilla ECMA-357 specification Basics of XML Flash Player 9 and later, Adobe AIR 1.0 and later XML is a standard way of representing structured information so that it is easy for computers to work with and reasonably easy for people to write and understand. XML is an abbreviation for eXtensible Markup Language. The XML standard is available at www.w3.org/XML/. XML offers a standard and convenient way to categorize data, to make it easier to read, access, and manipulate. XML uses a tree structure and tag structure that is similar to HTML. Here is a simple example of XML data: What you know? Steve and the flubberblubs 1989 2006-10-17-08:31 XML data can also be more complex, with tags nested in other tags as well as attributes and other structural components. Here is a more complex example of XML data: Last updated 3/21/2011 96 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Questions, unanswered Steve and the flubberblubs 1989 What do you know? Steve and the flubberblubs 2006-10-17-08:31 Who do you know? Steve and the flubberblubs 2006-10-17-08:35 When do you know? Steve and the flubberblubs 2006-10-17-08:39 Do you know? Steve and the flubberblubs 2006-10-17-08:44 Notice that this XML document contains other complete XML structures within it (such as the song tags with their children). It also demonstrates other XML structures such as attributes (tracknumber and length in the song tags), and tags that contain other tags rather than containing data (such as the tracks tag). Getting started with XML If you have little or no experience with XML, here is a brief description of the most common aspects of XML data. XML data is written in plain-text form, with a specific syntax for organizing the information into a structured format. Generally, a single set of XML data is known as an XML document. In XML format, data is organized into elements (which can be single data items or containers for other elements) using a hierarchical structure. Every XML document has a single element as the top level or main item; inside this root element there may be a single piece of information, although there are more likely to be other elements, which in turn contain other elements, and so forth. For example, this XML document contains the information about a music album: What do you know? Steve and the flubberblubs Happy 2006-10-17-08:31 Each element is distinguished by a set of tags—the element’s name wrapped in angle brackets (less-than and greaterthan signs). The opening tag, indicating the start of the element, has the element name: The closing tag, which marks the end of the element, has a forward slash before the element’s name: Last updated 3/21/2011 97 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML If an element contains no content, it can be written as an empty element (sometimes called a self-closing element). In XML, this element: is identical to this element: In addition to the element’s content contained between the opening and closing tags, an element can also include other values, known as attributes, defined in the element’s opening tag. For example, this XML element defines a single attribute named length, with the value "4:19" : Each XML element has content, which is either a single value, one or more XML elements, or nothing (for an empty element). Learning more about XML To learn more about working with XML, there are a number of additional books and resources for learning more about XML, including these web sites: • W3Schools XML Tutorial: http://w3schools.com/xml/ • XMLpitstop tutorials, discussion lists, and more: http://xmlpitstop.com/ ActionScript classes for working with XML ActionScript 3.0 includes several classes that are used for working with XML-structured information. The two main classes are as follows: • XML: Represents a single XML element, which can be an XML document with multiple children or a single-value element within a document. • XMLList: Represents a set of XML elements. An XMLList object is used when there are multiple XML elements that are “siblings” (at the same level, and contained by the same parent, in the XML document’s hierarchy). For instance, an XMLList instance would be the easiest way to work with this set of XML elements (presumably contained in an XML document): Fred Wilson James Schmidt Susan Harriet Thurndon For more advanced uses involving XML namespaces, ActionScript also includes the Namespace and QName classes. For more information, see “Using XML namespaces” on page 110. In addition to the built-in classes for working with XML, ActionScript 3.0 also includes several operators that provide specific functionality for accessing and manipulating XML data. This approach to working with XML using these classes and operators is known as ECMAScript for XML (E4X), as defined by the ECMA-357 edition 2 specification. Important concepts and terms The following reference list contains important terms you will encounter when programming XML handling routines: Element A single item in an XML document, identified as the content contained between a starting tag and an ending tag (including the tags). XML elements can contain text data or other elements, or can be empty. Empty element An XML element that contains no child elements. Empty elements are often written as self-closing tags (such as ). Last updated 3/21/2011 98 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Document A single XML structure. An XML document can contain any number of elements (or can consist only of a single empty element); however, an XML document must have a single top-level element that contains all the other elements in the document. Node Another name for an XML element. Attribute A named value associated with an element that is written into the opening tag of the element in attributename="value" format, rather than being written as a separate child element nested inside the element. The E4X approach to XML processing Flash Player 9 and later, Adobe AIR 1.0 and later The ECMAScript for XML specification defines a set of classes and functionality for working with XML data. These classes and functionality are known collectively as E4X. ActionScript 3.0 includes the following E4X classes: XML, XMLList, QName, and Namespace. The methods, properties, and operators of the E4X classes are designed with the following goals: • Simplicity—Where possible, E4X makes it easier to write and understand code for working with XML data. • Consistency—The methods and reasoning behind E4X are internally consistent and consistent with other parts of ActionScript. • Familiarity—You manipulate XML data with well-known operators, such as the dot (.) operator. Note: There is a different XML class in ActionScript 2.0. In ActionScript 3.0 that class has been renamed as XMLDocument, so that the name does not conflict with the ActionScript 3.0 XML class that is part of E4X. In ActionScript 3.0, the legacy classes—XMLDocument, XMLNode, XMLParser, and XMLTag—are included in the flash.xml package primarily for legacy support. The new E4X classes are core classes; you need not import a package to use them. For details on the legacy ActionScript 2.0 XML classes, see the flash.xml packagein the ActionScript 3.0 Reference for the Adobe Flash Platform. Here is an example of manipulating data with E4X: var myXML:XML = burger 3.95 fries 1.45 Often, your application will load XML data from an external source, such as a web service or a RSS feed. However, for clarity, the code examples provided here assign XML data as literals. As the following code shows, E4X includes some intuitive operators, such as the dot (.) and attribute identifier (@) operators, for accessing properties and attributes in the XML: trace(myXML.item[0].menuName); // Output: burger trace(myXML.item.(@id==2).menuName); // Output: fries trace(myXML.item.(menuName=="burger").price); // Output: 3.95 Last updated 3/21/2011 99 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Use the appendChild() method to assign a new child node to the XML, as the following snippet shows: var newItem:XML = medium cola 1.25 myXML.appendChild(newItem); Use the @ and . operators not only to read data, but also to assign data, as in the following: myXML.item[0].menuName="regular burger"; myXML.item[1].menuName="small fries"; myXML.item[2].menuName="medium cola"; myXML.item.(menuName=="regular burger").@quantity = "2"; myXML.item.(menuName=="small fries").@quantity = "2"; myXML.item.(menuName=="medium cola").@quantity = "2"; Use a for loop to iterate through nodes of the XML, as follows: var total:Number = 0; for each (var property:XML in myXML.item) { var q:int = Number(property.@quantity); var p:Number = Number(property.price); var itemTotal:Number = q * p; total += itemTotal; trace(q + " " + property.menuName + " $" + itemTotal.toFixed(2)) } trace("Total: $", total.toFixed(2)); XML objects Flash Player 9 and later, Adobe AIR 1.0 and later An XML object may represent an XML element, attribute, comment, processing instruction, or text element. An XML object is classified as having either simple content or complex content. An XML object that has child nodes is classified as having complex content. An XML object is said to have simple content if it is any one of the following: an attribute, a comment, a processing instruction, or a text node. For example, the following XML object contains complex content, including a comment and a processing instruction: Last updated 3/21/2011 100 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML XML.ignoreComments = false; XML.ignoreProcessingInstructions = false; var x1:XML = burger 3.95 fries 1.45 As the following example shows, you can now use the comments() and processingInstructions() methods to create new XML objects, a comment and a processing instruction: var x2:XML = x1.comments()[0]; var x3:XML = x1.processingInstructions()[0]; XML properties Flash Player 9 and later, Adobe AIR 1.0 and later The XML class has five static properties: • The ignoreComments and ignoreProcessingInstructions properties determine whether comments or processing instructions are ignored when the XML object is parsed. • The ignoreWhitespace property determines whether white space characters are ignored in element tags and embedded expressions that are separated only by white space characters. • The prettyIndentand prettyPrinting properties are used to format the text that is returned by the toString() and toXMLString() methods of the XML class. For details on these properties, see the ActionScript 3.0 Reference for the Adobe Flash Platform. XML methods Flash Player 9 and later, Adobe AIR 1.0 and later The following methods allow you to work with the hierarchical structure of XML objects: • appendChild() • child() • childIndex() • children() • descendants() • elements() • insertChildAfter() • insertChildBefore() Last updated 3/21/2011 101 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML • parent() • prependChild() The following methods allow you to work with XML object attributes: • attribute() • attributes() The following methods allow you to you work with XML object properties: • hasOwnProperty() • propertyIsEnumerable() • replace() • setChildren() The following methods are for working with qualified names and namespaces: • addNamespace() • inScopeNamespaces() • localName() • name() • namespace() • namespaceDeclarations() • removeNamespace() • setLocalName() • setName() • setNamespace() The following methods are for working with and determining certain types of XML content: • comments() • hasComplexContent() • hasSimpleContent() • nodeKind() • processingInstructions() • text() The following methods are for conversion to strings and for formatting XML objects: • defaultSettings() • setSettings() • settings() • normalize() • toString() • toXMLString() Last updated 3/21/2011 102 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML There are a few additional methods: • contains() • copy() • valueOf() • length() For details on these methods, see the ActionScript 3.0 Reference for the Adobe Flash Platform. XMLList objects Flash Player 9 and later, Adobe AIR 1.0 and later An XMLList instance represents an arbitrary collection of XML objects. It can contain full XML documents, XML fragments, or the results of an XML query. The following methods allow you to work with the hierarchical structure of XMLList objects: • child() • children() • descendants() • elements() • parent() The following methods allow you to work with XMLList object attributes: • attribute() • attributes() The following methods allow you to you work with XMLList properties: • hasOwnProperty() • propertyIsEnumerable() The following methods are for working with and determining certain types of XML content: • comments() • hasComplexContent() • hasSimpleContent() • processingInstructions() • text() The following are for conversion to strings and for formatting the XMLList object: • normalize() • toString() • toXMLString() There are a few additional methods: • contains() Last updated 3/21/2011 103 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML • copy() • length() • valueOf() For details on these methods, see the ActionScript 3.0 Reference for the Adobe Flash Platform. For an XMLList object that contains exactly one XML element, you can use all properties and methods of the XML class, because an XMLList with one XML element is treated the same as an XML object. For example, in the following code, because doc.div is an XMLList object containing one element, you can use the appendChild() method from the XML class: var doc:XML =

    ; doc.div.appendChild(

    World

    ); For a list of XML properties and methods, see “XML objects” on page 99. Initializing XML variables Flash Player 9 and later, Adobe AIR 1.0 and later You can assign an XML literal to an XML object, as follows: var myXML:XML = burger 3.95 fries 1.45 As the following snippet shows, you can also use the new constructor to create an instance of an XML object from a string that contains XML data: var str:String = "burger" + "3.95"; var myXML:XML = new XML(str); If the XML data in the string is not well formed (for example, if a closing tag is missing), you will see a run-time error. You can also pass data by reference (from other variables) into an XML object, as the following example shows: Last updated 3/21/2011 104 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var tagname:String = "item"; var attributename:String = "id"; var attributevalue:String = "5"; var content:String = "Chicken"; var x:XML = <{tagname} {attributename}={attributevalue}>{content}; trace(x.toXMLString()) // Output: Chicken To load XML data from a URL, use the URLLoader class, as the following example shows: import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; var externalXML:XML; var loader:URLLoader = new URLLoader(); var request:URLRequest = new URLRequest("xmlFile.xml"); loader.load(request); loader.addEventListener(Event.COMPLETE, onComplete); function onComplete(event:Event):void { var loader:URLLoader = event.target as URLLoader; if (loader != null) { externalXML = new XML(loader.data); trace(externalXML.toXMLString()); } else { trace("loader is not a URLLoader!"); } } To read XML data from a socket connection, use the XMLSocket class. For more information, see the XMLSocket class in the ActionScript 3.0 Reference for the Adobe Flash Platform. Assembling and transforming XML objects Flash Player 9 and later, Adobe AIR 1.0 and later Use the prependChild() method or the appendChild() method to add a property to the beginning or end of an XML object’s list of properties, as the following example shows: var var var x = x = x = x1:XML =

    Line 1

    x2:XML =

    Line 2

    x:XML = x.appendChild(x1); x.appendChild(x2); x.prependChild(

    Line 0

    ); // x ==

    Line 0

    Line 1

    Line 2

    Use the insertChildBefore() method or the insertChildAfter() method to add a property before or after a specified property, as follows: Last updated 3/21/2011 105 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var x:XML =

    Paragraph 1

    Paragraph 2

    var newNode:XML =

    Paragraph 1.5

    x = x.insertChildAfter(x.p[0], newNode) x = x.insertChildBefore(x.p[2],

    Paragraph 1.75

    ) As the following example shows, you can also use curly brace operators ( { and } ) to pass data by reference (from other variables) when constructing XML objects: var ids:Array = [121, 122, 123]; var names:Array = [["Murphy","Pat"], ["Thibaut","Jean"], ["Smith","Vijay"]] var x:XML = new XML(""); for (var i:int = 0; i < 3; i++) { var newnode:XML = new XML(); newnode = {names[i][0]} {names[i][1]} ; x = x.appendChild(newnode) } You can assign properties and attributes to an XML object by using the = operator, as in the following: var x:XML = Smith x.firstname = "Jean"; x.@id = "239"; This sets the XML object x to the following: Smith Jean You can use the + and += operators to concatenate XMLList objects: var x1:XML =
    test1 var x2:XML = test2 var xList:XMLList = x1 + x2; xList += test3 This sets the XMLList object xList to the following: test1 test2 test3 Last updated 3/21/2011 106 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Traversing XML structures Flash Player 9 and later, Adobe AIR 1.0 and later One of the powerful features of XML is its ability to provide complex, nested data via a linear string of text characters. When you load data into an XML object, ActionScript parses the data and loads its hierarchical structure into memory (or it sends a run-time error if the XML data is not well formed). The operators and methods of the XML and XMLList objects make it easy to traverse the structure of XML data. Use the dot (.) operator and the descendent accessor (..) operator to access child properties of an XML object. Consider the following XML object: var myXML:XML = Baking Extravagant Pastries with Kumquats Contino Chuck 238 Emu Care and Breeding Case Justin 115 The object myXML.book is an XMLList object containing child properties of the myXML object that have the name book. These are two XML objects, matching the two book properties of the myXML object. The object myXML..lastName is an XMLList object containing any descendent properties with the name lastName. These are two XML objects, matching the two lastName of the myXML object. The object myXML.book.editor.lastName is an XMLList object containing any children with the name lastName of children with the name editor of children with the name book of the myXML object: in this case, an XMLList object containing only one XML object (the lastName property with the value "Case"). Accessing parent and child nodes Flash Player 9 and later, Adobe AIR 1.0 and later The parent() method returns the parent of an XML object. You can use the ordinal index values of a child list to access specific child objects. For example, consider an XML object myXML that has two child properties named book. Each child property named book has an index number associated with it: myXML.book[0] myXML.book[1] Last updated 3/21/2011 107 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML To access a specific grandchild, you can specify index numbers for both the child and grandchild names: myXML.book[0].title[0] However, if there is only one child of x.book[0] that has the name title, you can omit the index reference, as follows: myXML.book[0].title Similarly, if there is only one book child of the object x, and if that child object has only one title object, you can omit both index references, like this: myXML.book.title You can use the child() method to navigate to children with names based on a variable or expression, as the following example shows: var myXML:XML = Dictionary ; var childName:String = "book"; trace(myXML.child(childName).title) // output: Dictionary Accessing attributes Flash Player 9 and later, Adobe AIR 1.0 and later Use the @ symbol (the attribute identifier operator) to access attributes in an XML or XMLList object, as shown in the following code: var employee:XML = Wu Erin ; trace(employee.@id); // 6401 You can use the * wildcard symbol with the @ symbol to access all attributes of an XML or XMLList object, as in the following code: var employee:XML = Wu Erin ; trace(employee.@*.toXMLString()); // 6401 // 233 You can use the attribute() or attributes() method to access a specific attribute or all attributes of an XML or XMLList object, as in the following code: Last updated 3/21/2011 108 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var employee:XML = Wu Erin ; trace(employee.attribute("id")); // 6401 trace(employee.attribute("*").toXMLString()); // 6401 // 233 trace(employee.attributes().toXMLString()); // 6401 // 233 Note that you can also use the following syntax to access attributes, as the following example shows: employee.attribute("id") employee["@id"] employee.@["id"] These are each equivalent to employee.@id. However, the syntax employee.@id is the preferred approach. Filtering by attribute or element value Flash Player 9 and later, Adobe AIR 1.0 and later You can use the parentheses operators— ( and ) —to filter elements with a specific element name or attribute value. Consider the following XML object: var x:XML = Zmed Sue Data analyst McGee Chuck Jr. data analyst The following expressions are all valid: • x.employee.(lastName == "McGee")—This is the second employee node. • x.employee.(lastName == "McGee").firstName—This is the firstName property of the second employee node. • x.employee.(lastName == "McGee").@id—This is the value of the id attribute of the second employee node. • x.employee.(@id == 347)—The first employee node. • x.employee.(@id == 347).lastName—This is the lastName property of the first employee node. • x.employee.(@id > 300)—This is an XMLList with both employee properties. • x.employee.(position.toString().search("analyst") > -1)—This is an XMLList with both position properties. If you try to filter on attributes or elements that do not exist, an exception is thrown. For example, the final line of the following code generates an error, because there is no id attribute in the second p element: Last updated 3/21/2011 109 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var doc:XML =

    Hello, Bob.

    Hello.

    ; trace(doc.p.(@id == '123')); Similarly, the final line of following code generates an error because there is no b property of the second p element: var doc:XML =

    Hello, Bob.

    Hello.

    ; trace(doc.p.(b == 'Bob')); To avoid these errors, you can identify the properties that have the matching attributes or elements by using the attribute() and elements() methods, as in the following code: var doc:XML =

    Hello, Bob.

    Hello.

    ; trace(doc.p.(attribute('id') == '123')); trace(doc.p.(elements('b') == 'Bob')); You can also use the hasOwnProperty() method, as in the following code: var doc:XML =

    Hello, Bob.

    Hello.

    ; trace(doc.p.(hasOwnProperty('@id') && @id == '123')); trace(doc.p.(hasOwnProperty('b') && b == 'Bob')); Using the for..in and the for each..in statements Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript 3.0 includes the for..in statement and the for each..in statement for iterating through XMLList objects. For example, consider the following XML object, myXML, and the XMLList object, myXML.item. The XMLList object, myXML.item, consists of the two item nodes of the XML object. var myXML:XML = burger 3.95 fries 1.45 ; The for..in statement lets you iterate over a set of property names in an XMLList: Last updated 3/21/2011 110 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var total:Number = 0; for (var pname:String in myXML.item) { total += myXML.item.@quantity[pname] * myXML.item.price[pname]; } The for each..in statement lets you iterate through the properties in the XMLList: var total2:Number = 0; for each (var prop:XML in myXML.item) { total2 += prop.@quantity * prop.price; } Using XML namespaces Flash Player 9 and later, Adobe AIR 1.0 and later Namespaces in an XML object (or document) identify the type of data that the object contains. For example, in sending and delivering XML data to a web service that uses the SOAP messaging protocol, you declare the namespace in the opening tag of the XML: var message:XML = 78 ; The namespace has a prefix, soap, and a URI that defines the namespace, http://schemas.xmlsoap.org/soap/envelope/. ActionScript 3.0 includes the Namespace class for working with XML namespaces. For the XML object in the previous example, you can use the Namespace class as follows: var soapNS:Namespace = message.namespace("soap"); trace(soapNS); // Output: http://schemas.xmlsoap.org/soap/envelope/ var wNS:Namespace = new Namespace("w", "http://www.test.com/weather/"); message.addNamespace(wNS); var encodingStyle:XMLList = message.@soapNS::encodingStyle; var body:XMLList = message.soapNS::Body; message.soapNS::Body.wNS::GetWeatherResponse.wNS::tempurature = "78"; The XML class includes the following methods for working with namespaces: addNamespace(), inScopeNamespaces(), localName(), name(), namespace(), namespaceDeclarations(), removeNamespace(), setLocalName(), setName(), and setNamespace(). The default xml namespace directive lets you assign a default namespace for XML objects. For example, in the following, both x1 and x2 have the same default namespace: Last updated 3/21/2011 111 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var ns1:Namespace = new Namespace("http://www.example.com/namespaces/"); default xml namespace = ns1; var x1:XML = ; var x2:XML = ; XML type conversion Flash Player 9 and later, Adobe AIR 1.0 and later You can convert XML objects and XMLList objects to String values. Similarly, you can convert strings to XML objects and XMLList objects. Also, keep in mind that all XML attribute values, names, and text values are strings. The following sections discuss all these forms of XML type conversion. Converting XML and XMLList objects to strings Flash Player 9 and later, Adobe AIR 1.0 and later The XML and XMLList classes include a toString() method and a toXMLString() method. The toXMLString() method returns a string that includes all tags, attributes, namespace declarations, and content of the XML object. For XML objects with complex content (child elements), the toString() method does exactly the same as the toXMLString() method. For XML objects with simple content (those that contain only one text element), the toString() method returns only the text content of the element, as the following example shows: var myXML:XML = burger 3.95 ; trace(myXML.item[0].menuName.toXMLString()); // burger trace(myXML.item[0].menuName.toString()); // burger If you use the trace() method without specifying toString() or toXMLString(), the data is converted using the toString() method by default, as this code shows: var myXML:XML = burger 3.95 ; trace(myXML.item[0].menuName); // burger When using the trace() method to debug code, you will often want to use the toXMLString() method so that the trace() method outputs more complete data. Last updated 3/21/2011 112 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Converting strings to XML objects Flash Player 9 and later, Adobe AIR 1.0 and later You can use the new XML() constructor to create an XML object from a string, as follows: var x:XML = new XML("test"); If you attempt to convert a string to XML from a string that represents invalid XML or XML that is not well formed, a run-time error is thrown, as follows: var x:XML = new XML("test"); // throws an error Converting attribute values, names, and text values from strings Flash Player 9 and later, Adobe AIR 1.0 and later All XML attribute values, names, and text values are String data types, and you may need to convert these to other data types. For example, the following code uses the Number() function to convert text values to numbers: var myXML:XML = 3.95 1.00 ; var total:XML = 0; myXML.appendChild(total); for each (var item:XML in myXML.item) { myXML.total.children()[0] = Number(myXML.total.children()[0]) + Number(item.price.children()[0]); } trace(myXML.total); // 4.95; If this code did not use the Number() function, the code would interpret the + operator as the string concatenation operator, and the trace() method in the last line would output the following: 01.003.95 Reading external XML documents Flash Player 9 and later, Adobe AIR 1.0 and later You can use the URLLoader class to load XML data from a URL. To use the following code in your applications, replace the XML_URL value in the example with a valid URL: Last updated 3/21/2011 113 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML import flash.events.Event; import flash.net.URLLoader; var myXML:XML = new XML(); var XML_URL:String = "http://www.example.com/Sample3.xml"; var myXMLURL:URLRequest = new URLRequest(XML_URL); var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(Event.COMPLETE, xmlLoaded); function xmlLoaded(event:Event):void { myXML = XML(myLoader.data); trace("Data loaded."); } You can also use the XMLSocket class to set up an asynchronous XML socket connection with a server. For more information, see the ActionScript 3.0 Reference for the Adobe Flash Platform. XML in ActionScript example: Loading RSS data from the Internet Flash Player 9 and later, Adobe AIR 1.0 and later The RSSViewer sample application shows a number of features of working with XML in ActionScript, including the following: • Using XML methods to traverse XML data in the form of an RSS feed. • Using XML methods to assemble XML data in the form of HTML to use in a text field. The RSS format is widely used to syndicate news via XML. A simple RSS data file may look like the following: Last updated 3/21/2011 114 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Alaska - Weather http://www.nws.noaa.gov/alerts/ak.html Alaska - Watches, Warnings and Advisories Short Term Forecast - Taiya Inlet, Klondike Highway (Alaska) http://www.nws.noaa.gov/alerts/ak.html#A18.AJKNK.1900 Short Term Forecast Issued At: 2005-04-11T19:00:00 Expired At: 2005-04-12T01:00:00 Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov Short Term Forecast - Haines Borough (Alaska) http://www.nws.noaa.gov/alerts/ak.html#AKZ019.AJKNOWAJK.190000 Short Term Forecast Issued At: 2005-04-11T19:00:00 Expired At: 2005-04-12T01:00:00 Issuing Weather Forecast Office Homepage: http://pajk.arh.noaa.gov The SimpleRSS application reads RSS data from the Internet, parses the data for headlines (titles), links, and descriptions, and returns that data. The SimpleRSSUI class provides the UI and calls the SimpleRSS class, which does all of the XML processing. To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The RSSViewer application files can be found in the folder Samples/RSSViewer. The application consists of the following files: File Description RSSViewer.mxml The main application file in Flash (FLA) or Flex (MXML). or RSSViewer.fla com/example/programmingas3/rssViewer/RSSParser.as A class that contains methods that use E4X to traverse RSS (XML) data and generate a corresponding HTML representation. RSSData/ak.rss A sample RSS file. The application is set up to read RSS data from the web, at a Flex RSS feed hosted by Adobe. However, you can easily change the application to read RSS data from this document, which uses a slightly different schema than that of the Flex RSS feed. Last updated 3/21/2011 115 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML Reading and parsing XML data Flash Player 9 and later, Adobe AIR 1.0 and later The RSSParser class includes an xmlLoaded() method that converts the input RSS data, stored in the rssXML variable, into an string containing HTML-formatted output, rssOutput. Near the beginning of the method, code sets the default XML namespace if the source RSS data includes a default namespace: if (rssXML.namespace("") != undefined) { default xml namespace = rssXML.namespace(""); } The next lines then loop through the contents of the source XML data, examining each descendant property named item: for each (var item:XML in rssXML..item) { var itemTitle:String = item.title.toString(); var itemDescription:String = item.description.toString(); var itemLink:String = item.link.toString(); outXML += buildItemHTML(itemTitle, itemDescription, itemLink); } The first three lines simply set string variables to represent the title, description and link properties of the item property of the XML data. The next line then calls the buildItemHTML() method to get HTML data in the form of an XMLList object, using the three new string variables as parameters. Assembling XMLList data Flash Player 9 and later, Adobe AIR 1.0 and later The HTML data (an XMLList object) is of the following form: itemTitle

    itemDescription
    More...

    The first lines of the method clear the default xml namespace: default xml namespace = new Namespace(); The default xml namespace directive has function block-level scope. This means that the scope of this declaration is the buildItemHTML() method. The lines that follow assemble the XMLList, based on the string arguments passed to the function: Last updated 3/21/2011 116 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with XML var body:XMLList = new XMLList(); body += new XML("" + itemTitle + ""); var p:XML = new XML("

    " + itemDescription + "

    "); var link:XML = ; link.@href = itemLink; // link.font.@color = "#008000"; // // 0x008000 = green link.font = "More..."; p.appendChild(
    ); p.appendChild(link); body += p; This XMLList object represents string data suitable for an ActionScript HTML text field. The xmlLoaded() method uses the return value of the buildItemHTML() method and converts it to a string: XML.prettyPrinting = false; rssOutput = outXML.toXMLString(); Extracting the title of the RSS feed and sending a custom event Flash Player 9 and later, Adobe AIR 1.0 and later The xmlLoaded() method sets a rssTitle string variable, based on information in the source RSS XML data: rssTitle = rssXML.channel.title.toString(); Finally, the xmlLoaded() method generates an event, which notifies the application that the data is parsed and available: dataWritten = new Event("dataWritten", true); Last updated 3/21/2011 117 Chapter 7: Handling events Flash Player 9 and later, Adobe AIR 1.0 and later An event-handling system allows programmers to respond to user input and system events in a convenient way. The ActionScript 3.0 event model is not only convenient, but also standards-compliant, and well integrated with the display list. Based on the Document Object Model (DOM) Level 3 Events Specification, an industry-standard eventhandling architecture, the new event model provides a powerful yet intuitive event-handling tool for ActionScript programmers. The ActionScript 3.0 event-handling system interacts closely with the display list. To gain a basic understanding of the display list, read “Display programming” on page 143. More Help topics flash.events package Document Object Model (DOM) Level 3 Events Specification Basics of handling events Flash Player 9 and later, Adobe AIR 1.0 and later You can think of events as occurrences of any kind in your SWF file that are of interest to you as a programmer. For example, most SWF files support user interaction of some sort—whether it's something as simple as responding to a mouse click or something more complex, such as accepting and processing data entered into a form. Any such user interaction with your SWF file is considered an event. Events can also occur without any direct user interaction, such as when data has finished loading from a server or when an attached camera has become active. In ActionScript 3.0, each event is represented by an event object, which is an instance of the Event class or one of its subclasses. An event object not only stores information about a specific event, but also contains methods that facilitate manipulation of the event object. For example, when Flash Player or AIR detects a mouse click, it creates an event object (an instance of the MouseEvent class) to represent that particular mouse click event. After creating an event object, Flash Player or AIR dispatches it, which means that the event object is passed to the object that is the target of the event. An object that serves as the destination for a dispatched event object is called an event target. For example, when an attached camera becomes active, Flash Player dispatches an event object directly to the event target, which in this case is the object that represents the camera. If the event target is on the display list, however, the event object is passed down through the display list hierarchy until it reaches the event target. In some cases, the event object then “bubbles” back up the display list hierarchy along the same route. This traversal of the display list hierarchy is called the event flow. You can “listen” for event objects in your code using event listeners. Event listeners are the functions or methods that you write to respond to specific events. To ensure that your program responds to events, you must add event listeners either to the event target or to any display list object that is part of an event object’s event flow. Any time you write event listener code, it follows this basic structure (elements in bold are placeholders you’d fill in for your specific case): Last updated 3/21/2011 118 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events function eventResponse(eventObject:EventType):void { // Actions performed in response to the event go here. } eventTarget.addEventListener(EventType.EVENT_NAME, eventResponse); This code does two things. First, it defines a function, which is the way to specify the actions that will be performed in response to the event. Next, it calls the addEventListener() method of the source object, in essence “subscribing” the function to the specified event so that when the event happens, the function’s actions are carried out. When the event actually happens, the event target checks its list of all the functions and methods that are registered as event listeners. It then calls each one in turn, passing the event object as a parameter. You need to alter four things in this code to create your own event listener. First, you must change the name of the function to the name you want to use (this must be changed in two places, where the code says eventResponse). Second, you must specify the appropriate class name of the event object that is dispatched by the event you want to listen for (EventType in the code), and you must specify the appropriate constant for the specific event (EVENT_NAME in the listing). Third, you must call the addEventListener() method on the object that will dispatch the event (eventTarget in this code). Optionally, you can change the name of the variable used as the function’s parameter (eventObject in this code). Important concepts and terms The following reference list contains important terms that you will encounter when writing event-handling routines: Bubbling Bubbling occurs for some events so that a parent display object can respond to events dispatched by its children. Bubbling phase The part of the event flow in which an event propagates up to parent display objects. The bubbling phase occurs after the capture and target phases. Capture phase The part of the event flow in which an event propagates down from the most general target to the most specific target object. The capture phase occurs before the target and bubbling phases. Default behavior Some events include a behavior that normally happens along with the event, known as the default behavior. For example, when a user types text in a text field, a text input event is raised. The default behavior for that event is to actually display the character that was typed into the text field—but you can override that default behavior (if for some reason you don’t want the typed character to be displayed). Dispatch To notify event listeners that an event has occurred. Event Something that happens to an object that the object can tell other objects about. Event flow When events happen to an object on the display list (an object displayed on the screen), all the objects that contain the object are notified of the event and notify their event listeners in turn. This process starts with the Stage and proceeds through the display list to the actual object where the event occurred, and then proceeds back to the Stage again. This process is known as the event flow. Event object An object that contains information about a particular event’s occurrence, which is sent to all listeners when an event is dispatched. Event target The object that actually dispatches an event. For example, if the user clicks a button that is inside a Sprite that is in turn inside the Stage, all those objects dispatch events, but the event target is the one where the event actually happened—in this case, the clicked button. Listener An object or function that has registered itself with an object, to indicate that it should be notified when a specific event takes place. Last updated 3/21/2011 119 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Target phase The point of the event flow at which an event has reached the most specific possible target. The target phase occurs between the capture and the bubbling phases. How ActionScript 3.0 event handling differs from earlier versions Flash Player 9 and later, Adobe AIR 1.0 and later The most noticeable difference between event handling in ActionScript 3.0 and event handling in previous versions of ActionScript is that in ActionScript 3.0 there is only one system for event handling, whereas in previous versions of ActionScript there are several different event-handling systems. This section begins with an overview of how event handling worked in previous versions of ActionScript, and then discusses how event handling has changed for ActionScript 3.0. Event handling in previous versions of ActionScript Flash Player 9 and later, Adobe AIR 1.0 and later Versions of ActionScript before ActionScript 3.0 provided a number of different ways to handle events: • on() event handlers that can be placed directly on Button and MovieClip instances • onClipEvent() handlers that can be placed directly on MovieClip instances • Callback function properties, such as XML.onload and Camera.onActivity • Event listeners that you register using the addListener() method • The UIEventDispatcher class that partially implemented the DOM event model. Each of these mechanisms presents its own set of advantages and limitations. The on() and onClipEvent() handlers are easy to use, but make subsequent maintenance of projects more difficult because code placed directly on buttons and movie clips can be difficult to find. Callback functions are also simple to implement, but limit you to only one callback function for any given event. Event listeners are more difficult to implement—they require not only the creation of a listener object and function, but also the registration of the listener with the object that generates the event. This increased overhead, however, enables you to create several listener objects and register them all for the same event. The development of components for ActionScript 2.0 engendered yet another event model. This new model, embodied in the UIEventDispatcher class, was based on a subset of the DOM Events Specification. Developers who are familiar with component event handling will find the transition to the new ActionScript 3.0 event model relatively painless. Unfortunately, the syntax used by the various event models overlap in various ways, and differ in others. For example, in ActionScript 2.0, some properties, such as TextField.onChanged, can be used as either a callback function or an event listener. However, the syntax for registering listener objects differs depending on whether you are using one of the six classes that support listeners or the UIEventDispatcher class. For the Key, Mouse, MovieClipLoader, Selection, Stage, and TextField classes, you use the addListener() method, but for components event handling, you use a method called addEventListener(). Another complexity introduced by the different event-handling models was that the scope of the event handler function varied widely depending on the mechanism used. In other words, the meaning of the this keyword was not consistent among the event-handling systems. Last updated 3/21/2011 120 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Event handling in ActionScript 3.0 Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript 3.0 introduces a single event-handling model that replaces the many different event-handling mechanisms that existed in previous versions of the language. The new event model is based on the Document Object Model (DOM) Level 3 Events Specification. Although the SWF file format does not adhere specifically to the Document Object Model standard, there are sufficient similarities between the display list and the structure of the DOM to make implementation of the DOM event model possible. An object on the display list is analogous to a node in the DOM hierarchical structure, and the terms display list object and node are used interchangeably throughout this discussion. The Flash Player and AIR implementation of the DOM event model includes a concept named default behaviors. A default behavior is an action that Flash Player or AIR executes as the normal consequence of certain events. Default behaviors Developers are usually responsible for writing code that responds to events. In some cases, however, a behavior is so commonly associated with an event that Flash Player or AIR automatically executes the behavior unless the developer adds code to cancel it. Because Flash Player or AIR automatically exhibits the behavior, such behaviors are called default behaviors. For example, when a user enters text into a TextField object, the expectation that the text will be displayed in that TextField object is so common that the behavior is built into Flash Player and AIR. If you do not want this default behavior to occur, you can cancel it using the new event-handling system. When a user inputs text into a TextField object, Flash Player or AIR creates an instance of the TextEvent class to represent that user input. To prevent Flash Player or AIR from displaying the text in the TextField object, you must access that specific TextEvent instance and call that instance’s preventDefault() method. Not all default behaviors can be prevented. For example, Flash Player and AIR generate a MouseEvent object when a user double-clicks a word in a TextField object. The default behavior, which cannot be prevented, is that the word under the cursor is highlighted. Many types of event objects do not have associated default behaviors. For example, Flash Player dispatches a connect event object when a network connection is established, but there is no default behavior associated with it. The API documentation for the Event class and its subclasses lists each type of event and describes any associated default behavior, and whether that behavior can be prevented. It is important to understand that default behaviors are associated only with event objects dispatched by Flash Player or AIR, and do not exist for event objects dispatched programmatically through ActionScript. For example, you can use the methods of the EventDispatcher class to dispatch an event object of type textInput, but that event object will not have a default behavior associated with it. In other words, Flash Player and AIR will not display a character in a TextField object as a result of a textInput event that you dispatched programmatically. What’s new for event listeners in ActionScript 3.0 For developers with experience using the ActionScript 2.0 addListener() method, it may be helpful to point out the differences between the ActionScript 2.0 event listener model and the ActionScript 3.0 event model. The following list describes a few major differences between the two event models: • To add event listeners in ActionScript 2.0, you use addListener() in some cases and addEventListener() in others, whereas in ActionScript 3.0, you use addEventListener() in all situations. • There is no event flow in ActionScript 2.0, which means that the addListener() method can be called only on the object that broadcasts the event, whereas in ActionScript 3.0, the addEventListener() method can be called on any object that is part of the event flow. Last updated 3/21/2011 121 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events • In ActionScript 2.0, event listeners can be either functions, methods, or objects, whereas in ActionScript 3.0, only functions or methods can be event listeners. The event flow Flash Player 9 and later, Adobe AIR 1.0 and later Flash Player or AIR dispatches event objects whenever an event occurs. If the event target is not on the display list, Flash Player or AIR dispatches the event object directly to the event target. For example, Flash Player dispatches the progress event object directly to a URLStream object. If the event target is on the display list, however, Flash Player dispatches the event object into the display list, and the event object travels through the display list to the event target. The event flow describes how an event object moves through the display list. The display list is organized in a hierarchy that can be described as a tree. At the top of the display list hierarchy is the Stage, which is a special display object container that serves as the root of the display list. The Stage is represented by the flash.display.Stage class and can only be accessed through a display object. Every display object has a property named stage that refers to the Stage for that application. When Flash Player or AIR dispatches an event object for a display list-related event, that event object makes a roundtrip journey from the Stage to the target node. The DOM Events Specification defines the target node as the node representing the event target. In other words, the target node is the display list object where the event occurred. For example, if a user clicks on a display list object named child1, Flash Player or AIR will dispatch an event object using child1 as the target node. The event flow is conceptually divided into three parts. The first part is called the capture phase; this phase comprises all of the nodes from the Stage to the parent of the target node. The second part is called the target phase, which consists solely of the target node. The third part is called the bubbling phase. The bubbling phase comprises the nodes encountered on the return trip from the parent of the target node back to the Stage. The names of the phases make more sense if you conceive of the display list as a vertical hierarchy with the Stage at the top, as shown in the following diagram: Stage Parent Node Child1 Node Child2 Node Last updated 3/21/2011 122 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events If a user clicks on Child1 Node, Flash Player or AIR dispatches an event object into the event flow. As the following image shows, the object’s journey starts at Stage, moves down to Parent Node, then moves to Child1 Node, and then “bubbles” back up to Stage, moving through Parent Node again on its journey back to Stage. Stage Capture Phase Bubbling Phase Parent Node Child1 Node Child2 Node Target Phase In this example, the capture phase comprises Stage and Parent Node during the initial downward journey. The target phase comprises the time spent at Child1 Node. The bubbling phase comprises Parent Node and Stage as they are encountered during the upward journey back to the root node. The event flow contributes to a more powerful event-handling system than that previously available to ActionScript programmers. In previous versions of ActionScript, the event flow does not exist, which means that event listeners can be added only to the object that generates the event. In ActionScript 3.0, you can add event listeners not only to a target node, but also to any node along the event flow. The ability to add event listeners along the event flow is useful when a user interface component comprises more than one object. For example, a button object often contains a text object that serves as the button’s label. Without the ability to add a listener to the event flow, you would have to add a listener to both the button object and the text object to ensure that you receive notification about click events that occur anywhere on the button. The existence of the event flow, however, allows you to place a single event listener on the button object that handles click events that occur either on the text object or on the areas of the button object that are not obscured by the text object. Not every event object, however, participates in all three phases of the event flow. Some types of events, such as the enterFrame and init event types, are dispatched directly to the target node and participate in neither the capture phase nor the bubbling phase. Other events may target objects that are not on the display list, such as events dispatched to an instance of the Socket class. These event objects will also flow directly to the target object, without participating in the capture and bubbling phases. To find out how a particular event type behaves, you can either check the API documentation or examine the event object's properties. Examining the event object’s properties is described in the following section. Event objects Flash Player 9 and later, Adobe AIR 1.0 and later Event objects serve two main purposes in the new event-handling system. First, event objects represent actual events by storing information about specific events in a set of properties. Second, event objects contain a set of methods that allow you to manipulate event objects and affect the behavior of the event-handling system. To facilitate access to these properties and methods, the Flash Player API defines an Event class that serves as the base class for all event objects. The Event class defines a fundamental set of properties and methods that are common to all event objects. Last updated 3/21/2011 123 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events This section begins with a discussion of the Event class properties, continues with a description of the Event class methods, and concludes with an explanation of why subclasses of the Event class exist. Understanding Event class properties Flash Player 9 and later, Adobe AIR 1.0 and later The Event class defines a number of read-only properties and constants that provide important information about an event object.The following are especially important: • Event object types are represented by constants and stored in the Event.type property. • Whether an event’s default behavior can be prevented is represented by a Boolean value and stored in the Event.cancelable property. • Event flow information is contained in the remaining properties. Event object types Every event object has an associated event type. Event types are stored in the Event.type property as string values. It is useful to know the type of an event object so that your code can distinguish objects of different types from one another. For example, the following code specifies that the clickHandler() listener function should respond to any mouse click event objects that are passed to myDisplayObject: myDisplayObject.addEventListener(MouseEvent.CLICK, clickHandler); Some two dozen event types are associated with the Event class itself and are represented by Event class constants, some of which are shown in the following excerpt from the Event class definition: package flash.events { public class Event { // class constants public static const ACTIVATE:String = "activate"; public static const ADDED:String= "added"; // remaining constants omitted for brevity } } These constants provide an easy way to refer to specific event types. You should use these constants instead of the strings they represent. If you misspell a constant name in your code, the compiler will catch the mistake, but if you instead use strings, a typographical error may not manifest at compile time and could lead to unexpected behavior that could be difficult to debug. For example, when adding an event listener, use the following code: myDisplayObject.addEventListener(MouseEvent.CLICK, clickHandler); rather than: myDisplayObject.addEventListener("click", clickHandler); Default behavior information Your code can check whether the default behavior for any given event object can be prevented by accessing the cancelable property. The cancelable property holds a Boolean value that indicates whether or not a default behavior can be prevented. You can prevent, or cancel, the default behavior associated with a small number of events using the preventDefault() method. For more information, see Cancelling default event behavior under “Understanding Event class methods” on page 125. Last updated 3/21/2011 124 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Event flow information The remaining Event class properties contain important information about an event object and its relationship to the event flow, as described in the following list: • The bubbles property contains information about the parts of the event flow in which the event object participates. • The eventPhase property indicates the current phase in the event flow. • The target property stores a reference to the event target. • The currentTarget property stores a reference to the display list object that is currently processing the event object. The bubbles property An event is said to bubble if its event object participates in the bubbling phase of the event flow, which means that the event object is passed from the target node back through its ancestors until it reaches the Stage. The Event.bubbles property stores a Boolean value that indicates whether the event object participates in the bubbling phase. Because all events that bubble also participate in the capture and target phases, any event that bubbles participates in all three of the event flow phases. If the value is true, the event object participates in all three phases. If the value is false, the event object does not participate in the bubbling phase. The eventPhase property You can determine the event phase for any event object by investigating its eventPhase property. The eventPhase property contains an unsigned integer value that represents one of the three phases of the event flow. The Flash Player API defines a separate EventPhase class that contains three constants that correspond to the three unsigned integer values, as shown in the following code excerpt: package flash.events { public final class EventPhase { public static const CAPTURING_PHASE:uint = 1; public static const AT_TARGET:uint = 2; public static const BUBBLING_PHASE:uint= 3; } } These constants correspond to the three valid values of the eventPhase property. You can use these constants to make your code more readable. For example, if you want to ensure that a function named myFunc() is called only if the event target is in the target stage, you can use the following code to test for this condition: if (event.eventPhase == EventPhase.AT_TARGET) { myFunc(); } The target property The target property holds a reference to the object that is the target of the event. In some cases, this is straightforward, such as when a microphone becomes active, the target of the event object is the Microphone object. If the target is on the display list, however, the display list hierarchy must be taken into account. For example, if a user inputs a mouse click on a point that includes overlapping display list objects, Flash Player and AIR always choose the object that is farthest away from the Stage as the event target. Last updated 3/21/2011 125 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events For complex SWF files, especially those in which buttons are routinely decorated with smaller child objects, the target property may not be used frequently because it will often point to a button’s child object instead of the button. In these situations, the common practice is to add event listeners to the button and use the currentTarget property because it points to the button, whereas the target property may point to a child of the button. The currentTarget property The currentTarget property contains a reference to the object that is currently processing the event object. Although it may seem odd not to know which node is currently processing the event object that you are examining, keep in mind that you can add a listener function to any display object in that event object's event flow, and the listener function can be placed in any location. Moreover, the same listener function can be added to different display objects. As a project increases in size and complexity, the currentTarget property becomes more and more useful. Understanding Event class methods Flash Player 9 and later, Adobe AIR 1.0 and later There are three categories of Event class methods: • Utility methods, which can create copies of an event object or convert it to a string • Event flow methods, which remove event objects from the event flow • Default behavior methods, which prevent default behavior or check whether it has been prevented Event class utility methods There are two utility methods in the Event class. The clone() method allows you to create copies of an event object. The toString() method allows you to generate a string representation of the properties of an event object along with their values. Both of these methods are used internally by the event model system, but are exposed to developers for general use. For advanced developers creating subclasses of the Event class, you must override and implement versions of both utility methods to ensure that the event subclass will work properly. Stopping event flow You can call either the Event.stopPropagation() method or the Event.stopImmediatePropagation() method to prevent an event object from continuing on its way through the event flow. The two methods are nearly identical and differ only in whether the current node’s other event listeners are allowed to execute: • The Event.stopPropagation() method prevents the event object from moving on to the next node, but only after any other event listeners on the current node are allowed to execute. • The Event.stopImmediatePropagation() method also prevents the event object from moving on to the next node, but does not allow any other event listeners on the current node to execute. Calling either of these methods has no effect on whether the default behavior associated with an event occurs. Use the default behavior methods of the Event class to prevent default behavior. Cancelling default event behavior The two methods that pertain to cancelling default behavior are the preventDefault() method and the isDefaultPrevented() method. Call the preventDefault() method to cancel the default behavior associated with an event. To check whether preventDefault() has already been called on an event object, call the isDefaultPrevented() method, which returns a value of true if the method has already been called and false otherwise. Last updated 3/21/2011 126 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events The preventDefault() method will work only if the event’s default behavior can be cancelled. You can check whether this is the case by referring to the API documentation for that event type, or by using ActionScript to examine the cancelable property of the event object. Cancelling the default behavior has no effect on the progress of an event object through the event flow. Use the event flow methods of the Event class to remove an event object from the event flow. Subclasses of the Event class Flash Player 9 and later, Adobe AIR 1.0 and later For many events, the common set of properties defined in the Event class is sufficient. Other events, however, have unique characteristics that cannot be captured by the properties available in the Event class. For these events, ActionScript 3.0 defines several subclasses of the Event class. Each subclass provides additional properties and event types that are unique to that category of events. For example, events related to mouse input have several unique characteristics that cannot be captured by the properties defined in the Event class. The MouseEvent class extends the Event class by adding ten properties that contain information such as the location of the mouse event and whether specific keys were pressed during the mouse event. An Event subclass also contains constants that represent the event types that are associated with the subclass. For example, the MouseEvent class defines constants for several mouse event types, include the click, doubleClick, mouseDown, and mouseUp event types. As described in the section on Event class utility methods under “Event objects” on page 122, when creating an Event subclass you must override the clone() and toString() methods to provide functionality specific to the subclass. Event listeners Flash Player 9 and later, Adobe AIR 1.0 and later Event listeners, which are also called event handlers, are functions that Flash Player and AIR execute in response to specific events. Adding an event listener is a two-step process. First, you create a function or class method for Flash Player or AIR to execute in response to the event. This is sometimes called the listener function or the event handler function. Second, you use the addEventListener() method to register your listener function with the target of the event or any display list object that lies along the appropriate event flow. Creating a listener function Flash Player 9 and later, Adobe AIR 1.0 and later The creation of listener functions is one area where the ActionScript 3.0 event model deviates from the DOM event model. In the DOM event model, there is a clear distinction between an event listener and a listener function: an event listener is an instance of a class that implements the EventListener interface, whereas a listener function is a method of that class named handleEvent(). In the DOM event model, you register the class instance that contains the listener function rather than the actual listener function. In the ActionScript 3.0 event model, there is no distinction between an event listener and a listener function. ActionScript 3.0 does not have an EventListener interface, and listener functions can be defined outside a class or as part of a class. Moreover, listener functions do not have to be named handleEvent()—they can be named with any valid identifier. In ActionScript 3.0, you register the name of the actual listener function. Last updated 3/21/2011 127 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Listener function defined outside of a class The following code creates a simple SWF file that displays a red square shape. A listener function named clickHandler(), which is not part of a class, listens for mouse click events on the red square. package { import flash.display.Sprite; public class ClickExample extends Sprite { public function ClickExample() { var child:ChildSprite = new ChildSprite(); addChild(child); } } } import flash.display.Sprite; import flash.events.MouseEvent; class ChildSprite extends Sprite { public function ChildSprite() { graphics.beginFill(0xFF0000); graphics.drawRect(0,0,100,100); graphics.endFill(); addEventListener(MouseEvent.CLICK, clickHandler); } } function clickHandler(event:MouseEvent):void { trace("clickHandler detected an event of type: " + event.type); trace("the this keyword refers to: " + this); } When a user interacts with the resulting SWF file by clicking on the square, Flash Player or AIR generates the following trace output: clickHandler detected an event of type: click the this keyword refers to: [object global] Notice that the event object is passed as an argument to clickHandler(). This allows your listener function to examine the event object. In this example, you use the event object's type property to ascertain that the event is a click event. The example also checks the value of the this keyword. In this case, this represents the global object, which makes sense because the function is defined outside of any custom class or object. Listener function defined as a class method The following example is identical to the previous example that defines the ClickExample class except that the clickHandler() function is defined as a method of the ChildSprite class: Last updated 3/21/2011 128 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events package { import flash.display.Sprite; public class ClickExample extends Sprite { public function ClickExample() { var child:ChildSprite = new ChildSprite(); addChild(child); } } } import flash.display.Sprite; import flash.events.MouseEvent; class ChildSprite extends Sprite { public function ChildSprite() { graphics.beginFill(0xFF0000); graphics.drawRect(0,0,100,100); graphics.endFill(); addEventListener(MouseEvent.CLICK, clickHandler); } private function clickHandler(event:MouseEvent):void { trace("clickHandler detected an event of type: " + event.type); trace("the this keyword refers to: " + this); } } When a user interacts with the resulting SWF file by clicking on the red square, Flash Player or AIR generates the following trace output: clickHandler detected an event of type: click the this keyword refers to: [object ChildSprite] Note that the this keyword refers to the ChildSprite instance named child. This is a change in behavior from ActionScript 2.0. If you used components in ActionScript 2.0, you may remember that when a class method was passed in to UIEventDispatcher.addEventListener(), the scope of the method was bound to the component that broadcast the event instead of the class in which the listener method was defined. In other words, if you used this technique in ActionScript 2.0, the this keyword would refer to the component broadcasting the event instead of the ChildSprite instance. This was a significant issue for some programmers because it meant that they could not access other methods and properties of the class containing the listener method. As a workaround, ActionScript 2.0 programmers could use the mx.util.Delegate class to change the scope of the listener method. This is no longer necessary, however, because ActionScript 3.0 creates a bound method when addEventListener() is called. As a result, the this keyword refers to the ChildSprite instance named child, and the programmer has access to the other methods and properties of the ChildSprite class. Last updated 3/21/2011 129 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Event listener that should not be used There is a third technique in which you create a generic object with a property that points to a dynamically assigned listener function, but it is not recommended. It is discussed here because it was commonly used in ActionScript 2.0, but should not be used in ActionScript 3.0. This technique is not recommended because the this keyword will refer to the global object instead of your listener object. The following example is identical to the previous ClickExample class example, except that the listener function is defined as part of a generic object named myListenerObj: package { import flash.display.Sprite; public class ClickExample extends Sprite { public function ClickExample() { var child:ChildSprite = new ChildSprite(); addChild(child); } } } import flash.display.Sprite; import flash.events.MouseEvent; class ChildSprite extends Sprite { public function ChildSprite() { graphics.beginFill(0xFF0000); graphics.drawRect(0,0,100,100); graphics.endFill(); addEventListener(MouseEvent.CLICK, myListenerObj.clickHandler); } } var myListenerObj:Object = new Object(); myListenerObj.clickHandler = function (event:MouseEvent):void { trace("clickHandler detected an event of type: " + event.type); trace("the this keyword refers to: " + this); } The results of the trace will look like this: clickHandler detected an event of type: click the this keyword refers to: [object global] You would expect that this would refer to myListenerObj and that the trace output would be [object Object], but instead it refers to the global object. When you pass in a dynamic property name as an argument to addEventListener(), Flash Player or AIR is unable to create a bound method. This is because what you are passing as the listener parameter is nothing more than the memory address of your listener function, and Flash Player and AIR have no way to link that memory address with the myListenerObj instance. Last updated 3/21/2011 130 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Managing event listeners Flash Player 9 and later, Adobe AIR 1.0 and later You can manage your listener functions using the methods of the IEventDispatcher interface. The IEventDispatcher interface is the ActionScript 3.0 version of the EventTarget interface of the DOM event model. Although the name IEventDispatcher may seem to imply that its main purpose is to send (or dispatch) event objects, the methods of this class are actually used much more frequently to register event listeners, check for event listeners, and remove event listeners. The IEventDispatcher interface defines five methods, as shown in the following code: package flash.events { public interface IEventDispatcher { function addEventListener(eventName:String, listener:Object, useCapture:Boolean=false, priority:Integer=0, useWeakReference:Boolean=false):Boolean; function removeEventListener(eventName:String, listener:Object, useCapture:Boolean=false):Boolean; function dispatchEvent(eventObject:Event):Boolean; function hasEventListener(eventName:String):Boolean; function willTrigger(eventName:String):Boolean; } } The Flash Player API implements the IEventDispatcher interface with the EventDispatcher class, which serves as a base class for all classes that can be event targets or part of an event flow. For example, the DisplayObject class inherits from the EventDispatcher class. This means that any object on the display list has access to the methods of the IEventDispatcher interface. Adding event listeners The addEventListener() method is the workhorse of the IEventDispatcher interface. You use it to register your listener functions. The two required parameters are type and listener. You use the type parameter to specify the type of event. You use the listener parameter to specify the listener function that will execute when the event occurs. The listener parameter can be a reference to either a function or a class method. Do not use parentheses when you specify the listener parameter. For example, the clickHandler() function is specified without parentheses in the following call to the addEventListener() method: addEventListener(MouseEvent.CLICK, clickHandler) The useCapture parameter of the addEventListener() method allows you to control the event flow phase on which your listener will be active. If useCapture is set to true, your listener will be active during the capture phase of the event flow. If useCapture is set to false, your listener will be active during the target and bubbling phases of the event flow. To listen for an event during all phases of the event flow, you must call addEventListener() twice, once with useCapture set to true, and then again with useCapture set to false. Last updated 3/21/2011 131 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events The priority parameter of the addEventListener() method is not an official part of the DOM Level 3 event model. It is included in ActionScript 3.0 to provide you with more flexibility in organizing your event listeners. When you call addEventListener(), you can set the priority for that event listener by passing an integer value as the priority parameter. The default value is 0, but you can set it to negative or positive integer values. The higher the number, the sooner that event listener will be executed. Event listeners with the same priority are executed in the order that they were added, so the earlier a listener is added, the sooner it will be executed. The useWeakReference parameter allows you to specify whether the reference to the listener function is weak or normal. Setting this parameter to true allows you to avoid situations in which listener functions persist in memory even though they are no longer needed. Flash Player and AIR use a technique called garbage collection to clear objects from memory that are no longer in use. An object is considered no longer in use if no references to it exist. The garbage collector disregards weak references, which means that a listener function that has only a weak reference pointing to it is eligible for garbage collection. Removing event listeners You can use the removeEventListener() method to remove an event listener that you no longer need. It is a good idea to remove any listeners that will no longer be used. Required parameters include the eventName and listener parameters, which are the same as the required parameters for the addEventListener() method. Recall that you can listen for events during all event phases by calling addEventListener() twice, once with useCapture set to true, and then again with it set to false. To remove both event listeners, you would need to call removeEventListener() twice, once with useCapture set to true, and then again with it set to false. Dispatching events The dispatchEvent() method can be used by advanced programmers to dispatch a custom event object into the event flow. The only parameter accepted by this method is a reference to an event object, which must be an instance of the Event class or a subclass of the Event class. Once dispatched, the target property of the event object is set to the object on which dispatchEvent() was called. Checking for existing event listeners The final two methods of the IEventDispatcher interface provide useful information about the existence of event listeners. The hasEventListener() method returns true if an event listener is found for a specific event type on a particular display list object. The willTrigger() method also returns true if a listener is found for a particular display list object, but willTrigger() checks for listeners not only on that display object, but also on all of that display list object’s ancestors for all phases of the event flow. Last updated 3/21/2011 132 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events Error events without listeners Flash Player 9 and later, Adobe AIR 1.0 and later Exceptions, rather than events, are the primary mechanism for error handling in ActionScript 3.0, but exception handling does not work for asynchronous operations such as loading files. If an error occurs during such an asynchronous operation, Flash Player and AIR dispatch an error event object. If you do not create a listener for the error event, the debugger versions of Flash Player and AIR will bring up a dialog box with information about the error. For example, the debugger version of Flash Player produces the following dialog box describing the error when the application attempts to load a file from an invalid URL: Most error events are based on the ErrorEvent class, and as such will have a property named text that is used to store the error message that Flash Player or AIR displays. The two exceptions are the StatusEvent and NetStatusEvent classes. Both of these classes have a level property (StatusEvent.level and NetStatusEvent.info.level). When the value of the level property is "error", these event types are considered to be error events. An error event will not cause a SWF file to stop running. It will manifest only as a dialog box on the debugger versions of the browser plug-ins and stand-alone players, as a message in the output panel in the authoring player, and as an entry in the log file for Adobe Flash Builder. It will not manifest at all in the release versions of Flash Player or AIR. Event handling example: Alarm Clock Flash Player 9 and later, Adobe AIR 1.0 and later The Alarm Clock example consists of a clock that allows the user to specify a time at which an alarm will go off, as well as a message to be displayed at that time. The Alarm Clock example builds on the SimpleClock application from “Working with dates and times” on page 1 Alarm Clock illustrates several aspects of working with events in ActionScript 3.0, including: • Listening and responding to an event • Notifying listeners of an event • Creating a custom event type To get the Flash Professional application files for this sample, see http://www.adobe.com/go/learn_programmingAS3samples_flash. To get the Flex application files for this sample, see http://www.adobe.com/go/as3examples. The Alarm Clock application files can be found in the Samples/AlarmClock folder. The application includes these files: Last updated 3/21/2011 133 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events File Description AlarmClockApp.mxml The main application file in Flash (FLA) or Flex (MXML). or AlarmClockApp.fla com/example/programmingas3/clock/AlarmClock.as A class which extends the SimpleClock class, adding alarm clock functionality. com/example/programmingas3/clock/AlarmEvent.as A custom event class (a subclass of flash.events.Event) which serves as the event object for the AlarmClock class’s alarm event. com/example/programmingas3/clock/AnalogClockFace.as Draws a round clock face and hour, minute, and seconds hands based on the time (described in the SimpleClock example). com/example/programmingas3/clock/SimpleClock.as A clock interface component with simple timekeeping functionality (described in the SimpleClock example). Alarm Clock overview Flash Player 9 and later, Adobe AIR 1.0 and later The primary functionality of the clock in this example, including tracking the time and displaying the clock face, reuses the SimpleClock application code, which is described in “Date and time example: Simple analog clock” on page 6. The AlarmClock class extends the SimpleClock class from that example by adding the functionality required for an alarm clock, including setting the alarm time and providing notification when the alarm “goes off.” Providing notification when something happens is the job that events are made for. The AlarmClock class exposes the Alarm event, which other objects can listen for in order to perform desired actions. In addition, the AlarmClock class uses an instance of the Timer class to determine when to trigger its alarm. Like the AlarmClock class, the Timer class provides an event to notify other objects (an AlarmClock instance, in this case) when a certain amount of time has passed. As with most ActionScript applications, events form an important part of the functionality of the Alarm Clock sample application. Triggering the alarm Flash Player 9 and later, Adobe AIR 1.0 and later As mentioned previously, the only functionality that the AlarmClock class actually provides relates to setting and triggering the alarm. The built-in Timer class (flash.utils.Timer) provides a way for a developer to define code that will be executed after a specified amount of time. The AlarmClock class uses a Timer instance to determine when to set off the alarm. Last updated 3/21/2011 134 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events import flash.events.TimerEvent; import flash.utils.Timer; /** * The Timer that will be used for the alarm. */ public var alarmTimer:Timer; ... /** * Instantiates a new AlarmClock of a given size. */ public override function initClock(faceSize:Number = 200):void { super.initClock(faceSize); alarmTimer = new Timer(0, 1); alarmTimer.addEventListener(TimerEvent.TIMER, onAlarm); } The Timer instance defined in the AlarmClock class is named alarmTimer. The initClock() method, which performs necessary setup operations for the AlarmClock instance, does two things with the alarmTimer variable. First, the variable is instantiated with parameters instructing the Timer instance to wait 0 milliseconds and only trigger its timer event one time. After instantiating alarmTimer, the code calls that variable’s addEventListener() method to indicate that it wants to listen to that variable’s timer event. A Timer instance works by dispatching its timer event after a specified amount of time has passed. The AlarmClock class will need to know when the timer event is dispatched in order to set off its own alarm. By calling addEventListener(), the AlarmClock code registers itself as a listener with alarmTimer. The two parameters indicate that the AlarmClock class wants to listen for the timer event (indicated by the constant TimerEvent.TIMER), and that when the event happens, the AlarmClock class’s onAlarm() method should be called in response to the event. In order to actually set the alarm, the AlarmClock class’s setAlarm() method is called, as follows: Last updated 3/21/2011 135 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events /** * Sets the time at which the alarm should go off. * @param hour The hour portion of the alarm time. * @param minutes The minutes portion of the alarm time. * @param message The message to display when the alarm goes off. * @return The time at which the alarm will go off. */ public function setAlarm(hour:Number = 0, minutes:Number = 0, message:String = "Alarm!"):Date { this.alarmMessage = message; var now:Date = new Date(); // Create this time on today's date. alarmTime = new Date(now.fullYear, now.month, now.date, hour, minutes); // Determine if the specified time has already passed today. if (alarmTime <= now) { alarmTime.setTime(alarmTime.time + MILLISECONDS_PER_DAY); } // Stop the alarm timer if it's currently set. alarmTimer.reset(); // Calculate how many milliseconds should pass before the alarm should // go off (the difference between the alarm time and now) and set that // value as the delay for the alarm timer. alarmTimer.delay = Math.max(1000, alarmTime.time - now.time); alarmTimer.start(); return alarmTime; } This method does several things, including storing the alarm message and creating a Date object (alarmTime) representing the actual moment in time when the alarm is to go off. Of most relevance to the current discussion, in the final several lines of the method, the alarmTimer variable’s timer is set and activated. First, its reset() method is called, stopping the timer and resetting it in case it is already running. Next, the current time (represented by the now variable) is subtracted from the alarmTime variable’s value to determine how many milliseconds need to pass before the alarm goes off. The Timer class doesn’t trigger its timer event at an absolute time, so it is this relative time difference that is assigned to the delay property of alarmTimer. Finally, the start() method is called to actually start the timer. Once the specified amount of time has passed, alarmTimer dispatches the timer event. Because the AlarmClock class registered its onAlarm() method as a listener for that event, when the timer event happens, onAlarm() is called. /** * Called when the timer event is dispatched. */ public function onAlarm(event:TimerEvent):void { trace("Alarm!"); var alarm:AlarmEvent = new AlarmEvent(this.alarmMessage); this.dispatchEvent(alarm); } Last updated 3/21/2011 136 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events A method that is registered as an event listener must be defined with the appropriate signature (that is, the set of parameters and return type of the method). To be a listener for the Timer class’s timer event, a method must define one parameter whose data type is TimerEvent (flash.events.TimerEvent), a subclass of the Event class. When the Timer instance calls its event listeners, it passes a TimerEvent instance as the event object. Notifying others of the alarm Flash Player 9 and later, Adobe AIR 1.0 and later Like the Timer class, the AlarmClock class provides an event that allows other code to receive notifications when the alarm goes off. For a class to use the event-handling framework built into ActionScript, that class must implement the flash.events.IEventDispatcher interface. Most commonly, this is done by extending the flash.events.EventDispatcher class, which provides a standard implementation of IEventDispatcher (or by extending one of EventDispatcher’s subclasses). As described previously, the AlarmClock class extends the SimpleClock class, which (through a chain of inheritance) extends the EventDispatcher class. All of this means that the AlarmClock class already has built-in functionality to provide its own events. Other code can register to be notified of the AlarmClock class’s alarm event by calling the addEventListener() method that AlarmClock inherits from EventDispatcher. When an AlarmClock instance is ready to notify other code that its alarm event has been raised, it does so by calling the dispatchEvent() method, which is also inherited from EventDispatcher. var alarm:AlarmEvent = new AlarmEvent(this.alarmMessage); this.dispatchEvent(alarm); These lines of code are taken from the AlarmClock class’s onAlarm() method (shown in its entirety previously). The AlarmClock instance’s dispatchEvent() method is called, which in turn notifies all the registered listeners that the AlarmClock instance’s alarm event has been triggered. The parameter that is passed to dispatchEvent() is the event object that will be passed along to the listener methods. In this case, it is an instance of the AlarmEvent class, an Event subclass created specifically for this example. Providing a custom alarm event Flash Player 9 and later, Adobe AIR 1.0 and later All event listeners receive an event object parameter with information about the particular event being triggered. In many cases, the event object is an instance of the Event class. However, in some cases it is useful to provide additional information to event listeners. A common way to accomplish this is to define a new class, a subclass of the Event class, and use an instance of that class as the event object. In this example, an AlarmEvent instance is used as the event object when the AlarmClock class’s alarm event is dispatched. The AlarmEvent class, shown here, provides additional information about the alarm event, specifically the alarm message: Last updated 3/21/2011 137 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events import flash.events.Event; /** * This custom Event class adds a message property to a basic Event. */ public class AlarmEvent extends Event { /** * The name of the new AlarmEvent type. */ public static const ALARM:String = "alarm"; /** * A text message that can be passed to an event handler * with this event object. */ public var message:String; /** *Constructor. *@param message The text to display when the alarm goes off. */ public function AlarmEvent(message:String = "ALARM!") { super(ALARM); this.message = message; } ... } The best way to create a custom event object class is to define a class that extends the Event class, as shown in the preceding example. To supplement the inherited functionality, the AlarmEvent class defines a property message that contains the text of the alarm message associated with the event; the message value is passed in as a parameter in the AlarmEvent constructor. The AlarmEvent class also defines the constant ALARM, which can be used to refer to the specific event (alarm) when calling the AlarmClock class’s addEventListener() method. In addition to adding custom functionality, every Event subclass must override the inherited clone() method as part of the ActionScript event-handling framework. Event subclasses can also optionally override the inherited toString() method to include the custom event’s properties in the value returned when the toString() method is called. Last updated 3/21/2011 138 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Handling events /** * Creates and returns a copy of the current instance. * @return A copy of the current instance. */ public override function clone():Event { return new AlarmEvent(message); } /** * Returns a String containing all the properties of the current * instance. * @return A string representation of the current instance. */ public override function toString():String { return formatToString("AlarmEvent", "type", "bubbles", "cancelable", "eventPhase", "message"); } The overridden clone() method needs to return a new instance of the custom Event subclass, with all the custom properties set to match the current instance. In the overridden toString() method, the utility method formatToString() (inherited from Event) is used to provide a string with the name of the custom type, as well as the names and values of all its properties. Last updated 3/21/2011 139 Chapter 8: Working with application domains Flash Player 9 and later, Adobe AIR 1.0 and later The purpose of the ApplicationDomain class is to store a table of ActionScript 3.0 definitions. All code in a SWF file is defined to exist in an application domain. You use application domains to partition classes that are in the same security domain. This allows multiple definitions of the same class to exist and also lets children reuse parent definitions. You can use application domains when loading an external SWF file written in ActionScript 3.0 using the Loader class API. (Note that you cannot use application domains when loading an image or SWF file written in ActionScript 1.0 or ActionScript 2.0.) All ActionScript 3.0 definitions contained in the loaded class are stored in the application domain. When loading the SWF file, you can specify that the file be included in the same application domain as that of the Loader object, by setting the applicationDomain parameter of the LoaderContext object to ApplicationDomain.currentDomain. By putting the loaded SWF file in the same application domain, you can access its classes directly. This can be useful if you are loading a SWF file that contains embedded media, which you can access via their associated class names, or if you want to access the loaded SWF file’s methods. The following example assumes it has access to a separate Greeter.swf file that defines a public method named welcome(): Last updated 3/21/2011 140 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with application domains package { import import import import import import flash.display.Loader; flash.display.Sprite; flash.events.*; flash.net.URLRequest; flash.system.ApplicationDomain; flash.system.LoaderContext; public class ApplicationDomainExample extends Sprite { private var ldr:Loader; public function ApplicationDomainExample() { ldr = new Loader(); var req:URLRequest = new URLRequest("Greeter.swf"); var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); ldr.load(req, ldrContext); } private function completeHandler(event:Event):void { var myGreeter:Class = ApplicationDomain.currentDomain.getDefinition("Greeter") as Class; var myGreeter:Greeter = Greeter(event.target.content); var message:String = myGreeter.welcome("Tommy"); trace(message); // Hello, Tommy } } } Also see the ApplicationDomain class example of the ActionScript 3.0 Reference for the Adobe Flash Platform. Other things to keep in mind when you work with application domains include the following: • All code in a SWF file is defined to exist in an application domain. The current domain is where your main application runs. The system domain contains all application domains, including the current domain, which means that it contains all Flash Player classes. • All application domains, except the system domain, have an associated parent domain. The parent domain for your main application's application domain is the system domain. Loaded classes are defined only when their parent doesn't already define them. You cannot override a loaded class definition with a newer definition. The following diagram shows an application that loads content from various SWF files within a single domain, domain1.com. Depending on the content you load, different application domains can be used. The text that follows describes the logic used to set the appropriate application domain for each SWF file in the application. Last updated 3/21/2011 141 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with application domains Security domain: domain1.com Stage Application domain 1 mx.core.Application application1.swf module1.swf Loader Application Loader Loader Loader Module B Application domain 3 module3.swf C Module Application domain 2 mx.core.Application A application2.swf Module A. Usage A B. Usage B C. Usage C The main application file is application1.swf. It contains Loader objects that load content from other SWF files. In this scenario, the current domain is Application domain 1. Usage A, usage B, and usage C illustrate different techniques for setting the appropriate application domain for each SWF file in an application. Usage A Partition the child SWF file by creating a child of the system domain. In the diagram, Application domain 2 is created as a child of the system domain.The application2.swf file is loaded in Application domain 2, and its class definitions are thus partitioned from the classes defined in application1.swf. One use of this technique is to have an old application dynamically loading a newer version of the same application without conflict. There is no conflict because although the same class names are used, they are partitioned into different application domains. The following code creates an application domain that is a child of the system domain, and starts loading a SWF using that application domain: var appDomainA:ApplicationDomain = new ApplicationDomain(); var contextA:LoaderContext = new LoaderContext(false, appDomainA); var loaderA:Loader = new Loader(); loaderA.load(new URLRequest("application2.swf"), contextA); Usage B: Add new class definitions to current class definitions. The application domain of module1.swf is set to the current domain (Application domain 1). This lets you add to the application’s current set of class definitions with new class definitions. This could be used for a run-time shared library of the main application. The loaded SWF is treated as a remote shared library (RSL). Use this technique to load RSLs by a preloader before the application starts. The following code loads a SWF, setting its application domain to the current domain: Last updated 3/21/2011 142 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with application domains var appDomainB:ApplicationDomain = ApplicationDomain.currentDomain; var contextB:LoaderContext = new LoaderContext(false, appDomainB); var loaderB:Loader = new Loader(); loaderB.load(new URLRequest("module1.swf"), contextB); Usage C: Use the parent’s class definitions by creating a new child domain of the current domain. The application domain of module3.swf is a child of the current domain, and the child uses the parent's versions of all classes. One use of this technique might be a module of a multiple-screen rich Internet application (RIA), loaded as a child of the main application, that uses the main application's types. If you can ensure that all classes are always updated to be backward compatible, and that the loading application is always newer than the things it loads, the children will use the parent versions. Having a new application domain also allows you to unload all the class definitions for garbage collection, if you can ensure that you do not continue to have references to the child SWF. This technique lets loaded modules share the loader's singleton objects and static class members. The following code creates a new child domain of the current domain, and starts loading a SWF using that application domain: var appDomainC:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); var contextC:LoaderContext = new LoaderContext(false, appDomainC); var loaderC:Loader = new Loader(); loaderC.load(new URLRequest("module3.swf"), contextC); Last updated 3/21/2011 143 Chapter 9: Display programming Flash Player 9 and later, Adobe AIR 1.0 and later Visual elements are programmed in Adobe® ActionScript® 3.0 by working with display objects on the display stage. For example, you can add, move, remove, and order display objects, apply filters and masks, draw vector and bitmap graphics, and perform three-dimensional transformations using the ActionScript display programming API. The primary classes used for display programming are part of the flash.display package. Note: Adobe® AIR™ provides the HTMLoader object for rendering and displaying HTML content. The HTMLLoader renders the visual elements of the HTML DOM as a single display object. You cannot access the individual elements of the DOM directly through the ActionScript display list hierarchy. Instead, you access these DOM elements using the separate DOM API provided by the HTMLLoader. Basics of display programming Flash Player 9 and later, Adobe AIR 1.0 and later Each application built with ActionScript 3.0 has a hierarchy of displayed objects known as the display list, illustrated below. The display list contains all the visible elements in the application. Stage Instance of the main class of the SWF file Display Object Container Display Object Display Object Container Display Object Display Object Container Display Object Display Object Container Last updated 3/21/2011 144 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming As the illustration shows, display elements fall into one or more of the following groups: • The Stage The Stage is the base container of display objects. Each application has one Stage object, which contains all onscreen display objects. The Stage is the top-level container and is at the top of the display list hierarchy: Each SWF file has an associated ActionScript class, known as the main class of the SWF file. When a SWF file opens in Flash Player or Adobe AIR, Flash Player or AIR calls the constructor function for that class and the instance that is created (which is always a type of display object) is added as a child of the Stage object. The main class of a SWF file always extends the Sprite class (for more information, see “Advantages of the display list approach” on page 148). You can access the Stage through the stage property of any DisplayObject instance. For more information, see “Setting Stage properties” on page 156. • Display objects In ActionScript 3.0, all elements that appear on screen in an application are types of display objects. The flash.display package includes a DisplayObject class, which is a base class extended by a number of other classes. These different classes represent different types of display objects, such as vector shapes, movie clips, and text fields, to name a few. For an overview of these classes, see “Advantages of the display list approach” on page 148. • Display object containers Display object containers are special types of display objects that, in addition to having their own visual representation, can also contain child objects that are also display objects. The DisplayObjectContainer class is a subclass of the DisplayObject class. A DisplayObjectContainer object can contain multiple display objects in its childlist. For example, the following illustration shows a type of DisplayObjectContainer object known as a Sprite that contains various display objects: A B C D A. A SimpleButton object. This type of display object has different “up,” “down,” and “over” states. B. A Bitmap object. In this case, the Bitmap object was loaded from an external JPEG through a Loader object. C. A Shape object. The “picture frame” contains a rounded rectangle that is drawn in ActionScript. This Shape object has a Drop Shadow filter applied to it. D. A TextField object. In the context of discussing display objects, DisplayObjectContainer objects are also known as display object containers or simply containers. As noted earlier, the Stage is a display object container. Last updated 3/21/2011 145 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Although all visible display objects inherit from the DisplayObject class, the type of each is of a specific subclass of DisplayObject class. For example, there is a constructor function for the Shape class or the Video class, but there is no constructor function for the DisplayObject class. Important concepts and terms The following reference list contains important terms that you will encounter when programming ActionScript graphics: Alpha The color value representing the amount of transparency (or more correctly, the amount of opacity) in a color. For example, a color with an alpha channel value of 60% only shows 60% of its full strength, and is 40% transparent. Bitmap graphic A graphic that is defined in the computer as a grid (rows and columns) of colored pixels. Commonly bitmap graphics include digital photos and similar images. Blending mode A specification of how the contents of two overlapping images should interact. Commonly an opaque image on top of another image simply blocks the image underneath so that it isn’t visible at all; however, different blending modes cause the colors of the images to blend together in different ways so the resulting content is some combination of the two images. Display list The hierarchy of display objects that will be rendered as visible screen content by Flash Player and AIR. The Stage is the root of the display list, and all the display objects that are attached to the Stage or one of its children form the display list (even if the object isn’t actually rendered, for example if it’s outside the boundaries of the Stage). Display object An object which represents some type of visual content in Flash Player or AIR. Only display objects can be included in the display list, and all display object classes are subclasses of the DisplayObject class. Display object container A special type of display object which can contain child display objects in addition to (generally) having its own visual representation. Main class of the SWF file The class that defines the behavior for the outermost display object in a SWF file, which conceptually is the class for the SWF file itself. For instance, in a SWF created in Flash authoring, the main class is the document class. It has a “main timeline” which contains all other timelines; the main class of the SWF file is the class of which the main timeline is an instance. Masking A technique of hiding from view certain parts of an image (or conversely, only allowing certain parts of an image to display). The portions of the mask image become transparent, so content underneath shows through. The term is related to painter’s masking tape that is used to prevent paint from being applied to certain areas. Stage The visual container that is the base or background of all visual content in a SWF. Transformation An adjustment to a visual characteristic of a graphic, such as rotating the object, altering its scale, skewing or distorting its shape, or altering its color. Vector graphic A graphic that is defined in the computer as lines and shapes drawn with particular characteristics (such as thickness, length, size, angle, and position). Last updated 3/21/2011 146 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Core display classes Flash Player 9 and later, Adobe AIR 1.0 and later The ActionScript 3.0 flash.display package includes classes for visual objects that can appear in Flash Player or AIR. The following illustration shows the subclass relationships of these core display object classes. DisplayObject AVM1Movie Bitmap InteractiveObject DisplayObjectContainer SimpleButton Loader Sprite MorphShape Shape StaticText Video TextField Stage MovieClip The illustration shows the class inheritance of display object classes. Note that some of these classes, specifically StaticText, TextField, and Video, are not in the flash.display package, but they still inherit from the DisplayObject class. All classes that extend the DisplayObject class inherit its methods and properties. For more information, see “Properties and methods of the DisplayObject class” on page 150. You can instantiate objects of the following classes contained in the flash.display package: • Bitmap—You use the Bitmap class to define bitmap objects, either loaded from external files or rendered through ActionScript. You can load bitmaps from external files through the Loader class. You can load GIF, JPG, or PNG files. You can also create a BitmapData object with custom data and then create a Bitmap object that uses that data. You can use the methods of the BitmapData class to alter bitmaps, whether they are loaded or created in ActionScript. For more information, see “Loading display objects” on page 189 and “Working with bitmaps” on page 232. • Loader—You use the Loader class to load external assets (either SWF files or graphics). For more information, see “Loading display content dynamically” on page 189. • Shape—You use the Shape class to create vector graphics, such as rectangles, lines, circles, and so on. For more information, see “Using the drawing API” on page 211. • SimpleButton—A SimpleButton object is the ActionScript representation of a button symbol created in the Flash authoring tool. A SimpleButton instance has four button states: up, down, over, and hit test (the area that responds to mouse and keyboard events). • Sprite—A Sprite object can contain graphics of its own, and it can contain child display objects. (The Sprite class extends the DisplayObjectContainer class). For more information, see “Working with display object containers” on page 151 and “Using the drawing API” on page 211. Last updated 3/21/2011 147 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming • MovieClip—A MovieClip object is the ActionScript form of a movie clip symbol created in the Flash authoring tool. In practice, a MovieClip is similar to a Sprite object, except that it also has a timeline. For more information, see “Working with movie clips” on page 310. The following classes, which are not in the flash.display package, are subclasses of the DisplayObject class: • The TextField class, included in the flash.text package, is a display object for text display and input. For more information, see “Basics of Working with text” on page 349. • The TextLine class, included in the flash.text.engine package, is the display object used to display lines of text composed by the Flash Text Engine and the Text Layout Framework. For more information, see “Using the Flash Text Engine” on page 375 and “Using the Text Layout Framework” on page 404. • The Video class, included in the flash.media package, is the display object used for displaying video files. For more information, see “Working with video” on page 452. The following classes in the flash.display package extend the DisplayObject class, but you cannot create instances of them. Instead, they serve as parent classes for other display objects, combining common functionality into a single class. • AVM1Movie—The AVM1Movie class is used to represent loaded SWF files that are authored in ActionScript 1.0 and 2.0. • DisplayObjectContainer—The Loader, Stage, Sprite, and MovieClip classes each extend the DisplayObjectContainer class. For more information, see “Working with display object containers” on page 151. • InteractiveObject—InteractiveObject is the base class for all objects used to interact with the mouse and keyboard. SimpleButton, TextField, Loader, Sprite, Stage, and MovieClip objects are all subclasses of the InteractiveObject class. For more information on creating mouse and keyboard interaction, see “Basics of user interaction” on page 523. • MorphShape—These objects are created when you create a shape tween in the Flash authoring tool. You cannot instantiate them using ActionScript, but they can be accessed from the display list. • Stage—The Stage class extends the DisplayObjectContainer class. There is one Stage instance for an application, and it is at the top of the display list hierarchy. To access the Stage, use the stage property of any DisplayObject instance. For more information, see “Setting Stage properties” on page 156. Also, the StaticText class, in the flash.text package, extends the DisplayObject class, but you cannot create an instance of it in code. Static text fields are created only in Flash. The following classes are not display objects or display object containers, and do not appear in the display list, but do display graphics on the stage. These classes draw into a rectangle, called a viewport, positioned relative to the stage. • StageVideo—The StageVideo class displays video content, using hardware acceleration, when possible. This class is available starting in Flash Player 10.2 and AIR 2.5 (in the AIR for TV profiles). For more information, see “Using the StageVideo class for hardware-accelerated rendering” on page 486. • StageWebView—The StageWebView class displays HTML content. This class is available starting in AIR 2.5. For more infromation, see “StageWebView objects” on page 997. Last updated 3/21/2011 148 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Advantages of the display list approach Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 3.0, there are separate classes for different types of display objects. In ActionScript 1.0 and 2.0, many of the same types of objects are all included in one class: the MovieClip class. This individualization of classes and the hierarchical structure of display lists have the following benefits: • More efficient rendering and reduced memory usage • Improved depth management • Full traversal of the display list • Off-list display objects • Easier subclassing of display objects More efficient rendering and smaller file sizes Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 1.0 and 2.0, you could draw shapes only in a MovieClip object. In ActionScript 3.0, there are simpler display object classes in which you can draw shapes. Because these ActionScript 3.0 display object classes do not include the full set of methods and properties that a MovieClip object includes, they are less taxing on memory and processor resources. For example, each MovieClip object includes properties for the timeline of the movie clip, whereas a Shape object does not. The properties for managing the timeline can use a lot of memory and processor resources. In ActionScript 3.0, using the Shape object results in better performance. The Shape object has less overhead than the more complex MovieClip object. Flash Player and AIR do not need to manage unused MovieClip properties, which improves speed and reduces the memory footprint the object uses. Improved depth management Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 1.0 and 2.0, depth was managed through a linear depth management scheme and methods such as getNextHighestDepth(). ActionScript 3.0 includes the DisplayObjectContainer class, which has more convenient methods and properties for managing the depth of display objects. In ActionScript 3.0, when you move a display object to a new position in the child list of a DisplayObjectContainer instance, the other children in the display object container are repositioned automatically and assigned appropriate child index positions in the display object container. Also, in ActionScript 3.0 it is always possible to discover all of the child objects of any display object container. Every DisplayObjectContainer instance has a numChildren property, which lists the number of children in the display object container. And since the child list of a display object container is always an indexed list, you can examine every object in the list from index position 0 through the last index position (numChildren - 1). This was not possible with the methods and properties of a MovieClip object in ActionScript 1.0 and 2.0. Last updated 3/21/2011 149 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming In ActionScript 3.0, you can easily traverse the display list sequentially; there are no gaps in the index numbers of a child list of a display object container. Traversing the display list and managing the depth of objects is much easier than was possible in ActionScript 1.0 and 2.0. In ActionScript 1.0 and 2.0, a movie clip could contain objects with intermittent gaps in the depth order, which could make it difficult to traverse the list of object. In ActionScript 3.0, each child list of a display object container is cached internally as an array, resulting in very fast lookups (by index). Looping through all children of a display object container is also very fast. In ActionScript 3.0, you can also access children in a display object container by using the getChildByName() method of the DisplayObjectContainer class. Full traversal of the display list Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 1.0 and 2.0, you could not access some objects, such as vector shapes, that were drawn in the Flash authoring tool. In ActionScript 3.0, you can access all objects on the display list—both those created using ActionScript and all display objects created in the Flash authoring tool. For details, see “Traversing the display list” on page 155. Off-list display objects Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 3.0, you can create display objects that are not on the visible display list. These are known as off-list display objects. A display object is added to the visible display list only when you call the addChild() or addChildAt() method of a DisplayObjectContainer instance that has already been added to the display list. You can use off-list display objects to assemble complex display objects, such as those that have multiple display object containers containing multiple display objects. By keeping display objects off-list, you can assemble complicated objects without using the processing time to render these display objects. You can then add an off-list display object to the display list when it is needed. Also, you can move a child of a display object container on and off the display list and to any desired position in the display list at will. Easier subclassing of display objects Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 1.0 and 2.0, you would often have to add new MovieClip objects to a SWF file to create basic shapes or to display bitmaps. In ActionScript 3.0, the DisplayObject class includes many built-in subclasses, including Shape and Bitmap. Because the classes in ActionScript 3.0 are more specialized for specific types of objects, it is easier to create basic subclasses of the built-in classes. For example, in order to draw a circle in ActionScript 2.0, you could create a CustomCircle class that extends the MovieClip class when an object of the custom class is instantiated. However, that class would also include a number of properties and methods from the MovieClip class (such as totalFrames) that do not apply to the class. In ActionScript 3.0, however, you can create a CustomCircle class that extends the Shape object, and as such does not include the unrelated properties and methods that are contained in the MovieClip class. The following code shows an example of a CustomCircle class: Last updated 3/21/2011 150 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming import flash.display.*; public class CustomCircle extends Shape { var xPos:Number; var yPos:Number; var radius:Number; var color:uint; public function CustomCircle(xInput:Number, yInput:Number, rInput:Number, colorInput:uint) { xPos = xInput; yPos = yInput; radius = rInput; color = colorInput; this.graphics.beginFill(color); this.graphics.drawCircle(xPos, yPos, radius); } } Working with display objects Flash Player 9 and later, Adobe AIR 1.0 and later Now that you understand the basic concepts of the Stage, display objects, display object containers, and the display list, this section provides you with some more specific information about working with display objects in ActionScript 3.0. Properties and methods of the DisplayObject class Flash Player 9 and later, Adobe AIR 1.0 and later All display objects are subclasses of the DisplayObject class, and as such they inherit the properties and methods of the DisplayObject class. The properties inherited are basic properties that apply to all display objects. For example, each display object has an x property and a y property that specifies the object’s position in its display object container. You cannot create a DisplayObject instance using the DisplayObject class constructor. You must create another type of object (an object that is a subclass of the DisplayObject class), such as a Sprite, to instantiate an object with the new operator. Also, if you want to create a custom display object class, you must create a subclass of one of the display object subclasses that has a usable constructor function (such as the Shape class or the Sprite class). For more information, see the DisplayObject class description in the ActionScript 3.0 Reference for the Adobe Flash Platform. Adding display objects to the display list Flash Player 9 and later, Adobe AIR 1.0 and later When you instantiate a display object, it will not appear on-screen (on the Stage) until you add the display object instance to a display object container that is on the display list. For example, in the following code, the myText TextField object would not be visible if you omitted the last line of code. In the last line of code, the this keyword must refer to a display object container that is already added to the display list. Last updated 3/21/2011 151 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming import flash.display.*; import flash.text.TextField; var myText:TextField = new TextField(); myText.text = "Buenos dias."; this.addChild(myText); When you add any visual element to the Stage, that element becomes a child of the Stage object. The first SWF file loaded in an application (for example, the one that you embed in an HTML page) is automatically added as a child of the Stage. It can be an object of any type that extends the Sprite class. Any display objects that you create without using ActionScript—for example, by adding an MXML tag in a Flex MXML file or by placing an item on the Stage in Flash Professional—are added to the display list. Although you do not add these display objects through ActionScript, you can access them through ActionScript. For example, the following code adjusts the width of an object named button1 that was added in the authoring tool (not through ActionScript): button1.width = 200; Working with display object containers Flash Player 9 and later, Adobe AIR 1.0 and later If a DisplayObjectContainer object is deleted from the display list, or if it is moved or transformed in some other way, each display object in the DisplayObjectContainer is also deleted, moved, or transformed. A display object container is itself a type of display object—it can be added to another display object container. For example, the following image shows a display object container, pictureScreen, that contains one outline shape and four other display object containers (of type PictureFrame): Last updated 3/21/2011 152 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming A B A. A shape defining the border of the pictureScreen display object container B. Four display object containers that are children of the pictureScreen object In order to have a display object appear in the display list, you must add it to a display object container that is on the display list. You do this by using the addChild() method or the addChildAt() method of the container object. For example, without the final line of the following code, the myTextField object would not be displayed: var myTextField:TextField = new TextField(); myTextField.text = "hello"; this.root.addChild(myTextField); In this code sample, this.root points to the MovieClip display object container that contains the code. In your actual code, you may specify a different container. Use the addChildAt() method to add the child to a specific position in the child list of the display object container. These zero-based index positions in the child list relate to the layering (the front-to-back order) of the display objects. For example, consider the following three display objects. Each object was created from a custom class called Ball. Last updated 3/21/2011 153 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The layering of these display objects in their container can be adjusted using the addChildAt() method. For example, consider the following code: ball_A = new Ball(0xFFCC00, "a"); ball_A.name = "ball_A"; ball_A.x = 20; ball_A.y = 20; container.addChild(ball_A); ball_B = new Ball(0xFFCC00, "b"); ball_B.name = "ball_B"; ball_B.x = 70; ball_B.y = 20; container.addChild(ball_B); ball_C = new Ball(0xFFCC00, "c"); ball_C.name = "ball_C"; ball_C.x = 40; ball_C.y = 60; container.addChildAt(ball_C, 1); After executing this code, the display objects are positioned as follows in the container DisplayObjectContainer object. Notice the layering of the objects. To reposition an object to the top of the display list, simply re-add it to the list. For example, after the previous code, to move ball_A to the top of the stack, use this line of code: container.addChild(ball_A); This code effectively removes ball_A from its location in container’s display list, and re-adds it to the top of the list— which has the end result of moving it to the top of the stack. You can use the getChildAt() method to verify the layer order of the display objects. The getChildAt() method returns child objects of a container based on the index number you pass it. For example, the following code reveals names of display objects at different positions in the child list of the container DisplayObjectContainer object: trace(container.getChildAt(0).name); // ball_A trace(container.getChildAt(1).name); // ball_C trace(container.getChildAt(2).name); // ball_B If you remove a display object from the parent container’s child list, the higher elements on the list each move down a position in the child index. For example, continuing with the previous code, the following code shows how the display object that was at position 2 in the container DisplayObjectContainer moves to position 1 if a display object that is lower in the child list is removed: Last updated 3/21/2011 154 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming container.removeChild(ball_C); trace(container.getChildAt(0).name); // ball_A trace(container.getChildAt(1).name); // ball_B The removeChild() and removeChildAt() methods do not delete a display object instance entirely. They simply remove it from the child list of the container. The instance can still be referenced by another variable. (Use the delete operator to completely remove an object.) Because a display object has only one parent container, you can add an instance of a display object to only one display object container. For example, the following code shows that the display object tf1 can exist in only one container (in this case, a Sprite, which extends the DisplayObjectContainer class): tf1:TextField = new TextField(); tf2:TextField = new TextField(); tf1.name = "text 1"; tf2.name = "text 2"; container1:Sprite = new Sprite(); container2:Sprite = new Sprite(); container1.addChild(tf1); container1.addChild(tf2); container2.addChild(tf1); trace(container1.numChildren); // 1 trace(container1.getChildAt(0).name); // text 2 trace(container2.numChildren); // 1 trace(container2.getChildAt(0).name); // text 1 If you add a display object that is contained in one display object container to another display object container, it is removed from the first display object container’s child list. In addition to the methods described above, the DisplayObjectContainer class defines several methods for working with child display objects, including the following: • contains(): Determines whether a display object is a child of a DisplayObjectContainer. • getChildByName(): Retrieves a display object by name. • getChildIndex(): Returns the index position of a display object. • setChildIndex(): Changes the position of a child display object. • swapChildren(): Swaps the front-to-back order of two display objects. • swapChildrenAt(): Swaps the front-to-back order of two display objects, specified by their index values. For more information, see the relevant entries in the ActionScript 3.0 Reference for the Adobe Flash Platform. Recall that a display object that is off the display list—one that is not included in a display object container that is a child of the Stage—is known as an off-list display object. Last updated 3/21/2011 155 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Traversing the display list Flash Player 9 and later, Adobe AIR 1.0 and later As you’ve seen, the display list is a tree structure. At the top of the tree is the Stage, which can contain multiple display objects. Those display objects that are themselves display object containers can contain other display objects, or display object containers. Stage Instance of the main class of the SWF file Display Object Container Display Object Display Object Container Display Object Display Object Container Display Object Display Object Container The DisplayObjectContainer class includes properties and methods for traversing the display list, by means of the child lists of display object containers. For example, consider the following code, which adds two display objects, title and pict, to the container object (which is a Sprite, and the Sprite class extends the DisplayObjectContainer class): var container:Sprite = new Sprite(); var title:TextField = new TextField(); title.text = "Hello"; var pict:Loader = new Loader(); var url:URLRequest = new URLRequest("banana.jpg"); pict.load(url); pict.name = "banana loader"; container.addChild(title); container.addChild(pict); The getChildAt() method returns the child of the display list at a specific index position: trace(container.getChildAt(0) is TextField); // true You can also access child objects by name. Each display object has a name property, and if you don’t assign it, Flash Player or AIR assigns a default value, such as "instance1". For example, the following code shows how to use the getChildByName() method to access a child display object with the name "banana loader": Last updated 3/21/2011 156 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming trace(container.getChildByName("banana loader") is Loader); // true Using the getChildByName() method can result in slower performance than using the getChildAt() method. Since a display object container can contain other display object containers as child objects in its display list, you can traverse the full display list of the application as a tree. For example, in the code excerpt shown earlier, once the load operation for the pict Loader object is complete, the pict object will have one child display object, which is the bitmap, loaded. To access this bitmap display object, you can write pict.getChildAt(0). You can also write container.getChildAt(0).getChildAt(0) (since container.getChildAt(0) == pict). The following function provides an indented trace() output of the display list from a display object container: function traceDisplayList(container:DisplayObjectContainer,indentString:String = ""):void { var child:DisplayObject; for (var i:uint=0; i < container.numChildren; i++) { child = container.getChildAt(i); trace(indentString, child, child.name); if (container.getChildAt(i) is DisplayObjectContainer) { traceDisplayList(DisplayObjectContainer(child), indentString + "") } } } Adobe Flex If you use Flex, you should know that Flex defines many component display object classes, and these classes override the display list access methods of the DisplayObjectContainer class. For example, the Container class of the mx.core package overrides the addChild() method and other methods of the DisplayObjectContainer class (which the Container class extends). In the case of the addChild() method, the class overrides the method in such a way that you cannot add all types of display objects to a Container instance in Flex. The overridden method, in this case, requires that the child object that you are adding be a type of mx.core.UIComponent object. Setting Stage properties Flash Player 9 and later, Adobe AIR 1.0 and later The Stage class overrides most properties and methods of the DisplayObject class. If you call one of these overridden properties or methods, Flash Player and AIR throw an exception. For example, the Stage object does not have x or y properties, since its position is fixed as the main container for the application. The x and y properties refer to the position of a display object relative to its container, and since the Stage is not contained in another display object container, these properties do not apply. Note: Some properties and methods of the Stage class are only available to display objects that are in the same security sandbox as the first SWF file loaded. For details, see “Stage security” on page 1029. Controlling the playback frame rate Flash Player 9 and later, Adobe AIR 1.0 and later The frameRate property of the Stage class is used to set the frame rate for all SWF files loaded into the application. For more information, see the ActionScript 3.0 Reference for the Adobe Flash Platform. Last updated 3/21/2011 157 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Controlling Stage scaling Flash Player 9 and later, Adobe AIR 1.0 and later When the portion of the screen representing Flash Player or AIR is resized, the runtime automatically adjusts the Stage contents to compensate. The Stage class’s scaleMode property determines how the Stage contents are adjusted. This property can be set to four different values, defined as constants in the flash.display.StageScaleMode class: • StageScaleMode.EXACT_FIT scales the SWF to fill the new stage dimensions without regard for the original content aspect ratio. The scale factors might not be the same for width and height, so the content can appear squeezed or stretched if the aspect ratio of the stage is changed. • StageScaleMode.SHOW_ALL scales the SWF to fit entirely within the new stage dimensions without changing the content aspect ratio. This scale mode displays all of the content, but can result in “letterbox” borders, like the black bars that appear when viewing a wide-screen movie on a standard television. • StageScaleMode.NO_BORDER scales the SWF to entirely fill the new stage dimensions without changing the aspect ratio of the content. This scale mode makes full use of the stage display area, but can result in cropping. • StageScaleMode.NO_SCALE — does not scale the SWF. If the new stage dimensions are smaller, the content is cropped; if larger, the added space is blank. In the StageScaleMode.NO_SCALE scale mode only, the stageWidth and stageHeight properties of the Stage class can be used to determine the actual pixel dimensions of the resized stage. (In the other scale modes, the stageWidth and stageHeight properties always reflect the original width and height of the SWF.) In addition, when scaleMode is set to StageScaleMode.NO_SCALE and the SWF file is resized, the Stage class’s resize event is dispatched, allowing you to make adjustments accordingly. Consequently, having scaleMode set to StageScaleMode.NO_SCALE allows you to have greater control over how the screen contents adjust to the window resizing if you desire. For example, in a SWF containing a video and a control bar, you might want to make the control bar stay the same size when the Stage is resized, and only change the size of the video window to accommodate the Stage size change. This is demonstrated in the following example: Last updated 3/21/2011 158 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // videoScreen is a display object (e.g. a Video instance) containing a // video; it is positioned at the top-left corner of the Stage, and // it should resize when the SWF resizes. // // // // controlBar is a display object (e.g. a Sprite) containing several buttons; it should stay positioned at the bottom-left corner of the Stage (below videoScreen) and it should not resize when the SWF resizes. import import import import flash.display.Stage; flash.display.StageAlign; flash.display.StageScaleMode; flash.events.Event; var swfStage:Stage = videoScreen.stage; swfStage.scaleMode = StageScaleMode.NO_SCALE; swfStage.align = StageAlign.TOP_LEFT; function resizeDisplay(event:Event):void { var swfWidth:int = swfStage.stageWidth; var swfHeight:int = swfStage.stageHeight; // Resize the video window. var newVideoHeight:Number = swfHeight - controlBar.height; videoScreen.height = newVideoHeight; videoScreen.scaleX = videoScreen.scaleY; // Reposition the control bar. controlBar.y = newVideoHeight; } swfStage.addEventListener(Event.RESIZE, resizeDisplay); Setting the stage scale mode for AIR windows The stage scaleMode property determines how the stage scales and clips child display objects when a window is resized. Only the noScale mode should be used in AIR. In this mode, the stage is not scaled. Instead, the size of the stage changes directly with the bounds of the window. Objects may be clipped if the window is resized smaller. The stage scale modes are designed for use in a environments such as a web browser where you don't always have control over the size or aspect ratio of the stage. The modes let you choose the least bad compromise when the stage does not match the ideal size or aspect ratio of your application. In AIR, you always have control of the stage, so in most cases re-laying out your content or adjusting the dimensions of your window will give you better results than enabling stage scaling. In the browser and for the initial AIR window, the relationship between the window size and the initial scale factor is read from the loaded SWF file. However, when you create a NativeWindow object, AIR chooses an arbitrary relationship between the window size and the scale factor of 72:1. Thus, if your window is 72x72 pixels, a 10x10 rectangle added to the window is drawn the correct size of 10x10 pixels. However, if the window is 144x144 pixels, then a 10x10 pixel rectangle is scaled to 20x20 pixels. If you insist on using a scaleMode other than noScale for a window stage, you can compensate by setting the scale factor of any display objects in the window to the ratio of 72 pixels to the current width and height of the stage. For example, the following code calculates the required scale factor for a display object named client: Last updated 3/21/2011 159 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming if(newWindow.stage.scaleMode != StageScaleMode.NO_SCALE){ client.scaleX = 72/newWindow.stage.stageWidth; client.scaleY = 72/newWindow.stage.stageHeight; } Note: Flex and HTML windows automatically set the stage scaleMode to noScale. Changing the scaleMode disturbs the automatic layout mechanisms used in these types of windows. Working with full-screen mode Flash Player 9 and later, Adobe AIR 1.0 and later Full-screen mode allows you to set a movie’s stage to fill a viewer’s entire monitor without any container borders or menus. The Stage class’s displayState property is used to toggle full-screen mode on and off for a SWF. The displayState property can be set to one of the values defined by the constants in the flash.display.StageDisplayState class. To turn on full-screen mode, set the displayState property to StageDisplayState.FULL_SCREEN: stage.displayState = StageDisplayState.FULL_SCREEN; In Flash Player, full-screen mode can only be initiated through ActionScript in response to a mouse click (including right-click) or keypress. AIR content running in the application security sandbox does not require that full-screen mode be entered in response to a user gesture. To exit full-screen mode, set the displayState property to StageDisplayState.NORMAL. stage.displayState = StageDisplayState.NORMAL; In addition, a user can choose to leave full-screen mode by switching focus to a different window or by using one of several key combinations: the Esc key (all platforms), Control-W (Windows), Command-W (Mac), or Alt-F4 (Windows). Enabling full-screen mode in Flash Player To enable full-screen mode for a SWF file embedded in an HTML page, the HTML code to embed Flash Player must include a param tag and embed attribute with the name allowFullScreen and value true, like this: ... In the Flash authoring tool, select File -> Publish Settings and in the Publish Settings dialog box, on the HTML tab, select the Flash Only - Allow Full Screen template. In Flex, ensure that the HTML template includes and tags that support full screen. If you are using JavaScript in a web page to generate the SWF-embedding tags, you must alter the JavaScript to add the allowFullScreen param tag and attribute. For example, if your HTML page uses the AC_FL_RunContent() function (which is used in HTML pages generated by Flash Professional and Flash Builder), you should add the allowFullScreen parameter to that function call as follows: AC_FL_RunContent( ... 'allowFullScreen','true', ... ); //end AC code Last updated 3/21/2011 160 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming This does not apply to SWF files running in the stand-alone Flash Player. Note: If you set the Window Mode (wmode in the HTML) to Opaque Windowless (opaque) or Transparent Windowless (transparent), the full-screen window is always opaque There are also security-related restrictions for using full-screen mode with Flash Player in a browser. These restrictions are described in “Security” on page 1008. Full screen stage size and scaling The Stage.fullScreenHeight and Stage.fullScreenWidth properties return the height and the width of the monitor that’s used when going to full-screen size, if that state is entered immediately. These values can be incorrect if the user has the opportunity to move the browser from one monitor to another after you retrieve these values but before entering full-screen mode. If you retrieve these values in the same event handler where you set the Stage.displayState property to StageDisplayState.FULL_SCREEN, the values are correct.For users with multiple monitors, the SWF content expands to fill only one monitor. Flash Player and AIR use a metric to determine which monitor contains the greatest portion of the SWF, and uses that monitor for full-screen mode. The fullScreenHeight and fullScreenWidth properties only reflect the size of the monitor that is used for full-screen mode. For more information, see Stage.fullScreenHeight and Stage.fullScreenWidth in the ActionScript 3.0 Reference for the Adobe Flash Platform. Stage scaling behavior for full-screen mode is the same as under normal mode; the scaling is controlled by the Stage class’s scaleMode property. If the scaleMode property is set to StageScaleMode.NO_SCALE, the Stage’s stageWidth and stageHeight properties change to reflect the size of the screen area occupied by the SWF (the entire screen, in this case); if viewed in the browser the HTML parameter for this controls the setting. You can use the Stage class’s fullScreen event to detect and respond when full-screen mode is turned on or off. For example, you might want to reposition, add, or remove items from the screen when entering or leaving full-screen mode, as in this example: import flash.events.FullScreenEvent; function fullScreenRedraw(event:FullScreenEvent):void { if (event.fullScreen) { // Remove input text fields. // Add a button that closes full-screen mode. } else { // Re-add input text fields. // Remove the button that closes full-screen mode. } } mySprite.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenRedraw); As this code shows, the event object for the fullScreen event is an instance of the flash.events.FullScreenEvent class, which includes a fullScreen property indicating whether full-screen mode is enabled (true) or not (false). Keyboard support in full-screen mode When Flash Player runs in a browser, all keyboard-related ActionScript, such as keyboard events and text entry in TextField instances, is disabled in full-screen mode. The exceptions (the keys that are enabled) are: • Selected non-printing keys, specifically the arrow keys, space bar, and tab key Last updated 3/21/2011 161 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming • Keyboard shortcuts that terminate full-screen mode: Esc (Windows and Mac), Control-W (Windows), CommandW (Mac), and Alt-F4 These restrictions are not present for SWF content running in the stand-alone Flash Player or in AIR. AIR supports an interactive full-screen mode that allows keyboard input. Hardware scaling in full-screen mode You can use the Stage class’s fullScreenSourceRect property to set Flash Player or AIR to scale a specific region of the stage to full-screen mode. Flash Player and AIR scale in hardware, if available, using the graphics and video card on a user's computer, and generally display content more quickly than software scaling. To take advantage of hardware scaling, you set the whole stage or part of the stage to full-screen mode. The following ActionScript 3.0 code sets the whole stage to full-screen mode: import flash.geom.*; { stage.fullScreenSourceRect = new Rectangle(0,0,320,240); stage.displayState = StageDisplayState.FULL_SCREEN; } When this property is set to a valid rectangle and the displayState property is set to full-screen mode, Flash Player and AIR scale the specified area. The actual Stage size in pixels within ActionScript does not change. Flash Player and AIR enforce a minimum limit for the size of the rectangle to accommodate the standard “Press Esc to exit full-screen mode” message. This limit is usually around 260 by 30 pixels but can vary depending on platform and Flash Player version. The fullScreenSourceRect property can only be set when Flash Player or AIR is not in full-screen mode. To use this property correctly, set this property first, then set the displayState property to full-screen mode. To enable scaling, set the fullScreenSourceRect property to a rectangle object. stage.fullScreenSourceRect = new Rectangle(0,0,320,240); To disable scaling, set the fullScreenSourceRect property to null. stage.fullScreenSourceRect = null; To take advantage of all hardware acceleration features with Flash Player, enable it through the Flash Player Settings dialog box. To load the dialog box, right-click (Windows) or Control-click (Mac) inside Flash Player content in your browser. Select the Display tab, which is the first tab, and click the checkbox: Enable hardware acceleration. Direct and GPU-compositing window modes Flash Player 10 introduces two window modes, direct and GPU compositing, which you can enable through the publish settings in the Flash authoring tool. These modes are not supported in AIR. To take advantage of these modes, you must enable hardware acceleration for Flash Player. Direct mode uses the fastest, most direct path to push graphics to the screen, which is advantageous for video playback. GPU Compositing uses the graphics processing unit on the video card to accelerate compositing. Video compositing is the process of layering multiple images to create a single video image. When compositing is accelerated with the GPU it can improve the performance of YUV conversion, color correction, rotation or scaling, and blending. YUV conversion refers to the color conversion of composite analog signals, which are used for transmission, to the RGB (red, green, blue) color model that video cameras and displays use. Using the GPU to accelerate compositing reduces the memory and computational demands that are otherwise placed on the CPU. It also results in smoother playback for standard-definition video. Last updated 3/21/2011 162 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Be cautious in implementing these window modes. Using GPU compositing can be expensive for memory and CPU resources. If some operations (such as blend modes, filtering, clipping or masking) cannot be carried out in the GPU, they are done by the software. Adobe recommends limiting yourself to one SWF file per HTML page when using these modes and you should not enable these modes for banners. The Flash Test Movie facility does not use hardware acceleration but you can use it through the Publish Preview option. Setting a frame rate in your SWF file that is higher than 60, the maximum screen refresh rate, is useless. Setting the frame rate from 50 through 55 allows for dropped frames, which can occur for various reasons from time to time. Using direct mode requires Microsoft DirectX 9 with VRAM 128 MB on Windows and OpenGL for Apple Macintosh, Mac OS X v10.2 or higher. GPU compositing requires Microsoft DirectX 9 and Pixel Shader 2.0 support on Windows with 128 MB of VRAM. On Mac OS X and Linux, GPU compositing requires OpenGL 1.5 and several OpenGL extensions (framebuffer object, multitexture, shader objects, shading language, fragment shader). You can activate direct and gpu acceleration modes on a per-SWF basis through the Flash Publish Settings dialog box, using the Hardware Acceleration menu on the Flash tab. If you choose None, the window mode reverts to default, transparent, or opaque, as specified by the Window Mode setting on the HTML tab. Handling events for display objects Flash Player 9 and later, Adobe AIR 1.0 and later The DisplayObject class inherits from the EventDispatcher class. This means that every display object can participate fully in the event model (described in “Handling events” on page 117). Every display object can use its addEventListener() method—inherited from the EventDispatcher class—to listen for a particular event, but only if the listening object is part of the event flow for that event. When Flash Player or AIR dispatches an event object, that event object makes a round-trip journey from the Stage to the display object where the event occurred. For example, if a user clicks on a display object named child1, Flash Player dispatches an event object from the Stage through the display list hierarchy down to the child1 display object. The event flow is conceptually divided into three phases, as illustrated in this diagram: Stage Capture Phase Bubbling Phase Parent Node Child1 Node Child2 Node Target Phase For more information, see “Handling events” on page 117. One important issue to keep in mind when working with display object events is the effect that event listeners can have on whether display objects are automatically removed from memory (garbage collected) when they’re removed from the display list. If a display object has objects subscribed as listeners to its events, that display object will not be removed from memory even when it’s removed from the display list, because it will still have references to those listener objects. For more information, see “Managing event listeners” on page 130. Last updated 3/21/2011 163 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Choosing a DisplayObject subclass Flash Player 9 and later, Adobe AIR 1.0 and later With several options to choose from, one of the important decisions you’ll make when you’re working with display objects is which display object to use for what purpose. Here are some guidelines to help you decide. These same suggestions apply whether you need an instance of a class or you’re choosing a base class for a class you’re creating: • If you don’t need an object that can be a container for other display objects (that is, you just need one that serves as a stand-alone screen element), choose one of these DisplayObject or InteractiveObject subclasses, depending on what it will be used for: • Bitmap for displaying a bitmap image. • TextField for adding text. • Video for displaying video. • Shape for a “canvas” for drawing content on-screen. In particular, if you want to create an instance for drawing shapes on the screen, and it won’t be a container for other display objects, you’ll gain significant performance benefits using Shape instead of Sprite or MovieClip. • MorphShape, StaticText, or SimpleButton for items created by the Flash authoring tool. (You can’t create instances of these classes programmatically, but you can create variables with these data types to refer to items created using the Flash authoring tool.) • If you need a variable to refer to the main Stage, use the Stage class as its data type. • If you need a container for loading an external SWF file or image file, use a Loader instance. The loaded content will be added to the display list as a child of the Loader instance. Its data type will depend on the nature of the loaded content, as follows: • A loaded image will be a Bitmap instance. • A loaded SWF file written in ActionScript 3.0 will be a Sprite or MovieClip instance (or an instance of a subclass of those classes, as specified by the content creator). • A loaded SWF file written in ActionScript 1.0 or ActionScript 2.0 will be an AVM1Movie instance. • If you need an object to serve as a container for other display objects (whether or not you’ll also be drawing onto the display object using ActionScript), choose one of the DisplayObjectContainer subclasses: • Sprite if the object will be created using only ActionScript, or as the base class for a custom display object that will be created and manipulated solely with ActionScript. • MovieClip if you’re creating a variable to refer to a movie clip symbol created in the Flash authoring tool. • If you are creating a class that will be associated with a movie clip symbol in the Flash library, choose one of these DisplayObjectContainer subclasses as your class’s base class: • MovieClip if the associated movie clip symbol has content on more than one frame • Sprite if the associated movie clip symbol has content only on the first frame Last updated 3/21/2011 164 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Manipulating display objects Flash Player 9 and later, Adobe AIR 1.0 and later Regardless of which display object you choose to use, there are a number of manipulations that all display objects have in common as elements that are displayed on the screen. For example, they can all be positioned on the screen, moved forward or backward in the stacking order of display objects, scaled, rotated, and so forth. Because all display objects inherit this functionality from their common base class (DisplayObject), this functionality behaves the same whether you’re manipulating a TextField instance, a Video instance, a Shape instance, or any other display object. The following sections detail several of these common display object manipulations. Changing position Flash Player 9 and later, Adobe AIR 1.0 and later The most basic manipulation to any display object is positioning it on the screen. To set a display object’s position, change the object’s x and y properties. myShape.x = 17; myShape.y = 212; The display object positioning system treats the Stage as a Cartesian coordinate system (the common grid system with a horizontal x axis and vertical y axis). The origin of the coordinate system (the 0,0 coordinate where the x and y axes meet) is at the top-left corner of the Stage. From there, x values are positive going right and negative going left, while (in contrast to typical graphing systems) y values are positive going down and negative going up. For example, the previous lines of code move the object myShape to the x coordinate 17 (17 pixels to the right of the origin) and y coordinate 212 (212 pixels below the origin). By default, when a display object is created using ActionScript, the x and y properties are both set to 0, placing the object at the top-left corner of its parent content. Changing position relative to the Stage It’s important to remember that the x and y properties always refer to the position of the display object relative to the 0,0 coordinate of its parent display object’s axes. So for a Shape instance (such as a circle) contained inside a Sprite instance, setting the Shape object’s x and y properties to 0 will place the circle at the top-left corner of the Sprite, which is not necessarily the top-left corner of the Stage. To position an object relative to the global Stage coordinates, you can use the globalToLocal() method of any display object to convert coordinates from global (Stage) coordinates to local (display object container) coordinates, like this: Last updated 3/21/2011 165 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // Position the shape at the top-left corner of the Stage, // regardless of where its parent is located. // Create a Sprite, positioned at x:200 and y:200. var mySprite:Sprite = new Sprite(); mySprite.x = 200; mySprite.y = 200; this.addChild(mySprite); // Draw a dot at the Sprite's 0,0 coordinate, for reference. mySprite.graphics.lineStyle(1, 0x000000); mySprite.graphics.beginFill(0x000000); mySprite.graphics.moveTo(0, 0); mySprite.graphics.lineTo(1, 0); mySprite.graphics.lineTo(1, 1); mySprite.graphics.lineTo(0, 1); mySprite.graphics.endFill(); // Create the circle Shape instance. var circle:Shape = new Shape(); mySprite.addChild(circle); // Draw a circle with radius 50 and center point at x:50, y:50 in the Shape. circle.graphics.lineStyle(1, 0x000000); circle.graphics.beginFill(0xff0000); circle.graphics.drawCircle(50, 50, 50); circle.graphics.endFill(); // Move the Shape so its top-left corner is at the Stage's 0, 0 coordinate. var stagePoint:Point = new Point(0, 0); var targetPoint:Point = mySprite.globalToLocal(stagePoint); circle.x = targetPoint.x; circle.y = targetPoint.y; You can likewise use the DisplayObject class’s localToGlobal() method to convert local coordinates to Stage coordinates. Moving display objects with the mouse You can let a user move display objects with mouse using two different techniques in ActionScript. In both cases, two mouse events are used: when the mouse button is pressed down, the object is told to follow the mouse cursor, and when it’s released, the object is told to stop following the mouse cursor. The first technique, using the startDrag() method, is simpler, but more limited. When the mouse button is pressed, the startDrag() method of the display object to be dragged is called. When the mouse button is released, the stopDrag() method is called. The Sprite class defines these two functions, so the object moved must be a Sprite or one of its subclasses. Last updated 3/21/2011 166 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // This code creates a mouse drag interaction using the startDrag() // technique. // square is a MovieClip or Sprite instance). import flash.events.MouseEvent; // This function is called when the mouse button is pressed. function startDragging(event:MouseEvent):void { square.startDrag(); } // This function is called when the mouse button is released. function stopDragging(event:MouseEvent):void { square.stopDrag(); } square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); square.addEventListener(MouseEvent.MOUSE_UP, stopDragging); This technique suffers from one fairly significant limitation: only one item at a time can be dragged using startDrag(). If one display object is being dragged and the startDrag() method is called on another display object, the first display object stops following the mouse immediately. For example, if the startDragging() function is changed as shown here, only the circle object will be dragged, in spite of the square.startDrag() method call: function startDragging(event:MouseEvent):void { square.startDrag(); circle.startDrag(); } As a consequence of the fact that only one object can be dragged at a time using startDrag(), the stopDrag() method can be called on any display object and it stops whatever object is currently being dragged. If you need to drag more than one display object, or to avoid the possibility of conflicts where more than one object might potentially use startDrag(), it’s best to use the mouse-following technique to create the dragging effect. With this technique, when the mouse button is pressed, a function is subscribed as a listener to the mouseMove event of the Stage. This function, which is then called every time the mouse moves, causes the dragged object to jump to the x, y coordinate of the mouse. Once the mouse button is released, the function is unsubscribed as a listener, meaning it is no longer called when the mouse moves and the object stops following the mouse cursor. Here is some code that demonstrates this technique: Last updated 3/21/2011 167 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // This code moves display objects using the mouse-following // technique. // circle is a DisplayObject (e.g. a MovieClip or Sprite instance). import flash.events.MouseEvent; var offsetX:Number; var offsetY:Number; // This function is called when the mouse button is pressed. function startDragging(event:MouseEvent):void { // Record the difference (offset) between where // the cursor was when the mouse button was pressed and the x, y // coordinate of the circle when the mouse button was pressed. offsetX = event.stageX - circle.x; offsetY = event.stageY - circle.y; // tell Flash Player to start listening for the mouseMove event stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCircle); } // This function is called when the mouse button is released. function stopDragging(event:MouseEvent):void { // Tell Flash Player to stop listening for the mouseMove event. stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCircle); } // This function is called every time the mouse moves, // as long as the mouse button is pressed down. function dragCircle(event:MouseEvent):void { // Move the circle to the location of the cursor, maintaining // the offset between the cursor's location and the // location of the dragged object. circle.x = event.stageX - offsetX; circle.y = event.stageY - offsetY; // Instruct Flash Player to refresh the screen after this event. event.updateAfterEvent(); } circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging); In addition to making a display object follow the mouse cursor, it is often desirable to move the dragged object to the front of the display, so that it appears to be floating above all the other objects. For example, suppose you have two objects, a circle and a square, that can both be moved with the mouse. If the circle happens to be below the square on the display list, and you click and drag the circle so that the cursor is over the square, the circle will appear to slide behind the square, which breaks the drag-and-drop illusion. Instead, you can make it so that when the circle is clicked, it moves to the top of the display list, and thus always appears on top of any other content. Last updated 3/21/2011 168 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The following code (adapted from the previous example) allows two display objects, a circle and a square, to be moved with the mouse. Whenever the mouse button is pressed over either one, that item is moved to the top of the Stage’s display list, so that the dragged item always appears on top. (Code that is new or changed from the previous listing appears in boldface.) // // // // This code creates a drag-and-drop interaction using the mouse-following technique. circle and square are DisplayObjects (e.g. MovieClip or Sprite instances). import flash.display.DisplayObject; import flash.events.MouseEvent; var offsetX:Number; var offsetY:Number; var draggedObject:DisplayObject; // This function is called when the mouse button is pressed. function startDragging(event:MouseEvent):void { // remember which object is being dragged draggedObject = DisplayObject(event.target); // Record the difference (offset) between where the cursor was when // the mouse button was pressed and the x, y coordinate of the // dragged object when the mouse button was pressed. offsetX = event.stageX - draggedObject.x; offsetY = event.stageY - draggedObject.y; // move the selected object to the top of the display list stage.addChild(draggedObject); // Tell Flash Player to start listening for the mouseMove event. stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); } // This function is called when the mouse button is released. function stopDragging(event:MouseEvent):void { // Tell Flash Player to stop listening for the mouseMove event. stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); } Last updated 3/21/2011 169 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // This function is called every time the mouse moves, // as long as the mouse button is pressed down. function dragObject(event:MouseEvent):void { // Move the dragged object to the location of the cursor, maintaining // the offset between the cursor's location and the location // of the dragged object. draggedObject.x = event.stageX - offsetX; draggedObject.y = event.stageY - offsetY; // Instruct Flash Player to refresh the screen after this event. event.updateAfterEvent(); } circle.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); circle.addEventListener(MouseEvent.MOUSE_UP, stopDragging); square.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); square.addEventListener(MouseEvent.MOUSE_UP, stopDragging); To extend this effect further, such as for a game where tokens or cards are moved among piles, you could add the dragged object to the Stage’s display list when it’s “picked up,” and then add it to another display list—such as the “pile” where it is dropped—when the mouse button is released. Finally, to enhance the effect, you could apply a drop shadow filter to the display object when it is clicked (when you start dragging it) and remove the drop shadow when the object is released. For details on using the drop shadow filter and other display object filters in ActionScript, see “Filtering display objects” on page 255. Panning and scrolling display objects Flash Player 9 and later, Adobe AIR 1.0 and later If you have a display object that is too large for the area in which you want it to display it, you can use the scrollRect property to define the viewable area of the display object. In addition, by changing the scrollRect property in response to user input, you can cause the content to pan left and right or scroll up and down. The scrollRect property is an instance of the Rectangle class, which is a class that combines the values needed to define a rectangular area as a single object. To initially define the viewable area of the display object, create a new Rectangle instance and assign it to the display object’s scrollRect property. Later, to scroll or pan, you read the scrollRect property into a separate Rectangle variable, and change the desired property (for instance, change the Rectangle instance’s x property to pan or y property to scroll). Then you reassign that Rectangle instance to the scrollRect property to notify the display object of the changed value. For example, the following code defines the viewable area for a TextField object named bigText that is too tall to fit in the SWF file’s boundaries. When the two buttons named up and down are clicked, they call functions that cause the contents of the TextField object to scroll up or down by modifying the y property of the scrollRect Rectangle instance. Last updated 3/21/2011 170 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming import flash.events.MouseEvent; import flash.geom.Rectangle; // Define the initial viewable area of the TextField instance: // left: 0, top: 0, width: TextField's width, height: 350 pixels. bigText.scrollRect = new Rectangle(0, 0, bigText.width, 350); // Cache the TextField as a bitmap to improve performance. bigText.cacheAsBitmap = true; // called when the "up" button is clicked function scrollUp(event:MouseEvent):void { // Get access to the current scroll rectangle. var rect:Rectangle = bigText.scrollRect; // Decrease the y value of the rectangle by 20, effectively // shifting the rectangle down by 20 pixels. rect.y -= 20; // Reassign the rectangle to the TextField to "apply" the change. bigText.scrollRect = rect; } // called when the "down" button is clicked function scrollDown(event:MouseEvent):void { // Get access to the current scroll rectangle. var rect:Rectangle = bigText.scrollRect; // Increase the y value of the rectangle by 20, effectively // shifting the rectangle up by 20 pixels. rect.y += 20; // Reassign the rectangle to the TextField to "apply" the change. bigText.scrollRect = rect; } up.addEventListener(MouseEvent.CLICK, scrollUp); down.addEventListener(MouseEvent.CLICK, scrollDown); As this example illustrates, when you work with the scrollRect property of a display object, it’s best to specify that Flash Player or AIR should cache the display object’s content as a bitmap, using the cacheAsBitmap property. When you do so, Flash Player and AIR don’t have to re-draw the entire contents of the display object each time it is scrolled, and can instead use the cached bitmap to render the necessary portion directly to the screen. For details, see “Caching display objects” on page 173. Manipulating size and scaling objects Flash Player 9 and later, Adobe AIR 1.0 and later You can measure and manipulate the size of a display object in two ways, using either the dimension properties (width and height) or the scale properties (scaleX and scaleY). Every display object has a width property and a height property, which are initially set to the size of the object in pixels. You can read the values of those properties to measure the size of the display object. You can also specify new values to change the size of the object, as follows: Last updated 3/21/2011 171 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // Resize a display object. square.width = 420; square.height = 420; // Determine the radius of a circle display object. var radius:Number = circle.width / 2; Changing the height or width of a display object causes the object to scale, meaning its contents stretch or squeeze to fit in the new area. If the display object contains only vector shapes, those shapes will be redrawn at the new scale, with no loss in quality. Any bitmap graphic elements in the display object will be scaled rather than redrawn. So, for example, a digital photo whose width and height are increased beyond the actual dimensions of the pixel information in the image will be pixelated, making it look jagged. When you change the width or height properties of a display object, Flash Player and AIR update the scaleX and scaleY properties of the object as well. Note: TextField objects are an exception to this scaling behavior. Text fields need to resize themselves to accommodate text wrapping and font sizes, so they reset their scaleX or scaleY values to 1 after resizing. However, if you adjust the scaleX or scaleY values of a TextField object, the width and height values change to accommodate the scaling values you provide. These properties represent the relative size of the display object compared to its original size. The scaleX and scaleY properties use fraction (decimal) values to represent percentage. For example, if a display object’s width has been changed so that it’s half as wide as its original size, the object’s scaleX property will have the value .5, meaning 50 percent. If its height has been doubled, its scaleY property will have the value 2, meaning 200 percent. // circle is a display object whose width and height are 150 pixels. // At original size, scaleX and scaleY are 1 (100%). trace(circle.scaleX); // output: 1 trace(circle.scaleY); // output: 1 // When you change the width and height properties, // Flash Player changes the scaleX and scaleY properties accordingly. circle.width = 100; circle.height = 75; trace(circle.scaleX); // output: 0.6622516556291391 trace(circle.scaleY); // output: 0.4966887417218543 Size changes are not proportional. In other words, if you change the height of a square but not its width, its proportions will no longer be the same, and it will be a rectangle instead of a square. If you want to make relative changes to the size of a display object, you can set the values of the scaleX and scaleY properties to resize the object, as an alternative to setting the width or height properties. For example, this code changes the width of the display object named square, and then alters the vertical scale (scaleY) to match the horizontal scale, so that the size of the square stays proportional. // Change the width directly. square.width = 150; // Change the vertical scale to match the horizontal scale, // to keep the size proportional. square.scaleY = square.scaleX; Last updated 3/21/2011 172 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Controlling distortion when scaling Flash Player 9 and later, Adobe AIR 1.0 and later Normally when a display object is scaled (for example, stretched horizontally), the resulting distortion is spread equally across the object, so that each part is stretched the same amount. For graphics and design elements, this is probably what you want. However, sometimes it’s preferable to have control over which portions of the display object stretch and which portions remain unchanged. One common example of this is a button that’s a rectangle with rounded corners. With normal scaling, the corners of the button will stretch, making the corner radius change as the button resizes. However, in this case it would be preferable to have control over the scaling—to be able to designate certain areas which should scale (the straight sides and middle) and areas which shouldn’t (the corners)—so that scaling happens without visible distortion. You can use 9-slice scaling (Scale-9) to create display objects where you have control over how the objects scale. With 9-slice scaling, the display object is divided into nine separate rectangles (a 3 by 3 grid, like the grid of a tic-tac-toe board). The rectangles aren’t necessarily the same size—you designate where the grid lines are placed. Any content that lies in the four corner rectangles (such as the rounded corners of a button) will not be stretched or compressed when the display object scales. The top-center and bottom-center rectangles will scale horizontally but not vertically, while the left-middle and right-middle rectangles will scale vertically but not horizontally. The center rectangle will scale both horizontally and vertically. Keeping this in mind, if you’re creating a display object and you want certain content to never scale, you just have to make sure that the dividing lines of the 9-slice scaling grid are placed so that the content ends up in one of the corner rectangles. In ActionScript, setting a value for the scale9Grid property of a display object turns on 9-slice scaling for the object and defines the size of the rectangles in the object’s Scale-9 grid. You use an instance of the Rectangle class as the value for the scale9Grid property, as follows: myButton.scale9Grid = new Rectangle(32, 27, 71, 64); Last updated 3/21/2011 173 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The four parameters of the Rectangle constructor are the x coordinate, y coordinate, width, and height. In this example, the rectangle’s top-left corner is placed at the point x: 32, y: 27 on the display object named myButton. The rectangle is 71 pixels wide and 64 pixels tall (so its right edge is at the x coordinate 103 on the display object and its bottom edge is at the y coordinate 92 on the display object). The actual area contained in the region defined by the Rectangle instance represents the center rectangle of the Scale9 grid. The other rectangles are calculated by Flash Player and AIR by extending the sides of the Rectangle instance, as shown here: In this case, as the button scales up or down, the rounded corners will not stretch or compress, but the other areas will adjust to accommodate the scaling. A B C A. myButton.width = 131;myButton.height = 106; B. myButton.width = 73;myButton.height = 69; C. myButton.width = 54;myButton.height = 141; Caching display objects Flash Player 9 and later, Adobe AIR 1.0 and later As your designs in Flash grow in size, whether you are creating an application or complex scripted animations, you need to consider performance and optimization. When you have content that remains static (such as a rectangle Shape instance), Flash Player and AIR do not optimize the content. Therefore, when you change the position of the rectangle, Flash Player or AIR redraws the entire Shape instance. You can cache specified display objects to improve the performance of your SWF file. The display object is a surface, essentially a bitmap version of the instance’s vector data, which is data that you do not intend to change much over the course of your SWF file. Therefore, instances with caching turned on are not continually redrawn as the SWF file plays, letting the SWF file render quickly. Note: You can update the vector data, at which time the surface is recreated. Therefore, the vector data cached in the surface does not need to remain the same for the entire SWF file. Setting a display object’s cacheAsBitmap property to true makes the display object cache a bitmap representation of itself. Flash Player or AIR creates a surface object for the instance, which is a cached bitmap instead of vector data. If you change the bounds of the display object, the surface is recreated instead of resized. Surfaces can nest within other surfaces. The child surface copies its bitmap onto its parent surface. For more information, see “Enabling bitmap caching” on page 175. Last updated 3/21/2011 174 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The DisplayObject class’s opaqueBackground property and scrollRect property are related to bitmap caching using the cacheAsBitmap property. Although these three properties are independent of each other, the opaqueBackground and scrollRect properties work best when an object is cached as a bitmap—you see performance benefits for the opaqueBackground and scrollRect properties only when you set cacheAsBitmap to true. For more information about scrolling display object content, see “Panning and scrolling display objects” on page 169. For more information about setting an opaque background, see “Setting an opaque background color” on page 176. For information on alpha channel masking, which requires you to set the cacheAsBitmap property to true, see “Masking display objects” on page 181. When to enable caching Flash Player 9 and later, Adobe AIR 1.0 and later Enabling caching for a display object creates a surface, which has several advantages, such as helping complex vector animations to render fast. There are several scenarios in which you will want to enable caching. It might seem as though you would always want to enable caching to improve the performance of your SWF files; however, there are situations in which enabling caching does not improve performance, or can even decrease it. This section describes scenarios in which caching should be used, and when to use regular display objects. Overall performance of cached data depends on how complex the vector data of your instances are, how much of the data you change, and whether or not you set the opaqueBackground property. If you are changing small regions, the difference between using a surface and using vector data could be negligible. You might want to test both scenarios with your work before you deploy the application. When to use bitmap caching The following are typical scenarios in which you might see significant benefits when you enable bitmap caching. • Complex background image: An application that contains a detailed and complex background image of vector data (perhaps an image where you applied the trace bitmap command, or artwork that you created in Adobe Illustrator®). You might animate characters over the background, which slows the animation because the background needs to continuously regenerate the vector data. To improve performance, you can set the opaqueBackground property of the background display object to true. The background is rendered as a bitmap and can be redrawn quickly, so that your animation plays much faster. • Scrolling text field: An application that displays a large amount of text in a scrolling text field. You can place the text field in a display object that you set as scrollable with scrolling bounds (the scrollRect property). This enables fast pixel scrolling for the specified instance. When a user scrolls the display object instance, Flash Player or AIR shifts the scrolled pixels up and generates the newly exposed region instead of regenerating the entire text field. • Windowing system: An application with a complex system of overlapping windows. Each window can be open or closed (for example, web browser windows). If you mark each window as a surface (by setting the cacheAsBitmap property to true), each window is isolated and cached. Users can drag the windows so that they overlap each other, and each window doesn’t need to regenerate the vector content. • Alpha channel masking: When you are using alpha channel masking, you must set the cacheAsBitmap property to true. For more information, see “Masking display objects” on page 181. Enabling bitmap caching in all of these scenarios improves the responsiveness and interactivity of the application by optimizing the vector graphics. In addition, whenever you apply a filter to a display object, cacheAsBitmap is automatically set to true, even if you explicitly set it to false. If you clear all the filters from the display object, the cacheAsBitmap property returns to the value it was last set to. Last updated 3/21/2011 175 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming When to avoid using bitmap caching Using this feature in the wrong circumstances can negatively affect the performance of your SWF file. When you use bitmap caching, remember the following guidelines: • Do not overuse surfaces (display objects with caching enabled). Each surface uses more memory than a regular display object, which means that you should only enable surfaces when you need to improve rendering performance. A cached bitmap can use significantly more memory than a regular display object. For example, if a Sprite instance on the Stage is 250 pixels by 250 pixels in size, when cached it might use 250 KB instead of 1 KB when it’s a regular (un-cached) Sprite instance. • Avoid zooming into cached surfaces. If you overuse bitmap caching, a large amount of memory is consumed (see previous bullet), especially if you zoom in on the content. • Use surfaces for display object instances that are largely static (non-animating). You can drag or move the instance, but the contents of the instance should not animate or change a lot. (Animation or changing content are more likely with a MovieClip instance containing animation or a Video instance.) For example, if you rotate or transform an instance, the instance changes between the surface and vector data, which is difficult to process and negatively affects your SWF file. • If you mix surfaces with vector data, it increases the amount of processing that Flash Player and AIR (and sometimes the computer) need to do. Group surfaces together as much as possible—for example, when you create windowing applications. • Do not cache objects whose graphics change frequently. Every time you scale, skew, rotate the display object, change the alpha or color transform, move child display objects, or draw using the graphics property, the bitmap cache is redrawn. If this happens every frame, the runtime must draw the object into a bitmap and then copy that bitmap onto the stage—which results in extra work compared to just drawing the uncached object to the stage. The performance tradeoff of caching versus update frequency depends on the complexity and size of the display object and can only be determined by testing the specific content. Enabling bitmap caching Flash Player 9 and later, Adobe AIR 1.0 and later To enable bitmap caching for a display object, you set its cacheAsBitmap property to true: mySprite.cacheAsBitmap = true; After you set the cacheAsBitmap property to true, you might notice that the display object automatically pixel-snaps to whole coordinates. When you test the SWF file, you should notice that any animation performed on a complex vector image renders much faster. A surface (cached bitmap) is not created, even if cacheAsBitmap is set to true, if one or more of the following occurs: • The bitmap is greater than 2880 pixels in height or width. • The bitmap fails to allocate (because of an out-of-memory error). Cached bitmap transform matrices Adobe AIR 2.0 and later (mobile profile) In AIR applications for mobile devices, you should set the cacheAsBitmapMatrix property whenever you set the cacheAsBitmap property. Setting this property allows you to apply a wider range of transformations to the display object without triggering rerendering. Last updated 3/21/2011 176 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming mySprite.cacheAsBitmap = true; mySprite.cacheAsBitmapMatrix = new Matrix(); When you set this matrix property, you can apply the following additional transformation to the display object without recaching the object: • Move or translate without pixel-snapping • Rotate • Scale • Skew • Change alpha (between 0 and 100% transparency) These transformations are applied directly to the cached bitmap. Setting an opaque background color Flash Player 9 and later, Adobe AIR 1.0 and later You can set an opaque background for a display object. For example, when your SWF has a background that contains complex vector art, you can set the opaqueBackground property to a specified color (typically the same color as the Stage). The color is specified as a number (commonly a hexadecimal color value). The background is then treated as a bitmap, which helps optimize performance. When you set cacheAsBitmap to true, and also set the opaqueBackground property to a specified color, the opaqueBackground property allows the internal bitmap to be opaque and rendered faster. If you do not set cacheAsBitmap to true, the opaqueBackground property adds an opaque vector-square shape to the background of the display object. It does not create a bitmap automatically. The following example shows how to set the background of a display object to optimize performance: myShape.cacheAsBitmap = true; myShape.opaqueBackground = 0xFF0000; In this case, the background color of the Shape named myShape is set to red (0xFF0000). Assuming the Shape instance contains a drawing of a green triangle, on a Stage with a white background, this would show up as a green triangle with red in the empty space in the Shape instance’s bounding box (the rectangle that completely encloses the Shape). Of course, this code would make more sense if it were used with a Stage with a solid red background. On another colored background, that color would be specified instead. For example, in a SWF with a white background, the opaqueBackground property would most likely be set to 0xFFFFFF, or pure white. Last updated 3/21/2011 177 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Applying blending modes Flash Player 9 and later, Adobe AIR 1.0 and later Blending modes involve combining the colors of one image (the base image) with the colors of another image (the blend image) to produce a third image—the resulting image is the one that is actually displayed on the screen. Each pixel value in an image is processed with the corresponding pixel value of the other image to produce a pixel value for that same position in the result. Every display object has a blendMode property that can be set to one of the following blending modes. These are constants defined in the BlendMode class. Alternatively, you can use the String values (in parentheses) that are the actual values of the constants. • BlendMode.ADD ("add"): Commonly used to create an animated lightening dissolve effect between two images. • BlendMode.ALPHA ("alpha"): Commonly used to apply the transparency of the foreground on the background. (Not supported under GPU rendering.) • BlendMode.DARKEN ("darken"): Commonly used to superimpose type. (Not supported under GPU rendering.) • BlendMode.DIFFERENCE ("difference"): Commonly used to create more vibrant colors. • BlendMode.ERASE ("erase"): Commonly used to cut out (erase) part of the background using the foreground alpha. (Not supported under GPU rendering.) • BlendMode.HARDLIGHT ("hardlight"): Commonly used to create shading effects. (Not supported under GPU rendering.) • BlendMode.INVERT ("invert"): Used to invert the background. • BlendMode.LAYER ("layer"): Used to force the creation of a temporary buffer for precomposition for a particular display object. (Not supported under GPU rendering.) • BlendMode.LIGHTEN ("lighten"): Commonly used to superimpose type. (Not supported under GPU rendering.) • BlendMode.MULTIPLY ("multiply"): Commonly used to create shadows and depth effects. • BlendMode.NORMAL ("normal"): Used to specify that the pixel values of the blend image override those of the base image. • BlendMode.OVERLAY ("overlay"): Commonly used to create shading effects. (Not supported under GPU rendering.) • BlendMode.SCREEN ("screen"): Commonly used to create highlights and lens flares. • BlendMode.SHADER ("shader"): Used to specify that a Pixel Bender shader is used to create a custom blending effect. For more information about using shaders, see “Working with Pixel Bender shaders” on page 288. (Not supported under GPU rendering.) • BlendMode.SUBTRACT ("subtract"): Commonly used to create an animated darkening dissolve effect between two images. Last updated 3/21/2011 178 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Adjusting DisplayObject colors Flash Player 9 and later, Adobe AIR 1.0 and later You can use the methods of the ColorTransform class (flash.geom.ColorTransform) to adjust the color of a display object. Each display object has a transform property, which is an instance of the Transform class, and contains information about various transformations that are applied to the display object (such as rotation, changes in scale or position, and so forth). In addition to its information about geometric transformations, the Transform class also includes a colorTransform property, which is an instance of the ColorTransform class, and provides access to make color adjustments to the display object. To access the color transformation information of a display object, you can use code such as this: var colorInfo:ColorTransform = myDisplayObject.transform.colorTransform; Once you’ve created a ColorTransform instance, you can read its property values to find out what color transformations have already been applied, or you can set those values to make color changes to the display object. To update the display object after any changes, you must reassign the ColorTransform instance back to the transform.colorTransform property. var colorInfo:ColorTransform = myDisplayObject.transform.colorTransform; // Make some color transformations here. // Commit the change. myDisplayObject.transform.colorTransform = colorInfo; Setting color values with code Flash Player 9 and later, Adobe AIR 1.0 and later The color property of the ColorTransform class can be used to assign a specific red, green, blue (RGB) color value to the display object. The following example uses the color property to change the color of the display object named square to blue, when the user clicks a button named blueBtn: // square is a display object on the Stage. // blueBtn, redBtn, greenBtn, and blackBtn are buttons on the Stage. import flash.events.MouseEvent; import flash.geom.ColorTransform; // Get access to the ColorTransform instance associated with square. var colorInfo:ColorTransform = square.transform.colorTransform; // This function is called when blueBtn is clicked. function makeBlue(event:MouseEvent):void { // Set the color of the ColorTransform object. colorInfo.color = 0x003399; // apply the change to the display object square.transform.colorTransform = colorInfo; } blueBtn.addEventListener(MouseEvent.CLICK, makeBlue); Last updated 3/21/2011 179 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Note that when you change a display object’s color using the color property, it completely changes the color of the entire object, regardless of whether the object previously had multiple colors. For example, if there is a display object containing a green circle with black text on top, setting the color property of that object’s associated ColorTransform instance to a shade of red will make the entire object, circle and text, turn red (so the text will no longer be distinguishable from the rest of the object). Altering color and brightness effects with code Flash Player 9 and later, Adobe AIR 1.0 and later Suppose you have a display object with multiple colors (for example, a digital photo) and you don’t want to completely recolor the object; you just want to adjust the color of a display object based on the existing colors. In this scenario, the ColorTransform class includes a series of multiplier and offset properties that you can use to make this type of adjustment. The multiplier properties, named redMultiplier, greenMultiplier, blueMultiplier, and alphaMultiplier, work like colored photographic filters (or colored sunglasses), amplifying or diminishing certain colors in the display object. The offset properties (redOffset, greenOffset, blueOffset, and alphaOffset) can be used to add extra amounts of a certain color to the object, or to specify the minimum value that a particular color can have. These multiplier and offset properties are identical to the advanced color settings that are available for movie clip symbols in the Flash authoring tool when you choose Advanced from the Color pop-up menu on the Property inspector. The following code loads a JPEG image and applies a color transformation to it, which adjusts the red and green channels as the mouse pointer moves along the x axis and y axis. In this case, because no offset values are specified, the color value of each color channel displayed on screen will be a percentage of the original color value in the image— meaning that the most red or green displayed in any given pixel will be the original amount of red or green in that pixel. Last updated 3/21/2011 180 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming import import import import import flash.display.Loader; flash.events.MouseEvent; flash.geom.Transform; flash.geom.ColorTransform; flash.net.URLRequest; // Load an image onto the Stage. var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg"); loader.load(url); this.addChild(loader); // This function is called when the mouse moves over the loaded image. function adjustColor(event:MouseEvent):void { // Access the ColorTransform object for the Loader (containing the image) var colorTransformer:ColorTransform = loader.transform.colorTransform; // Set the red and green multipliers according to the mouse position. // The red value ranges from 0% (no red) when the cursor is at the left // to 100% red (normal image appearance) when the cursor is at the right. // The same applies to the green channel, except it's controlled by the // position of the mouse in the y axis. colorTransformer.redMultiplier = (loader.mouseX / loader.width) * 1; colorTransformer.greenMultiplier = (loader.mouseY / loader.height) * 1; // Apply the changes to the display object. loader.transform.colorTransform = colorTransformer; } loader.addEventListener(MouseEvent.MOUSE_MOVE, adjustColor); Rotating objects Flash Player 9 and later, Adobe AIR 1.0 and later Display objects can be rotated using the rotation property. You can read this value to find out whether an object has been rotated, or to rotate the object you can set this property to a number (in degrees) representing the amount of rotation to be applied to the object. For instance, this line of code rotates the object named square 45 degrees (one eighth of one complete revolution): square.rotation = 45; Alternatively, you can rotate a display object using a transformation matrix, described in “Working with geometry” on page 199. Fading objects Flash Player 9 and later, Adobe AIR 1.0 and later You can control the transparency of a display object to make it partially (or completely transparent), or change the transparency to make the object appear to fade in or out. The DisplayObject class’s alpha property defines the transparency (or more accurately, the opacity) of a display object. The alpha property can be set to any value between 0 and 1, where 0 is completely transparent, and 1 is completely opaque. For example, these lines of code make the object named myBall partially (50 percent) transparent when it is clicked with the mouse: Last updated 3/21/2011 181 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming function fadeBall(event:MouseEvent):void { myBall.alpha = .5; } myBall.addEventListener(MouseEvent.CLICK, fadeBall); You can also alter the transparency of a display object using the color adjustments available through the ColorTransform class. For more information, see “Adjusting DisplayObject colors” on page 178. Masking display objects Flash Player 9 and later, Adobe AIR 1.0 and later You can use a display object as a mask to create a hole through which the contents of another display object are visible. Defining a mask To indicate that a display object will be the mask for another display object, set the mask object as the mask property of the display object to be masked: // Make the object maskSprite be a mask for the object mySprite. mySprite.mask = maskSprite; The masked display object is revealed under all opaque (nontransparent) areas of the display object acting as the mask. For instance, the following code creates a Shape instance containing a red 100 by 100 pixel square and a Sprite instance containing a blue circle with a radius of 25 pixels. When the circle is clicked, it is set as the mask for the square, so that the only part of the square that shows is the part that is covered by the solid part of the circle. In other words, only a red circle will be visible. // This code assumes it's being run within a display object container // such as a MovieClip or Sprite instance. import flash.display.Shape; // Draw a square and add it to the display list. var square:Shape = new Shape(); square.graphics.lineStyle(1, 0x000000); square.graphics.beginFill(0xff0000); square.graphics.drawRect(0, 0, 100, 100); square.graphics.endFill(); this.addChild(square); // Draw a circle and add it to the display list. var circle:Sprite = new Sprite(); circle.graphics.lineStyle(1, 0x000000); circle.graphics.beginFill(0x0000ff); circle.graphics.drawCircle(25, 25, 25); circle.graphics.endFill(); this.addChild(circle); function maskSquare(event:MouseEvent):void { square.mask = circle; circle.removeEventListener(MouseEvent.CLICK, maskSquare); } circle.addEventListener(MouseEvent.CLICK, maskSquare); Last updated 3/21/2011 182 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The display object that is acting as a mask can be draggable, animated, resized dynamically, and can use separate shapes within a single mask. The mask display object doesn’t necessarily need to be added to the display list. However, if you want the mask object to scale when the Stage is scaled or if you want to enable user interaction with the mask (such as user-controlled dragging and resizing), the mask object must be added to the display list. The actual z-index (front-toback order) of the display objects doesn’t matter, as long as the mask object is added to the display list. (The mask object will not appear on the screen except as a mask.) If the mask object is a MovieClip instance with multiple frames, it plays all the frames in its timeline, the same as it would if it were not serving as a mask. You can remove a mask by setting the mask property to null: // remove the mask from mySprite mySprite.mask = null; You cannot use a mask to mask another mask. You cannot set the alpha property of a mask display object. Only fills are used in a display object that is used as a mask; strokes are ignored. AIR 2 If a masked display object is cached by setting the cacheAsBitmap and cacheAsBitmapMatrix properties, the mask must be a child of the masked display object. Similarly, if the masked display object is a descendent of a display object container that is cached, both the mask and the display object must be descendents of that container. If the masked object is a descendent of more than one cached display object container, the mask must be a descendent of the cached container closest to the masked object in the display list. About masking device fonts You can use a display object to mask text that is set in a device font. When you use a display object to mask text set in a device font, the rectangular bounding box of the mask is used as the masking shape. That is, if you create a nonrectangular display object mask for device font text, the mask that appears in the SWF file is the shape of the rectangular bounding box of the mask, not the shape of the mask itself. Alpha channel masking Alpha channel masking is supported if both the mask and the masked display objects use bitmap caching, as shown here: // maskShape is a Shape instance which includes a gradient fill. mySprite.cacheAsBitmap = true; maskShape.cacheAsBitmap = true; mySprite.mask = maskShape; For instance, one application of alpha channel masking is to use a filter on the mask object independently of a filter that is applied to the masked display object. In the following example, an external image file is loaded onto the Stage. That image (or more accurately, the Loader instance it is loaded into) will be the display object that is masked. A gradient oval (solid black center fading to transparent at the edges) is drawn over the image; this will be the alpha mask. Both display objects have bitmap caching turned on. The oval is set as a mask for the image, and it is then made draggable. Last updated 3/21/2011 183 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming // This code assumes it's being run within a display object container // such as a MovieClip or Sprite instance. import import import import import flash.display.GradientType; flash.display.Loader; flash.display.Sprite; flash.geom.Matrix; flash.net.URLRequest; // Load an image and add it to the display list. var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg"); loader.load(url); this.addChild(loader); // Create a Sprite. var oval:Sprite = new Sprite(); // Draw a gradient oval. var colors:Array = [0x000000, 0x000000]; var alphas:Array = [1, 0]; var ratios:Array = [0, 255]; var matrix:Matrix = new Matrix(); matrix.createGradientBox(200, 100, 0, -100, -50); oval.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix); oval.graphics.drawEllipse(-100, -50, 200, 100); oval.graphics.endFill(); // add the Sprite to the display list this.addChild(oval); // Set cacheAsBitmap = true for both display objects. loader.cacheAsBitmap = true; oval.cacheAsBitmap = true; // Set the oval as the mask for the loader (and its child, the loaded image) loader.mask = oval; // Make the oval draggable. oval.startDrag(true); Animating objects Flash Player 9 and later, Adobe AIR 1.0 and later Animation is the process of making something move, or alternatively, of making something change over time. Scripted animation is a fundamental part of video games, and is often used to add polish and useful interaction clues to other applications. Last updated 3/21/2011 184 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The fundamental idea behind scripted animation is that a change needs to take place, and that change needs to be divided into increments over time. It’s easy to make something repeat in ActionScript, using a common looping statement. However, a loop will run through all its iterations before updating the display. To create scripted animation, you need to write ActionScript that performs some action repeatedly over time and also updates the screen each time it runs. For example, imagine you want to create a simple animation, such as making a ball travel across the screen. ActionScript includes a simple mechanism that allows you to track the passage of time and update the screen accordingly—meaning you could write code that moves the ball a small amount each time, until it reaches its destination. After each move the screen would update, making the cross-Stage motion visible to the viewer. From a practical standpoint, it makes sense to synchronize scripted animation with the SWF file’s frame rate (in other words, make one animation change each time a new frame displays or would display), since that defines how frequently Flash Player or AIR updates the screen. Each display object has an enterFrame event that is dispatched according to the frame rate of the SWF file—one event per frame. Most developers who create scripted animation use the enterFrame event as a way to create actions that repeat over time. You could write code that listens to the enterFrame event, moving the animated ball a certain amount each frame, and as the screen is updated (each frame), the ball would be redrawn in its new location, creating motion. Note: Another way to perform an action repeatedly over time is to use the Timer class. A Timer instance triggers an event notification each time a specified amount of time has past. You could write code that performs animation by handling the Timer class’s timer event, setting the time interval to a small one (some fraction of a second). For more information about using the Timer class, see “Controlling time intervals” on page 4. In the following example, a circle Sprite instance, named circle, is created on the Stage. When the user clicks the circle, a scripted animation sequence begins, causing circle to fade (its alpha property is decreased) until it is completely transparent: Last updated 3/21/2011 185 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; // draw a circle and add it to the display list var circle:Sprite = new Sprite(); circle.graphics.beginFill(0x990000); circle.graphics.drawCircle(50, 50, 50); circle.graphics.endFill(); addChild(circle); // When this animation starts, this function is called every frame. // The change made by this function (updated to the screen every // frame) is what causes the animation to occur. function fadeCircle(event:Event):void { circle.alpha -= .05; if (circle.alpha <= 0) { circle.removeEventListener(Event.ENTER_FRAME, fadeCircle); } } function startAnimation(event:MouseEvent):void { circle.addEventListener(Event.ENTER_FRAME, fadeCircle); } circle.addEventListener(MouseEvent.CLICK, startAnimation); When the user clicks the circle, the function fadeCircle() is subscribed as a listener of the enterFrame event, meaning it begins to be called once per frame. That function fades circle by changing its alpha property, so once per frame the circle’s alpha decreases by .05 (5 percent) and the screen is updated. Eventually, when the alpha value is 0 (circle is completely transparent), the fadeCircle() function is removed as an event listener, ending the animation. The same code could be used, for example, to create animated motion instead of fading. By substituting a different property for alpha in the function that is an enterFrame event listener, that property will be animated instead. For example, changing this line circle.alpha -= .05; to this code circle.x += 5; will animate the x property, causing the circle to move to the right across the Stage. The condition that ends the animation could be changed to end the animation (that is, unsubscribe the enterFrame listener) when the desired x coordinate is reached. Last updated 3/21/2011 186 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Stage orientation AIR 2.0 and later Mobile devices typically re-orient the user interface to keep the display upright when the user rotates the device. If you enable auto-orientation in your application, the device keeps the display properly oriented, but it is up to you to make sure that your content looks okay when the aspect ratio of the stage changes. If you disable auto-orientation, then the device display remains fixed unless you change the orientation manually. AIR applications run on a number of different mobile devices and operating systems. The underlying orientation behavior can vary across operating systems, and even across different devices on the same operating system. A simple design strategy, that works well across all devices and operating systems, is to enable auto-orientation and to listen for Stage resize events to determine when you need to refresh the application layout. Alternately, if your application only supports the portrait aspect ratio or only supports the landscape aspect ratio, you can turn off auto-orientation and set the supported aspect ratio in the AIR application descriptor. This design strategy provides consistent behavior and selects the “best” orientation for the selected aspect ratio. For example, if you specify the landscape aspect ratio, the orientation chosen is appropriate for devices with landscape-mode, slide-out keyboards. Getting the current Stage orientation and aspect ratio Orientation is reported relative to the normal position of the device. On most devices there is a clear, upright position. This position is considered the default orientation. The other three possible orientations are then: rotated left, rotated right, and upside down. The StageOrientation class defines string constants to use when setting or comparing orientation values. The Stage class defines two properties that report orientation: • Stage.deviceOrientation — Reports the physical orientation of the device relative to the default position. Note: The deviceOrientation is not always available when your application first starts up or when the device is lying flat. In these cases, the device orientation is reported as unknown. • Stage.orientation — Reports the orientation of the Stage relative to the default position. When auto-orientation is enabled, the stage rotates in the opposite direction as the device to remain upright. Thus, the right and left positions reported by the orientation property are the opposite of those reported by the deviceOrientation property. For example, when deviceRotation reports rotated right, orientation reports rotated left. The aspect ratio of the stage can be derived by simply comparing the current width and height of the stage: var aspect:String = this.stage.stageWidth >= this.stage.stageHeight ? StageAspectRatio.LANDSCAPE : StageAspectRatio.PORTRAIT; Automatic orientation When auto-orientation is on and a user rotates their device, the operating system re-orients the entire user interface, including the system taskbar and your application. As a result, the aspect ratio of the stage changes from portrait to landscape or landscape to portrait. When the aspect ratio changes, the stage dimensions also change. Enable or disable auto-orientation at runtime, by setting the Stage autoOrients property to true or false. You can set the initial value of this property in the AIR application descriptor with the element. (Note that prior to AIR 2.6, autoOrients is a read-only property and can only be set in the application descriptor.) Last updated 3/21/2011 187 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Stage dimension changes When the stage dimensions change, the stage contents are scaled and repositioned as specified by the scaleMode and align properties of the Stage object. In most cases, relying on the automatic behavior provided by the Stage scaleMode settings does not produce good results. Instead you must re-layout or redraw your graphics and components to support more than one aspect ratio. (Providing flexible layout logic also means that your application will work better across devices with different screen sizes and aspect ratios.) The following illustration demonstrates the effects of the different scaleMode settings when rotating a typical mobile device: Rotation from landscape to portrait aspect ratio The illustration demonstrates the scaling behavior that occurs when rotating from a landscape aspect ratio to a portrait aspect ratio with different scale modes. Rotating from portrait to landscape causes a similar set of effects. Orientation change events The Stage object dispatches two types of events that you can use to detect and react to orientation changes. Both stage resize and orientationChange events are dispatched when auto-orientation is enabled. The resize event is the best event to use when you are relying on auto-orientation to keep the display upright. When the stage dispatches a resize event, relayout or redraw your content, as needed. The resize event is only dispatched when the stage scale mode is set to noScale. The orientationChange event can also be used to detect orientation changes. The orientationChange event is only dispatched when auto-orientation is enabled. Note: On some mobile platforms, the stage dispatches a cancelable orientationChanging event before dispatching the resize or orientationChange events. Since the event is not supported on all platforms, avoid relying on it. Last updated 3/21/2011 188 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming Manual orientation AIR 2.6 and later You can control the stage orientation using the Stage setOrientation() or setAspectRatio() methods. Setting the stage orientation You can set the stage orientation at runtime using the setOrientation() method of the Stage object. Use the string constants defined by the StageOrientation class to specify the desired orientation: this.stage.setOrientation( StageOrientation.ROTATED_RIGHT ); Not every device and operating system supports every possible orientation. For example, Android 2.2 does not support programmatically choosing the rotated-left orientation on portrait-standard devices and does not support the upsidedown orientation at all. The supportedOrientations property of the stage provides a list of the orientations that can be passed to the setOrientation() method: var orientations:Vector. = this.stage.supportedOrientations; for each( var orientation:String in orientations ) { trace( orientation ); } Setting the stage aspect ratio If you are primarily concerned about the aspect ratio of the stage, you can set the aspect ratio to portrait or landscape. You can set the aspect ratio in either the AIR application descriptor or, at run time, using the Stage setAspectRatio() method: this.stage.setAspectRatio( StageAspectRatio.LANDSCAPE ); The runtime chooses one of the two possible orientations for the specified aspect ratio. This may not match the current device orientation. The default orientation is chosen in preference to the upside-down orientation. The orientation appropriate for the slide-out keyboard is chosen in preference to the opposite orientation. Example: Setting the stage orientation to match the device orientation The following example illustrates a function that updates the stage orientation to match the current device orientation. The stage deviceOrientation property indicates the physical orientation of the device, even when auto-orientation is turned off. Last updated 3/21/2011 189 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming function refreshOrientation( theStage:Stage ):void { switch ( theStage.deviceOrientation ) { case StageOrientation.DEFAULT: theStage.setOrientation( StageOrientation.DEFAULT ); break; case StageOrientation.ROTATED_RIGHT: theStage.setOrientation( StageOrientation.ROTATED_LEFT ); break; case StageOrientation.ROTATED_LEFT: theStage.setOrientation( StageOrientation.ROTATED_RIGHT ); break; case StageOrientation.UPSIDE_DOWN: theStage.setOrientation( StageOrientation.UPSIDE_DOWN ); break; default: //No change } } The orientation change is asynchronous. You can listen for the orientationChange event dispatched by the stage to detect the completion of the change. If an orientation is not supported on a device, the setOrientation() call fails without throwing an error. Loading display content dynamically Flash Player 9 and later, Adobe AIR 1.0 and later You can load any of the following external display assets into an ActionScript 3.0 application: • A SWF file authored in ActionScript 3.0—This file can be a Sprite, MovieClip, or any class that extends Sprite. In AIR applications on iOS, only SWF files that do not contain ActionScript bytecode can be loaded. This means that SWF files containing embedded data, such as images and sound can be loaded, but not SWF files containing executable code. • An image file—This includes JPG, PNG, and GIF files. • An AVM1 SWF file—This is a SWF file written in ActionScript 1.0 or 2.0. (not supported in mobile AIR applications) You load these assets by using the Loader class. Loading display objects Flash Player 9 and later, Adobe AIR 1.0 and later Loader objects are used to load SWF files and graphics files into an application. The Loader class is a subclass of the DisplayObjectContainer class. A Loader object can contain only one child display object in its display list—the display object representing the SWF or graphic file that it loads. When you add a Loader object to the display list, as in the following code, you also add the loaded child display object to the display list once it loads: Last updated 3/21/2011 190 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming var pictLdr:Loader = new Loader(); var pictURL:String = "banana.jpg" var pictURLReq:URLRequest = new URLRequest(pictURL); pictLdr.load(pictURLReq); this.addChild(pictLdr); Once the SWF file or image is loaded, you can move the loaded display object to another display object container, such as the container DisplayObjectContainer object in this example: import flash.display.*; import flash.net.URLRequest; import flash.events.Event; var container:Sprite = new Sprite(); addChild(container); var pictLdr:Loader = new Loader(); var pictURL:String = "banana.jpg" var pictURLReq:URLRequest = new URLRequest(pictURL); pictLdr.load(pictURLReq); pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded); function imgLoaded(event:Event):void { container.addChild(pictLdr.content); } Monitoring loading progress Flash Player 9 and later, Adobe AIR 1.0 and later Once the file has started loading, a LoaderInfo object is created. A LoaderInfo object provides information such as load progress, the URLs of the loader and loadee, the number of bytes total for the media, and the nominal height and width of the media. A LoaderInfo object also dispatches events for monitoring the progress of the load. The following diagram shows the different uses of the LoaderInfo object—for the instance of the main class of the SWF file, for a Loader object, and for an object loaded by the Loader object: Stage LoaderInfo object loaderInfo property Instance of the main class of the SWF file Loader object contentLoaderInfo property LoaderInfo object content loaderInfo property Last updated 3/21/2011 191 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The LoaderInfo object can be accessed as a property of both the Loader object and the loaded display object. As soon as loading begins, the LoaderInfo object can be accessed through the contentLoaderInfo property of the Loader object. Once the display object has finished loading, the LoaderInfo object can also be accessed as a property of the loaded display object through the display object’s loaderInfo property. The loaderInfo property of the loaded display object refers to the same LoaderInfo object as the contentLoaderInfo property of the Loader object. In other words, a LoaderInfo object is shared between a loaded object and the Loader object that loaded it (between loader and loadee). In order to access properties of loaded content, you will want to add an event listener to the LoaderInfo object, as in the following code: import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; var ldr:Loader = new Loader(); var urlReq:URLRequest = new URLRequest("Circle.swf"); ldr.load(urlReq); ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded); addChild(ldr); function loaded(event:Event):void { var content:Sprite = event.target.content; content.scaleX = 2; } For more information, see “Handling events” on page 117. Specifying loading context Flash Player 9 and later, Adobe AIR 1.0 and later When you load an external file into Flash Player or AIR through the load() or loadBytes() method of the Loader class, you can optionally specify a context parameter. This parameter is a LoaderContext object. The LoaderContext class includes three properties that let you define the context of how the loaded content can be used: • checkPolicyFile: Use this property only when loading an image file (not a SWF file). If you set this property to true, the Loader checks the origin server for a policy file (see “Website controls (policy files)” on page 1017). This is necessary only for content originating from domains other than that of the SWF file containing the Loader object. If the server grants permission to the Loader domain, ActionScript from SWF files in the Loader domain can access data in the loaded image; in other words, you can use the BitmapData.draw() command to access data in the loaded image. Note that a SWF file from other domains than that of the Loader object can call Security.allowDomain() to permit a specific domain. • securityDomain: Use this property only when loading a SWF file (not an image). Specify this for a SWF file from a domain other than that of the file containing the Loader object. When you specify this option, Flash Player checks for the existence of a policy file, and if one exists, SWF files from the domains permitted in the cross-policy file can cross-script the loaded SWF content. You can specify flash.system.SecurityDomain.currentDomain as this parameter. Last updated 3/21/2011 192 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming • applicationDomain: Use this property only when loading a SWF file written in ActionScript 3.0 (not an image or a SWF file written in ActionScript 1.0 or 2.0). When loading the file, you can specify that the file be included in the same application domain as that of the Loader object, by setting the applicationDomain parameter to flash.system.ApplicationDomain.currentDomain. By putting the loaded SWF file in the same application domain, you can access its classes directly. This can be useful if you are loading a SWF file that contains embedded media, which you can access via their associated class names. For more information, see “Working with application domains” on page 139. Here’s an example of checking for a policy file when loading a bitmap from another domain: var context:LoaderContext = new LoaderContext(); context.checkPolicyFile = true; var urlReq:URLRequest = new URLRequest("http://www.[your_domain_here].com/photo11.jpg"); var ldr:Loader = new Loader(); ldr.load(urlReq, context); Here’s an example of checking for a policy file when loading a SWF from another domain, in order to place the file in the same security sandbox as the Loader object. Additionally, the code adds the classes in the loaded SWF file to the same application domain as that of the Loader object: var context:LoaderContext = new LoaderContext(); context.securityDomain = SecurityDomain.currentDomain; context.applicationDomain = ApplicationDomain.currentDomain; var urlReq:URLRequest = new URLRequest("http://www.[your_domain_here].com/library.swf"); var ldr:Loader = new Loader(); ldr.load(urlReq, context); For more information, see the LoaderContext class in the ActionScript 3.0 Reference for the Adobe Flash Platform. Display object example: SpriteArranger Flash Player 9 and later, Adobe AIR 1.0 and later The SpriteArranger sample application builds upon the Geometric Shapes sample application described separately in Learning ActionScript 3.0. The SpriteArranger sample application illustrates a number of concepts for dealing with display objects: • Extending display object classes • Adding objects to the display list • Layering display objects and working with display object containers • Responding to display object events • Using properties and methods of display objects To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The SpriteArranger application files can be found in the folder Examples/SpriteArranger. The application consists of the following files: Last updated 3/21/2011 193 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming File Description SpriteArranger.mxml The main application file in Flash (FLA) or Flex (MXML). or SpriteArranger.fla com/example/programmingas3/SpriteArranger/CircleSprite.as A class defining a type of Sprite object that renders a circle on-screen. com/example/programmingas3/SpriteArranger/DrawingCanvas.as A class defining the canvas, which is a display object container that contains GeometricSprite objects. com/example/programmingas3/SpriteArranger/SquareSprite.as A class defining a type of Sprite object that renders a square on-screen. com/example/programmingas3/SpriteArranger/TriangleSprite.as A class defining a type of Sprite object that renders a triangle on-screen. com/example/programmingas3/SpriteArranger/GeometricSprite.as A class that extends the Sprite object, used to define an on-screen shape. The CircleSprite, SquareSprite, and TriangleSprite each extend this class. com/example/programmingas3/geometricshapes/IGeometricShape.as The base interface defining methods to be implemented by all geometric shape classes. com/example/programmingas3/geometricshapes/IPolygon.as An interface defining methods to be implemented by geometric shape classes that have multiple sides. com/example/programmingas3/geometricshapes/RegularPolygon.as A type of geometric shape that has sides of equal length positioned symmetrically around the shape’s center. com/example/programmingas3/geometricshapes/Circle.as A type of geometric shape that defines a circle. com/example/programmingas3/geometricshapes/EquilateralTriangle.as A subclass of RegularPolygon that defines a triangle with all sides the same length. com/example/programmingas3/geometricshapes/Square.as A subclass of RegularPolygon defining a rectangle with all four sides the same length. com/example/programmingas3/geometricshapes/GeometricShapeFactory.as A class containing a “factory method” for creating shapes given a shape type and size. Defining the SpriteArranger classes Flash Player 9 and later, Adobe AIR 1.0 and later The SpriteArranger application lets the user add a variety of display objects to the on-screen “canvas.” The DrawingCanvas class defines a drawing area, a type of display object container, to which the user can add onscreen shapes. These on-screen shapes are instances of one of the subclasses of the GeometricSprite class. The DrawingCanvas class In Flex, all child display objects added to a Container object must be of a class that descends from the mx.core.UIComponent class. This application adds an instance of the DrawingCanvas class as a child of an mx.containers.VBox object, as defined in MXML code in the SpriteArranger.mxml file. This inheritance is defined in the DrawingCanvas class declaration, as follows: Last updated 3/21/2011 194 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming public class DrawingCanvas extends UIComponent The UIComponent class inherits from the DisplayObject, DisplayObjectContainer, and Sprite classes, and the code in the DrawingCanvas class uses methods and properties of those classes. The DrawingCanvas class extends the Sprite class, and this inheritance is defined in the DrawingCanvas class declaration, as follows: public class DrawingCanvas extends Sprite The Sprite class is a subclass of the DisplayObjectContainer and DisplayObject classes, and the DrawingCanvas class uses methods and properties of those classes. The DrawingCanvas() constructor method sets up a Rectangle object, bounds, which is property that is later used in drawing the outline of the canvas. It then calls the initCanvas() method, as follows: this.bounds = new Rectangle(0, 0, w, h); initCanvas(fillColor, lineColor); AS the following example shows, the initCanvas() method defines various properties of the DrawingCanvas object, which were passed as arguments to the constructor function: this.lineColor = lineColor; this.fillColor = fillColor; this.width = 500; this.height = 200; The initCanvas() method then calls the drawBounds() method, which draws the canvas using the DrawingCanvas class’s graphics property. The graphics property is inherited from the Shape class. this.graphics.clear(); this.graphics.lineStyle(1.0, this.lineColor, 1.0); this.graphics.beginFill(this.fillColor, 1.0); this.graphics.drawRect(bounds.left - 1, bounds.top - 1, bounds.width + 2, bounds.height + 2); this.graphics.endFill(); The following additional methods of the DrawingCanvas class are invoked based on user interactions with the application: • The addShape() and describeChildren() methods, which are described in “Adding display objects to the canvas” on page 195 • The moveToBack(), moveDown(), moveToFront(), and moveUp() methods, which are described in “Rearranging display object layering” on page 197 • The onMouseUp() method, which is described in “Clicking and dragging display objects” on page 196 The GeometricSprite class and its subclasses Each display object the user can add to the canvas is an instance of one of the following subclasses of the GeometricSprite class: • CircleSprite • SquareSprite • TriangleSprite The GeometricSprite class extends the flash.display.Sprite class: Last updated 3/21/2011 195 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming public class GeometricSprite extends Sprite The GeometricSprite class includes a number of properties common to all GeometricSprite objects. These are set in the constructor function based on parameters passed to the function. For example: this.size = size; this.lineColor = lColor; this.fillColor = fColor; The geometricShape property of the GeometricSprite class defines an IGeometricShape interface, which defines the mathematical properties, but not the visual properties, of the shape. The classes that implement the IGeometricShape interface are defined in the GeometricShapes sample application described in Learning ActionScript 3.0. The GeometricSprite class defines the drawShape() method, which is further refined in the override definitions in each subclass of GeometricSprite. For more information, see the “Adding display objects to the canvas” section, which follows. The GeometricSprite class also provides the following methods: • The onMouseDown() and onMouseUp() methods, which are described in “Clicking and dragging display objects” on page 196 • The showSelected() and hideSelected() methods, which are described in “Clicking and dragging display objects” on page 196 Adding display objects to the canvas Flash Player 9 and later, Adobe AIR 1.0 and later When the user clicks the Add Shape button, the application calls the addShape() method of the DrawingCanvas class. It instantiates a new GeometricSprite by calling the appropriate constructor function of one of the GeometricSprite subclasses, as the following example shows: public function addShape(shapeName:String, len:Number):void { var newShape:GeometricSprite; switch (shapeName) { case "Triangle": newShape = new TriangleSprite(len); break; case "Square": newShape = new SquareSprite(len); break; case "Circle": newShape = new CircleSprite(len); break; } newShape.alpha = 0.8; this.addChild(newShape); } Each constructor method calls the drawShape() method, which uses the graphics property of the class (inherited from the Sprite class) to draw the appropriate vector graphic. For example, the drawShape() method of the CircleSprite class includes the following code: Last updated 3/21/2011 196 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming this.graphics.clear(); this.graphics.lineStyle(1.0, this.lineColor, 1.0); this.graphics.beginFill(this.fillColor, 1.0); var radius:Number = this.size / 2; this.graphics.drawCircle(radius, radius, radius); The second to last line of the addShape() function sets the alpha property of the display object (inherited from the DisplayObject class), so that each display object added to the canvas is slightly transparent, letting the user see the objects behind it. The final line of the addChild() method adds the new display object to the child list of the instance of the DrawingCanvas class, which is already on the display list. This causes the new display object to appear on the Stage. The interface for the application includes two text fields, selectedSpriteTxt and outputTxt. The text properties of these text fields are updated with information about the GeometricSprite objects that have been added to the canvas or selected by the user. The GeometricSprite class handles this information-reporting task by overriding the toString() method, as follows: public override function toString():String { return this.shapeType + " of size " + this.size + " at " + this.x + ", " + this.y; } The shapeType property is set to the appropriate value in the constructor method of each GeometricSprite subclass. For example, the toString() method might return the following value for a CircleSprite instance recently added to the DrawingCanvas instance: Circle of size 50 at 0, 0 The describeChildren() method of the DrawingCanvas class loops through the canvas’s child list, using the numChildren property (inherited from the DisplayObjectContainer class) to set the limit of the for loop. It generates a string listing each child, as follows: var desc:String = ""; var child:DisplayObject; for (var i:int=0; i < this.numChildren; i++) { child = this.getChildAt(i); desc += i + ": " + child + '\n'; } The resulting string is used to set the text property of the outputTxt text field. Clicking and dragging display objects Flash Player 9 and later, Adobe AIR 1.0 and later When the user clicks on a GeometricSprite instance, the application calls the onMouseDown() event handler. As the following shows, this event handler is set to listen for mouse down events in the constructor function of the GeometricSprite class: this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); The onMouseDown() method then calls the showSelected() method of the GeometricSprite object. If it is the first time this method has been called for the object, the method creates a new Shape object named selectionIndicator and it uses the graphics property of the Shape object to draw a red highlight rectangle, as follows: Last updated 3/21/2011 197 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming this.selectionIndicator = new Shape(); this.selectionIndicator.graphics.lineStyle(1.0, 0xFF0000, 1.0); this.selectionIndicator.graphics.drawRect(-1, -1, this.size + 1, this.size + 1); this.addChild(this.selectionIndicator); If this is not the first time the onMouseDown() method is called, the method simply sets the selectionIndicator shape’s visible property (inherited from the DisplayObject class), as follows: this.selectionIndicator.visible = true; The hideSelected() method hides the selectionIndicator shape of the previously selected object by setting its visible property to false. The onMouseDown() event handler method also calls the startDrag() method (inherited from the Sprite class), which includes the following code: var boundsRect:Rectangle = this.parent.getRect(this.parent); boundsRect.width -= this.size; boundsRect.height -= this.size; this.startDrag(false, boundsRect); This lets the user drag the selected object around the canvas, within the boundaries set by the boundsRect rectangle. When the user releases the mouse button, the mouseUp event is dispatched. The constructor method of the DrawingCanvas sets up the following event listener: this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); This event listener is set for the DrawingCanvas object, rather than for the individual GeometricSprite objects. This is because when the GeometricSprite object is dragged, it could end up behind another display object (another GeometricSprite object) when the mouse is released. The display object in the foreground would receive the mouse up event but the display object the user is dragging would not. Adding the listener to the DrawingCanvas object ensures that the event is always handled. The onMouseUp() method calls the onMouseUp() method of the GeometricSprite object, which in turn calls the stopDrag() method of the GeometricSprite object. Rearranging display object layering Flash Player 9 and later, Adobe AIR 1.0 and later The user interface for the application includes buttons labeled Move Back, Move Down, Move Up, and Move to Front. When the user clicks one of these buttons, the application calls the corresponding method of the DrawingCanvas class: moveToBack(), moveDown(), moveUp(), or moveToFront(). For example, the moveToBack() method includes the following code: public function moveToBack(shape:GeometricSprite):void { var index:int = this.getChildIndex(shape); if (index > 0) { this.setChildIndex(shape, 0); } } The method uses the setChildIndex() method (inherited from the DisplayObjectContainer class) to position the display object in index position 0 in the child list of the DrawingCanvas instance (this). Last updated 3/21/2011 198 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Display programming The moveDown() method works similarly, except that it decrements the index position of the display object by 1 in the child list of the DrawingCanvas instance: public function moveDown(shape:GeometricSprite):void { var index:int = this.getChildIndex(shape); if (index > 0) { this.setChildIndex(shape, index - 1); } } The moveUp() and moveToFront() methods work similarly to the moveToBack() and moveDown() methods. Last updated 3/21/2011 199 Chapter 10: Working with geometry Flash Player 9 and later, Adobe AIR 1.0 and later The flash.geom package contains classes that define geometric objects such as points, rectangles, and transformation matrixes. You use these classes to define the properties of objects that are used in other classes. More Help topics flash.geom package Basics of geometry Flash Player 9 and later, Adobe AIR 1.0 and later The flash.geom package contains classes that define geometric objects such as points, rectangles, and transformation matrixes. These classes don’t necessarily provide functionality by themselves; however, they are used to define the properties of objects that are used in other classes. All the geometry classes are based around the notion that locations on the screen are represented as a two-dimensional plane. The screen is treated like a flat graph with a horizontal (x) axis and a vertical (y) axis. Any location (or point) on the screen can be represented as a pair of x and y values—the coordinates of that location. Every display object, including the Stage, has its own coordinate space. The coordinate space is an object’s own graph for plotting the locations of child display objects, drawings, and so on. The origin is at coordinate location 0, 0 (where the x and y-axes meet), and is placed at the upper-left corner of the display object. While this origin location is always true for the Stage, it is not necessarily true for other display objects. Values on the x-axis get bigger going toward the right, and smaller going toward the left. For locations to the left of the origin, the x coordinate is negative. However, contrary to traditional coordinate systems, Flash runtime coordinate values on the y-axis get bigger going down the screen and smaller going up the screen. Values above the origin have a negative y coordinate value). Since the upperleft corner of the Stage is the origin of its coordinate space, most objects on the Stage have an x coordinate greater than 0 and smaller than the Stage width. And the same object has a y coordinate larger than 0 and smaller than the Stage height. You can use Point class instances to represent individual points in a coordinate space. You can create a Rectangle instance to represent a rectangular region in a coordinate space. For advanced users, you can use a Matrix instance to apply multiple or complex transformations to a display object. Many simple transformations, such as rotation, position, and scale changes, can be applied directly to a display object using that object’s properties. For more information on applying transformations using display object properties, see “Manipulating display objects” on page 164. Important concepts and terms The following reference list contains important geometry terms: Cartesian coordinates Coordinates are commonly written as a pair of number (like 5, 12 or 17, -23). The two numbers are the x coordinate and the y coordinate, respectively. Coordinate space The graph of coordinates contained in a display object, on which its child elements are positioned. Last updated 3/21/2011 200 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry Origin The point in a coordinate space where the x-axis meets the y-axis. This point has the coordinate 0, 0. Point A single location in a coordinate space. In the 2-d coordinate system used in ActionScript, the location along the x-axis and the y-axis (the point’s coordinates) define the point. Registration point In a display object, the origin (0, 0 coordinate) of its coordinate space. Scale The size of an object relative to its original size. When used as a verb, to scale an object means to change its size by stretching or shrinking the object. Translate To change a point’s coordinates from one coordinate space to another. Transformation An adjustment to a visual characteristic of a graphic, such as rotating the object, altering its scale, skewing or distorting its shape, or altering its color. X-axis The horizontal axis in the 2-d coordinate system used in ActionScript. Y-axis The vertical axis in the 2-d coordinate system used in ActionScript. Using Point objects Flash Player 9 and later, Adobe AIR 1.0 and later A Point object defines a Cartesian pair of coordinates. It represents location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. To define a Point object, you set its x and y properties, as follows: import flash.geom.*; var pt1:Point = new Point(10, 20); // x == 10; y == 20 var pt2:Point = new Point(); pt2.x = 10; pt2.y = 20; Finding the distance between two points Flash Player 9 and later, Adobe AIR 1.0 and later You can use the distance() method of the Point class to find the distance between two points in a coordinate space. For example, the following code finds the distance between the registration points of two display objects, circle1 and circle2, in the same display object container: import flash.geom.*; var pt1:Point = new Point(circle1.x, circle1.y); var pt2:Point = new Point(circle2.x, circle2.y); var distance:Number = Point.distance(pt1, pt2); Translating coordinate spaces Flash Player 9 and later, Adobe AIR 1.0 and later If two display objects are in different display object containers, they can be in different coordinate spaces. You can use the localToGlobal() method of the DisplayObject class to translate the coordinates to the same (global) coordinate space, that of the Stage. For example, the following code finds the distance between the registration points of two display objects, circle1 and circle2, in the different display object containers: Last updated 3/21/2011 201 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry import flash.geom.*; var pt1:Point = new Point(circle1.x, circle1.y); pt1 = circle1.localToGlobal(pt1); var pt2:Point = new Point(circle2.x, circle2.y); pt2 = circle2.localToGlobal(pt2); var distance:Number = Point.distance(pt1, pt2); Similarly, to find the distance of the registration point of a display object named target from a specific point on the Stage, use the localToGlobal() method of the DisplayObject class: import flash.geom.*; var stageCenter:Point = new Point(); stageCenter.x = this.stage.stageWidth / 2; stageCenter.y = this.stage.stageHeight / 2; var targetCenter:Point = new Point(target.x, target.y); targetCenter = target.localToGlobal(targetCenter); var distance:Number = Point.distance(stageCenter, targetCenter); Moving a display object by a specified angle and distance Flash Player 9 and later, Adobe AIR 1.0 and later You can use the polar() method of the Point class to move a display object a specific distance by a specific angle. For example, the following code moves the myDisplayObject object 100 pixels by 60°: import flash.geom.*; var distance:Number = 100; var angle:Number = 2 * Math.PI * (90 / 360); var translatePoint:Point = Point.polar(distance, angle); myDisplayObject.x += translatePoint.x; myDisplayObject.y += translatePoint.y; Other uses of the Point class Flash Player 9 and later, Adobe AIR 1.0 and later You can use Point objects with the following methods and properties: Class Methods or properties Description DisplayObjectContainer areInaccessibleObjectsUnderPoint()getObject sUnderPoint() Used to return a list of objects under a point in a display object container. BitmapData hitTest() Used to define the pixel in the BitmapData object as well as the point that you are checking for a hit. Last updated 3/21/2011 202 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry Class Methods or properties Description BitmapData applyFilter() Used to define the positions of rectangles that define the operations. copyChannel() merge() paletteMap() pixelDissolve() threshold() Matrix deltaTransformPoint() transformPoint() Rectangle Used to define points for which you want to apply a transformation. Used to define these properties. bottomRight size topLeft Using Rectangle objects Flash Player 9 and later, Adobe AIR 1.0 and later A Rectangle object defines a rectangular area. A Rectangle object has a position, defined by the x and y coordinates of its upper-left corner, a width property, and a height property. You can define these properties for a new Rectangle object by calling the Rectangle() constructor function, as follows: import flash.geom.Rectangle; var rx:Number = 0; var ry:Number = 0; var rwidth:Number = 100; var rheight:Number = 50; var rect1:Rectangle = new Rectangle(rx, ry, rwidth, rheight); Resizing and repositioning Rectangle objects Flash Player 9 and later, Adobe AIR 1.0 and later There are a number of ways to resize and reposition Rectangle objects. You can directly reposition the Rectangle object by changing its x and y properties. This change has no effect on the width or height of the Rectangle object. import flash.geom.Rectangle; var x1:Number = 0; var y1:Number = 0; var width1:Number = 100; var height1:Number = 50; var rect1:Rectangle = new Rectangle(x1, y1, width1, height1); trace(rect1) // (x=0, y=0, w=100, h=50) rect1.x = 20; rect1.y = 30; trace(rect1); // (x=20, y=30, w=100, h=50) Last updated 3/21/2011 203 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry As the following code shows, when you change the left or top property of a Rectangle object, the rectangle is repositioned. The rectangle’s x and y properties match the left and top properties, respectively. However, the position of the lower-left corner of the Rectangle object does not change, so it is resized. import flash.geom.Rectangle; var x1:Number = 0; var y1:Number = 0; var width1:Number = 100; var height1:Number = 50; var rect1:Rectangle = new Rectangle(x1, y1, width1, height1); trace(rect1) // (x=0, y=0, w=100, h=50) rect1.left = 20; rect1.top = 30; trace(rect1); // (x=20, y=30, w=80, h=20) Similarly, as the following example shows, if you change the bottom or right property of a Rectangle object, the position of its upper-left corner does not change. The rectangle is resized accordingly: import flash.geom.Rectangle; var x1:Number = 0; var y1:Number = 0; var width1:Number = 100; var height1:Number = 50; var rect1:Rectangle = new Rectangle(x1, y1, width1, height1); trace(rect1) // (x=0, y=0, w=100, h=50) rect1.right = 60; trect1.bottom = 20; trace(rect1); // (x=0, y=0, w=60, h=20) You can also reposition a Rectangle object by using the offset() method, as follows: import flash.geom.Rectangle; var x1:Number = 0; var y1:Number = 0; var width1:Number = 100; var height1:Number = 50; var rect1:Rectangle = new Rectangle(x1, y1, width1, height1); trace(rect1) // (x=0, y=0, w=100, h=50) rect1.offset(20, 30); trace(rect1); // (x=20, y=30, w=100, h=50) The offsetPt() method works similarly, except that it takes a Point object as its parameter, rather than x and y offset values. You can also resize a Rectangle object by using the inflate() method, which includes two parameters, dx and dy. The dx parameter represents the number of pixels that the left and right sides of the rectangle moves from the center. The dy parameter represents the number of pixels that the top and bottom of the rectangle moves from the center: import flash.geom.Rectangle; var x1:Number = 0; var y1:Number = 0; var width1:Number = 100; var height1:Number = 50; var rect1:Rectangle = new Rectangle(x1, y1, width1, height1); trace(rect1) // (x=0, y=0, w=100, h=50) rect1.inflate(6,4); trace(rect1); // (x=-6, y=-4, w=112, h=58) Last updated 3/21/2011 204 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry The inflatePt() method works similarly, except that it takes a Point object as its parameter, rather than dx and dy values. Finding unions and intersections of Rectangle objects Flash Player 9 and later, Adobe AIR 1.0 and later You use the union() method to find the rectangular region formed by the boundaries of two rectangles: import flash.display.*; import flash.geom.Rectangle; var rect1:Rectangle = new Rectangle(0, 0, 100, 100); trace(rect1); // (x=0, y=0, w=100, h=100) var rect2:Rectangle = new Rectangle(120, 60, 100, 100); trace(rect2); // (x=120, y=60, w=100, h=100) trace(rect1.union(rect2)); // (x=0, y=0, w=220, h=160) You use the intersection() method to find the rectangular region formed by the overlapping region of two rectangles: import flash.display.*; import flash.geom.Rectangle; var rect1:Rectangle = new Rectangle(0, 0, 100, 100); trace(rect1); // (x=0, y=0, w=100, h=100) var rect2:Rectangle = new Rectangle(80, 60, 100, 100); trace(rect2); // (x=120, y=60, w=100, h=100) trace(rect1.intersection(rect2)); // (x=80, y=60, w=20, h=40) You use the intersects() method to find out whether two rectangles intersect. You can also use the intersects() method to find out whether a display object is in a certain region of the Stage. For the following code example, assume the coordinate space of the display object container that contains the circle object is the same as that of the Stage. The example shows how to use the intersects() method to determine if a display object, circle, intersects specified regions of the Stage, defined by the target1 and target2 Rectangle objects: import flash.display.*; import flash.geom.Rectangle; var circle:Shape = new Shape(); circle.graphics.lineStyle(2, 0xFF0000); circle.graphics.drawCircle(250, 250, 100); addChild(circle); var circleBounds:Rectangle = circle.getBounds(stage); var target1:Rectangle = new Rectangle(0, 0, 100, 100); trace(circleBounds.intersects(target1)); // false var target2:Rectangle = new Rectangle(0, 0, 300, 300); trace(circleBounds.intersects(target2)); // true Similarly, you can use the intersects() method to find out whether the bounding rectangles of two display objects overlap. Use the getRect() method of the DisplayObject class to include any additional space that the strokes of a display object add to a bounding region. Other uses of Rectangle objects Flash Player 9 and later, Adobe AIR 1.0 and later Rectangle objects are used in the following methods and properties: Last updated 3/21/2011 205 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry Class Methods or properties Description BitmapData applyFilter(), colorTransform(), copyChannel(), copyPixels(), draw(), fillRect(), generateFilterRect(), getColorBoundsRect(), getPixels(), merge(), paletteMap(), pixelDissolve(), setPixels(), and threshold() Used as the type for some parameters to define a region of the BitmapData object. DisplayObject getBounds(), getRect(), scrollRect, scale9Grid Used as the data type for the property or the data type returned. PrintJob addPage() Used to define the printArea parameter. Sprite startDrag() Used to define the bounds parameter. TextField getCharBoundaries() Used as the return value type. Transform pixelBounds Used as the data type. Using Matrix objects Flash Player 9 and later, Adobe AIR 1.0 and later The Matrix class represents a transformation matrix that determines how to map points from one coordinate space to another. You can perform various graphical transformations on a display object by setting the properties of a Matrix object, applying that Matrix object to the matrix property of a Transform object, and then applying that Transform object as the transform property of the display object. These transformation functions include translation (x and y repositioning), rotation, scaling, and skewing. Although you could define a matrix by directly adjusting the properties (a, b, c, d, tx, ty) of a Matrix object, it is easier to use the createBox() method. This method includes parameters that let you directly define the scaling, rotation, and translation effects of the resulting matrix. For example, the following code creates a Matrix object that scales an object horizontally by 2.0, scales it vertically by 3.0, rotates it by 45°, moving (translating) it 10 pixels to the right, and moving it 20 pixels down: var matrix:Matrix = new Matrix(); var scaleX:Number = 2.0; var scaleY:Number = 3.0; var rotation:Number = 2 * Math.PI * (45 / 360); var tx:Number = 10; var ty:Number = 20; matrix.createBox(scaleX, scaleY, rotation, tx, ty); You can also adjust the scaling, rotation, and translation effects of a Matrix object by using the scale(), rotate(), and translate() methods. Note that these methods combine with the values of the existing Matrix object. For example, the following code sets a Matrix object that scales an object by a factor of 4 and rotates it 60°, since the scale() and rotate() methods are called twice: Last updated 3/21/2011 206 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry var matrix:Matrix = new Matrix(); var rotation:Number = 2 * Math.PI * (30 / 360); // 30° var scaleFactor:Number = 2; matrix.scale(scaleFactor, scaleFactor); matrix.rotate(rotation); matrix.scale(scaleX, scaleY); matrix.rotate(rotation); myDisplayObject.transform.matrix = matrix; To apply a skew transformation to a Matrix object, adjust its b or c property. Adjusting the b property skews the matrix vertically, and adjusting the c property skews the matrix horizontally. The following code skews the myMatrix Matrix object vertically by a factor of 2: var skewMatrix:Matrix = new Matrix(); skewMatrix.b = Math.tan(2); myMatrix.concat(skewMatrix); You can apply a Matrix transformation to the transform property of a display object. For example, the following code applies a matrix transformation to a display object named myDisplayObject: var matrix:Matrix = myDisplayObject.transform.matrix; var scaleFactor:Number = 2; var rotation:Number = 2 * Math.PI * (60 / 360); // 60° matrix.scale(scaleFactor, scaleFactor); matrix.rotate(rotation); myDisplayObject.transform.matrix = matrix; The first line sets a Matrix object to the existing transformation matrix used by the myDisplayObject display object (the matrix property of the transformation property of the myDisplayObject display object). This way, the Matrix class methods that you call have a cumulative effect on the display object’s existing position, scale, and rotation. Note: The ColorTransform class is also included in the flash.geometry package. This class is used to set the colorTransform property of a Transform object. Since it does not apply any geometrical transformation, it is not discussed, in detail, here. For more information, see the ColorTransform class in the ActionScript 3.0 Reference for the Adobe Flash Platform. Geometry example: Applying a matrix transformation to a display object Flash Player 9 and later, Adobe AIR 1.0 and later The DisplayObjectTransformer sample application shows a number of features of using the Matrix class to transform a display object, including the following: • Rotating the display object • Scaling the display object • Translating (repositioning) the display object • Skewing the display object Last updated 3/21/2011 207 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry The application provides an interface for adjusting the parameters of the matrix transformation, as follows: When the user clicks the Transform button, the application applies the appropriate transformation. The original display object, and the display object rotated by -45° and scaled by 50% To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The DisplayObjectTransformer application files can be found in the folder Samples/DisplayObjectTransformer. The application consists of the following files: File Description DisplayObjectTransformer.mxml The main application file in Flash (FLA) or Flex (MXML) or DisplayObjectTransformer.fla com/example/programmingas3/geometry/MatrixTransformer.as A class that contains methods for applying matrix transformations. img/ A directory containing sample image files used by the application. Last updated 3/21/2011 208 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry Defining the MatrixTransformer class Flash Player 9 and later, Adobe AIR 1.0 and later The MatrixTransformer class includes static methods that apply geometric transformations of Matrix objects. The transform() method The transform() method includes parameters for each of the following: • sourceMatrix—The input matrix, which the method transforms • xScale and yScale—The x and y scale factor • dx and dy—The x and y translation amounts, in pixels • rotation—The rotation amount, in degrees • skew—The skew factor, as a percentage • skewType—The direction in which the skew, either "right" or "left" The return value is the resulting matrix. The transform() method calls the following static methods of the class: • skew() • scale() • translate() • rotate() Each returns the source matrix with the applied transformation. The skew() method The skew() method skews the matrix by adjusting the b and c properties of the matrix. An optional parameter, unit, determines the units used to define the skew angle, and if necessary, the method converts the angle value to radians: if (unit == { angle = } if (unit == { angle = } "degrees") Math.PI * 2 * angle / 360; "gradients") Math.PI * 2 * angle / 100; A skewMatrix Matrix object is created and adjusted to apply the skew transformation. Initially, it is the identity matrix, as follows: var skewMatrix:Matrix = new Matrix(); The skewSide parameter determines the side to which the skew is applied. If it is set to "right", the following code sets the b property of the matrix: skewMatrix.b = Math.tan(angle); Otherwise, the bottom side is skewed by adjusting the c property of the Matrix, as follows: skewMatrix.c = Math.tan(angle); Last updated 3/21/2011 209 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry The resulting skew is then applied to the existing matrix by concatenating the two matrixes, as the following example shows: sourceMatrix.concat(skewMatrix); return sourceMatrix; The scale() method The following example shows the scale() method adjusts the scale factor if it is provided as a percentage, first, and then uses the scale() method of the matrix object: if (percent) { xScale = xScale / 100; yScale = yScale / 100; } sourceMatrix.scale(xScale, yScale); return sourceMatrix; The translate() method The translate() method simply applies the dx and dy translation factors by calling the translate() method of the matrix object, as follows: sourceMatrix.translate(dx, dy); return sourceMatrix; The rotate() method The rotate() method converts the input rotation factor to radians (if it is provided in degrees or gradients), and then calls the rotate() method of the matrix object: if (unit == "degrees") { angle = Math.PI * 2 * angle / 360; } if (unit == "gradients") { angle = Math.PI * 2 * angle / 100; } sourceMatrix.rotate(angle); return sourceMatrix; Calling the MatrixTransformer.transform() method from the application Flash Player 9 and later, Adobe AIR 1.0 and later The application contains a user interface for getting the transformation parameters from the user. It then passes these values, along with the matrix property of the transform property of the display object, to the Matrix.transform() method, as follows: Last updated 3/21/2011 210 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with geometry tempMatrix = MatrixTransformer.transform(tempMatrix, xScaleSlider.value, yScaleSlider.value, dxSlider.value, dySlider.value, rotationSlider.value, skewSlider.value, skewSide ); The application then applies the return value to the matrix property of the transform property of the display object, triggering the transformation: img.content.transform.matrix = tempMatrix; Last updated 3/21/2011 211 Chapter 11: Using the drawing API Flash Player 9 and later, Adobe AIR 1.0 and later Although imported images and artwork are important, the functionality known as the drawing API, which allows you to draw lines and shapes in ActionScript, gives you the freedom to start an application with the computer equivalent of a blank canvas, on which you can create whatever images you wish. The ability to create your own graphics opens up broad possibilities for your applications. With the techniques covered here you can create a drawing program, make animated, interactive art, or programmatically create your own user interface elements, among many possibilities. More Help topics flash.display.Graphics Basics of the drawing API Flash Player 9 and later, Adobe AIR 1.0 and later The drawing API is the name for the functionality built into ActionScript that allows you to create vector graphics— lines, curves, shapes, fills, and gradients—and display them on the screen using ActionScript. The flash.display.Graphics class provides this functionality. You can draw with ActionScript on any Shape, Sprite, or MovieClip instance, using the graphics property defined in each of those classes. (Each of those classes’ graphics property is in fact an instance of the Graphics class.) If you’re just getting started with drawing with code, the Graphics class includes several methods that make it easy to draw common shapes like circles, ellipses, rectangles, and rectangles with rounded corners. You can draw them as empty lines or filled shapes. When you need more advanced functionality, the Graphics class also includes methods for drawing lines and quadratic Bézier curves, which you can use in conjunction with the trigonometry functions in the Math class to create any shape you need. Flash runtimes (such as Flash Player 10 and Adobe AIR 1.5 and later versions) add an additional API for drawing, which allow you to programmatically draw entire shapes with a single command. Once you’re familiar with the Graphics class and tasks covered in “Basics of using the drawing API”, continue to “Advanced use of the drawing API” on page 224 to learn more about these drawing API features. Important concepts and terms The following reference list contains important terms that you will encounter while using the drawing API: Anchor point One of the two end points of a quadratic Bézier curve. Control point The point that defines the direction and amount of curve of a quadratic Bézier curve. The curved line never reaches the control point; however, the line curves as though being drawn toward the control point. Coordinate space The graph of coordinates contained in a display object, on which its child elements are positioned. Fill The solid inner portion of a shape that has a line filled in with color, or all of a shape that has no outline. Gradient A color that consists of a gradual transition from one color to one or more other colors (as opposed to a solid color). Last updated 3/21/2011 212 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Point A single location in a coordinate space. In the 2-d coordinate system used in ActionScript, a point is defined by its location along the x axis and the y axis (the point’s coordinates). Quadratic Bézier curve A type of curve defined by a particular mathematical formula. In this type of curve, the shape of the curve is calculated based on the positions of the anchor points (the end points of the curve) and a control point that defines the amount and direction of the curve. Scale The size of an object relative to its original size. When used as a verb, to scale an object means to change its size by stretching or shrinking the object. Stroke The outline portion of a shape that has a line filled in with color, or the lines of an un-filled shape. Translate To change a point’s coordinates from one coordinate space to another. X axis The horizontal axis in the 2-d coordinate system used in ActionScript. Y axis The vertical axis in the 2-d coordinate system used in ActionScript. The Graphics class Flash Player 9 and later, Adobe AIR 1.0 and later Each Shape, Sprite, and MovieClip object has a graphics property, which is an instance of the Graphics class. The Graphics class includes properties and methods for drawing lines, fills, and shapes. If you want a display object to use solely as a canvas for drawing content, you can use a Shape instance. A Shape instance will perform better than other display objects for drawing, because it doesn’t have the overhead of the additional functionality in the Sprite and MovieClip classes. If you want a display object on which you can draw graphical content and also want that object to contain other display objects, you can use a Sprite instance. For more information on determining which display object to use for various tasks, see “Choosing a DisplayObject subclass” on page 163. Drawing lines and curves Flash Player 9 and later, Adobe AIR 1.0 and later All drawing that you do with a Graphics instance is based on basic drawing with lines and curves. Consequently, all ActionScript drawing must be performed using the same series of steps: • Define line and fill styles • Set the initial drawing position • Draw lines, curves, and shapes (optionally moving the drawing point) • If necessary, finish creating a fill Last updated 3/21/2011 213 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Defining line and fill styles Flash Player 9 and later, Adobe AIR 1.0 and later To draw with the graphics property of a Shape, Sprite, or MovieClip instance, you must first define the style (line size and color, fill color) to use when drawing. Just like when you use the drawing tools in Adobe® Flash® Professional or another drawing application, when you’re using ActionScript to draw you can draw with or without a stroke, and with or without a fill color. You specify the appearance of the stroke using the lineStyle() or lineGradientStyle() method. To create a solid line, use the lineStyle() method. When calling this method, the most common values you’ll specify are the first three parameters: line thickness, color, and alpha. For example, this line of code tells the Shape named myShape to draw lines that are 2 pixels thick, red (0x990000), and 75% opaque: myShape.graphics.lineStyle(2, 0x990000, .75); The default value for the alpha parameter is 1.0 (100%), so you can leave that parameter off if you want a completely opaque line. The lineStyle() method also accepts two additional parameters for pixel hinting and scale mode; for more information about using those parameters see the description of the Graphics.lineStyle() method in the ActionScript 3.0 Reference for the Adobe Flash Platform. To create a gradient line, use the lineGradientStyle() method. This method is described in “Creating gradient lines and fills” on page 216. If you want to create a filled shape, you call the beginFill(), beginGradientFill(), beginBitmapFill(), or beginShaderFill() methods before starting the drawing. The most basic of these, the beginFill() method, accepts two parameters: the fill color, and (optionally) an alpha value for the fill color. For example, if you want to draw a shape with a solid green fill, you would use the following code (assuming you’re drawing on an object named myShape): myShape.graphics.beginFill(0x00FF00); Calling any fill method implicitly ends any previous fill before starting a new one. Calling any method that specifies a stroke style replaces the previous stroke, but does not alter a previously specified fill, and vice versa. Once you have specified the line style and fill properties, the next step is to indicate the starting point for your drawing. The Graphics instance has a drawing point, like the tip of a pen on a piece of paper. Wherever the drawing point is located, that is where the next drawing action will begin. Initially a Graphics object begins with its drawing point at the point 0, 0 in the coordinate space of the object on which it’s drawing. To start the drawing at a different point, you can first call the moveTo() method before calling one of the drawing methods. This is analogous to lifting the pen tip off of the paper and moving it to a new position. With the drawing point in place you draw using a series of calls to the drawing methods lineTo() (for drawing straight lines) and curveTo() (for drawing curved lines). While you are drawing, you can call the moveTo() method at any time to move the drawing point to a new position without drawing. While drawing, if you have specified a fill color, you can close off the fill by calling the endFill() method. If you have not drawn a closed shape (in other words, if at the time you call endFill() the drawing point is not at the starting point of the shape), when you call the endFill() method the Flash runtime automatically closes the shape by drawing a straight line from the current drawing point to the location specified in the most recent moveTo() call. If you have started a fill and not called endFill(), calling beginFill() (or one of the other fill methods) closes the current fill and starts the new one. Last updated 3/21/2011 214 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Drawing straight lines Flash Player 9 and later, Adobe AIR 1.0 and later When you call the lineTo() method, the Graphics object draws a straight line from the current drawing point to the coordinates you specify as the two parameters in the method call, drawing with the line style you have specified. For example, this line of code puts the drawing point at the point 100, 100 then draws a line to the point 200, 200: myShape.graphics.moveTo(100, 100); myShape.graphics.lineTo(200, 200); The following example draws red and green triangles with a height of 100 pixels: var triangleHeight:uint = 100; var triangle:Shape = new Shape(); // red triangle, starting at point 0, 0 triangle.graphics.beginFill(0xFF0000); triangle.graphics.moveTo(triangleHeight / 2, 0); triangle.graphics.lineTo(triangleHeight, triangleHeight); triangle.graphics.lineTo(0, triangleHeight); triangle.graphics.lineTo(triangleHeight / 2, 0); // green triangle, starting at point 200, 0 triangle.graphics.beginFill(0x00FF00); triangle.graphics.moveTo(200 + triangleHeight / 2, 0); triangle.graphics.lineTo(200 + triangleHeight, triangleHeight); triangle.graphics.lineTo(200, triangleHeight); triangle.graphics.lineTo(200 + triangleHeight / 2, 0); this.addChild(triangle); Drawing curves Flash Player 9 and later, Adobe AIR 1.0 and later The curveTo() method draws a quadratic Bézier curve. This draws an arc that connects two points (called anchor points) while bending toward a third point (called the control point). The Graphics object uses the current drawing position as the first anchor point. When you call the curveTo() method, you pass four parameters: the x and y coordinates of the control point, followed by the x and y coordinates of the second anchor point. For example, the following code draws a curve starting at point 100, 100 and ending at point 200, 200. Because the control point is at point 175, 125, this creates a curve that moves to the right and then downward: myShape.graphics.moveTo(100, 100); myShape.graphics.curveTo(175, 125, 200, 200); The following example draws red and green circular objects with a width and height of 100 pixels. Note that due to the nature of the quadratic Bézier equation, these are not perfect circles: Last updated 3/21/2011 215 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API var size:uint = 100; var roundObject:Shape = new Shape(); // red circular shape roundObject.graphics.beginFill(0xFF0000); roundObject.graphics.moveTo(size / 2, 0); roundObject.graphics.curveTo(size, 0, size, size / 2); roundObject.graphics.curveTo(size, size, size / 2, size); roundObject.graphics.curveTo(0, size, 0, size / 2); roundObject.graphics.curveTo(0, 0, size / 2, 0); // green circular shape roundObject.graphics.beginFill(0x00FF00); roundObject.graphics.moveTo(200 + size / 2, 0); roundObject.graphics.curveTo(200 + size, 0, 200 + size, size / 2); roundObject.graphics.curveTo(200 + size, size, 200 + size / 2, size); roundObject.graphics.curveTo(200, size, 200, size / 2); roundObject.graphics.curveTo(200, 0, 200 + size / 2, 0); this.addChild(roundObject); Drawing shapes using built-in methods Flash Player 9 and later, Adobe AIR 1.0 and later For convenience when drawing common shapes such as circles, ellipses, rectangles, and rectangles with rounded corners, ActionScript 3.0 has methods that draw these common shapes for you. These are the drawCircle(), drawEllipse(), drawRect(), and drawRoundRect() methods of the Graphics class. These methods may be used in place of the lineTo() and curveTo() methods. Note, however, that you must still specify line and fill styles before calling these methods. The following example recreates the example of drawing red, green, and blue squares with width and height of 100 pixels. This code uses the drawRect() method, and additionally specifies that the fill color has an alpha of 50% (0.5): var squareSize:uint = 100; var square:Shape = new Shape(); square.graphics.beginFill(0xFF0000, 0.5); square.graphics.drawRect(0, 0, squareSize, squareSize); square.graphics.beginFill(0x00FF00, 0.5); square.graphics.drawRect(200, 0, squareSize, squareSize); square.graphics.beginFill(0x0000FF, 0.5); square.graphics.drawRect(400, 0, squareSize, squareSize); square.graphics.endFill(); this.addChild(square); In a Sprite or MovieClip object, the drawing content created with the graphics property always appears behind all child display objects that are contained by the object. Also, the graphics property content is not a separate display object so it does not appear in the list of a Sprite or MovieClip object’s children. For example, the following Sprite object has a circle drawn with its graphics property, and it has a TextField object in its list of child display objects: Last updated 3/21/2011 216 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API var mySprite:Sprite = new Sprite(); mySprite.graphics.beginFill(0xFFCC00); mySprite.graphics.drawCircle(30, 30, 30); var label:TextField = new TextField(); label.width = 200; label.text = "They call me mellow yellow..."; label.x = 20; label.y = 20; mySprite.addChild(label); this.addChild(mySprite); Note that the TextField appears on top of the circle drawn with the graphics object. Creating gradient lines and fills Flash Player 9 and later, Adobe AIR 1.0 and later The graphics object can also draw strokes and fills with gradients rather than solid colors. A gradient stroke is created with the lineGradientStyle() method and a gradient fill is created with the beginGradientFill() method. Both methods accept the same parameters. The first four are required: type, colors, alphas, and ratios. The remaining four are optional but are useful for advanced customizing. • The first parameter specifies the type of gradient you are creating. Acceptable values are GradientType.LINEAR or GradientType.RADIAL. • The second parameter specifies the array of the color values to use. In a linear gradient, the colors will be arranged from left to right. In a radial gradient, they will be arranged from inside to outside. The order of the colors of the array represents the order that the colors will be drawn in the gradient. • The third parameter specifies the alpha transparency values of the corresponding colors in the previous parameter. • The fourth parameter specifies ratios, or the emphasis each color has within the gradient. Acceptable values range from 0-255. These values do not represent any width or height, but rather the position within the gradient; 0 represents the beginning of the gradient, 255 represents the end of the gradient. The array of ratios must increase sequentially and have the same number of entries as both the color and alpha arrays specified in the second and third parameters. Although the fifth parameter, the transformation matrix, is optional, it is commonly used because it provides an easy and powerful way to control the gradient’s appearance. This parameter accepts a Matrix instance. The easiest way to create a Matrix object for a gradient is to use the Matrix class’s createGradientBox() method. Defining a Matrix object for use with a gradient Flash Player 9 and later, Adobe AIR 1.0 and later You use the beginGradientFill() and lineGradientStyle() methods of the flash.display.Graphics class to define gradients for use in shapes. When you define a gradient, you supply a matrix as one of the parameters of these methods. Last updated 3/21/2011 217 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API The easiest way to define the matrix is by using the Matrix class’s createGradientBox() method, which creates a matrix that is used to define the gradient. You define the scale, rotation, and position of the gradient using the parameters passed to the createGradientBox() method. The createGradientBox() method accepts the following parameters: • Gradient box width: the width (in pixels) to which the gradient will spread • Gradient box height: the height (in pixels) to which the gradient will spread • Gradient box rotation: the rotation (in radians) that will be applied to the gradient • Horizontal translation: how far (in pixels) the gradient is shifted horizontally • Vertical translation: how far (in pixels) the gradient is shifted vertically For example, consider a gradient with the following characteristics: • GradientType.LINEAR • Two colors, green and blue, with the ratios array set to [0, • SpreadMethod.PAD • InterpolationMethod.LINEAR_RGB 255] The following examples show gradients in which the rotation parameter of the createGradientBox() method differs as indicated, but all other settings stay the same: width = 100; height = 100; rotation = 0; tx = 0; ty = 0; width = 100; height = 100; rotation = Math.PI/4; // 45° tx = 0; ty = 0; width = 100; height = 100; rotation = Math.PI/2; // 90° tx = 0; ty = 0; The following examples show the effects on a green-to-blue linear gradient in which the rotation, tx, and ty parameters of the createGradientBox() method differ as indicated, but all other settings stay the same: Last updated 3/21/2011 218 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API width = 50; height = 100; rotation = 0; tx = 0; ty = 0; width = 50; height = 100; rotation = 0 tx = 50; ty = 0; width = 100; height = 50; rotation = Math.PI/2; // 90° tx = 0; ty = 0; width = 100; height = 50; rotation = Math.PI/2; // 90° tx = 0; ty = 50; The width, height, tx, and ty parameters of the createGradientBox() method affect the size and position of a radial gradient fill as well, as the following example shows: width = 50; height = 100; rotation = 0; tx = 25; ty = 0; The following code produces the last radial gradient illustrated: Last updated 3/21/2011 219 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API import flash.display.Shape; import flash.display.GradientType; import flash.geom.Matrix; var var var var var var var type:String = GradientType.RADIAL; colors:Array = [0x00FF00, 0x000088]; alphas:Array = [1, 1]; ratios:Array = [0, 255]; spreadMethod:String = SpreadMethod.PAD; interp:String = InterpolationMethod.LINEAR_RGB; focalPtRatio:Number = 0; var matrix:Matrix = new Matrix(); var boxWidth:Number = 50; var boxHeight:Number = 100; var boxRotation:Number = Math.PI/2; // 90° var tx:Number = 25; var ty:Number = 0; matrix.createGradientBox(boxWidth, boxHeight, boxRotation, tx, ty); var square:Shape = new Shape; square.graphics.beginGradientFill(type, colors, alphas, ratios, matrix, spreadMethod, interp, focalPtRatio); square.graphics.drawRect(0, 0, 100, 100); addChild(square); Note that the width and height of the gradient fill is determined by the width and height of the gradient matrix rather than the width or height that is drawn using the Graphics object. When drawing with the Graphics object, you draw what exists at those coordinates in the gradient matrix. Even if you use one of the shape methods of a Graphics object such as drawRect(), the gradient does not stretch itself to the size of the shape that is drawn—the gradient’s size must be specified in the gradient matrix itself. The following illustrates the visual difference between the dimensions of the gradient matrix and the dimensions of the draw itself: var myShape:Shape = new Shape(); var gradientBoxMatrix:Matrix = new Matrix(); gradientBoxMatrix.createGradientBox(100, 40, 0, 0, 0); myShape.graphics.beginGradientFill(GradientType.LINEAR, [0xFF0000, 0x00FF00, 0x0000FF], [1, 1, 1], [0, 128, 255], gradientBoxMatrix); myShape.graphics.drawRect(0, 0, 50, 40); myShape.graphics.drawRect(0, 50, 100, 40); myShape.graphics.drawRect(0, 100, 150, 40); myShape.graphics.endFill(); this.addChild(myShape); This code draws three gradients with the same fill style, specified with an equal distribution of red, green, and blue. The gradients are drawn using the drawRect() method with pixel widths of 50, 100, and 150 respectively. The gradient matrix which is specified in the beginGradientFill() method is created with a width of 100 pixels. This means that the first gradient will encompass only half of the gradient spectrum, the second will encompass all of it, and the third will encompass all of it and have an additional 50 pixels of blue extending to the right. Last updated 3/21/2011 220 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API The lineGradientStyle() method works similarly to beginGradientFill() except that in addition to defining the gradient, you must specify the thickness of the stroke using the lineStyle() method before drawing. The following code draws a box with a red, green, and blue gradient stroke: var myShape:Shape = new Shape(); var gradientBoxMatrix:Matrix = new Matrix(); gradientBoxMatrix.createGradientBox(200, 40, 0, 0, 0); myShape.graphics.lineStyle(5, 0); myShape.graphics.lineGradientStyle(GradientType.LINEAR, [0xFF0000, 0x00FF00, 0x0000FF], [1, 1, 1], [0, 128, 255], gradientBoxMatrix); myShape.graphics.drawRect(0, 0, 200, 40); this.addChild(myShape); For more information on the Matrix class, see “Using Matrix objects” on page 205. Using the Math class with drawing methods Flash Player 9 and later, Adobe AIR 1.0 and later A Graphics object draws circles and squares, but can also draw more complex forms, particularly when the drawing methods are used in combination with the properties and methods of the Math class. The Math class contains constants of common mathematical interest, such as Math.PI (approximately 3.14159265...), a constant for the ratio of the circumference of a circle to its diameter. It also contains methods for trigonometry functions, including Math.sin(), Math.cos(), and Math.tan() among others. Drawing shapes using these methods and constants create more dynamic visual effects, particularly when used with repetition or recursion. Many methods of the Math class expect circular measurements in units of radians rather than degrees. Converting between these two types of units is a common use of the Math class: var degrees = 121; var radians = degrees * Math.PI / 180; trace(radians) // 2.111848394913139 The following example creates a sine wave and a cosine wave, to highlight the difference between the Math.sin() and Math.cos() methods for a given value. var var var var var var sinWavePosition = 100; cosWavePosition = 200; sinWaveColor:uint = 0xFF0000; cosWaveColor:uint = 0x00FF00; waveMultiplier:Number = 10; waveStretcher:Number = 5; var i:uint; for(i = 1; i < stage.stageWidth; i++) { var sinPosY:Number = Math.sin(i / waveStretcher) * waveMultiplier; var cosPosY:Number = Math.cos(i / waveStretcher) * waveMultiplier; graphics.beginFill(sinWaveColor); graphics.drawRect(i, sinWavePosition + sinPosY, 2, 2); graphics.beginFill(cosWaveColor); graphics.drawRect(i, cosWavePosition + cosPosY, 2, 2); } Last updated 3/21/2011 221 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Animating with the drawing API Flash Player 9 and later, Adobe AIR 1.0 and later One advantage of creating content with the drawing API is that you are not limited to positioning your content once. What you draw can be modified by maintaining and modifying the variables you use to draw. You can convey animation by changing variables and redrawing, either over a period of frames or with a timer. For example, the following code changes the display with each passing frame (by listening to the Event.ENTER_FRAME event), incrementing the current degree count, and directs the graphics object to clear and redraw with the updated position. stage.frameRate = 31; var currentDegrees:Number = 0; var radius:Number = 40; var satelliteRadius:Number = 6; var container:Sprite = new Sprite(); container.x = stage.stageWidth / 2; container.y = stage.stageHeight / 2; addChild(container); var satellite:Shape = new Shape(); container.addChild(satellite); addEventListener(Event.ENTER_FRAME, doEveryFrame); function doEveryFrame(event:Event):void { currentDegrees += 4; var radians:Number = getRadians(currentDegrees); var posX:Number = Math.sin(radians) * radius; var posY:Number = Math.cos(radians) * radius; satellite.graphics.clear(); satellite.graphics.beginFill(0); satellite.graphics.drawCircle(posX, posY, satelliteRadius); } function getRadians(degrees:Number):Number { return degrees * Math.PI / 180; } To produce a significantly different result, you can experiment by modifying the initial seed variables at the beginning of the code, currentDegrees, radius, and satelliteRadius. For example, try shrinking the radius variable and/or increasing the totalSatellites variable. This is only one example of how the drawing API can create a visual display whose complexity conceals the simplicity of its creation. Last updated 3/21/2011 222 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Drawing API example: Algorithmic Visual Generator Flash Player 9 and later, Adobe AIR 1.0 and later The Algorithmic Visual Generator example dynamically draws to the stage several “satellites”, or circles moving in a circular orbit. Among the features explored are: • Using the drawing API to draw a basic shape with dynamic appearances • Connecting user interaction with the properties that are used in a draw • Conveying animation by clearing the stage on each frame and redrawing The example in the previous subsection animated a solitary “satellite” using the Event.ENTER_FRAME event. This example expands upon this, building a control panel with series of sliders that immediately update the visual display of several satellites. This example formalizes the code into external classes and wraps the satellite creation code into a loop, storing a reference to each satellite in a satellites array. To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The application files can be found in the folder Samples/AlgorithmicVisualGenerator. This folder contains the following files: File Description AlgorithmicVisualGenerator.fla The main application file in Flash Professional (FLA). com/example/programmingas3/algorithmic/AlgorithmicVisualGenerator.as The class that provides the main functionality of the application, including drawing satellites on the stage and responding to events from the control panel to update the variables that affect the drawing of satellites. com/example/programmingas3/algorithmic/ControlPanel.as A class that manages user interaction with several sliders and dispatching events when this occurs. com/example/programmingas3/algorithmic/Satellite.as A class which represents the display object that rotates in an orbit around a central point and contains properties related to its current draw state. Setting the listeners Flash Player 9 and later, Adobe AIR 1.0 and later The application first creates three listeners. The first listens for a dispatched event from the control panel that a rebuild of the satellites is necessary. The second listens to changes to the size of the SWF file’s stage. The third listens for each passing frame in the SWF file and to redraw using the doEveryFrame() function. Creating the satellites Flash Player 9 and later, Adobe AIR 1.0 and later Once these listeners are set, the build() function is called. This function first calls the clear() function, which empties the satellites array and clears any previous draws to the stage. This is necessary since the build() function could be recalled whenever the control panel sends an event to do so, such as when the color settings have been changed. In such a case, the satellites must be removed and recreated. Last updated 3/21/2011 223 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API The function then creates the satellites, setting the initial properties needed for creation, such as a the position variable, which starts at a random position in the orbit, and the color variable, which in this example does not change once the satellite has been created. As each satellite is created, a reference to it is added to the satellites array. When the doEveryFrame() function is called, it will update to all satellites in this array. Updating the satellite position Flash Player 9 and later, Adobe AIR 1.0 and later The doEveryFrame() function is the heart of the application’s animation process. It is called for every frame, at a rate equal the framerate of the SWF file. Because the variables of the draw change slightly, this conveys the appearance of animation. The function first clears all previous draws and redraws the background. Then, it loops through each satellite container and increments the position property of each satellite, and updates the radius and orbitRadius properties that may have changed from user interaction with the control panel. Finally, the satellite updates to its new position by calling the draw() method of the Satellite class. Note that the counter, i, only increments up to the visibleSatellites variable. This is because if the user has limited the amount of satellites that are displayed through the control panel, the remaining satellites in the loop should not be redrawn but should instead be hidden. This occurs in a loop which immediately follows the loop responsible for drawing. When the doEveryFrame() function completes, the number of visibleSatellites update in position across the screen. Responding to user interaction Flash Player 9 and later, Adobe AIR 1.0 and later User interaction occurs via the control panel, which is managed by the ControlPanel class. This class sets a listener along with the individual minimum, maximum, and default values of each slider. As the user moves these sliders, the changeSetting() function is called. This function updates the properties of the control panel. If the change requires a rebuild of the display, an event is dispatched which is then handled in the main application file. As the control panel settings change, the doEveryFrame() function draws each satellite with the updated variables. Customizing further Flash Player 9 and later, Adobe AIR 1.0 and later This example is only a basic schematic of how to generate visuals using the drawing API. It uses relatively few lines of code to create an interactive experience that appears quite complex. Even so, this example could be extended with minor changes. A few ideas: • The doEveryFrame() function could increment the color value of the satellite. • The doEveryFrame() function could shrink or expand the satellite radius over time. • The satellite radius does not have to be circular; it could use the Math class to move according to a sine wave, for example. • Satellites could use hit detection with other satellites. Last updated 3/21/2011 224 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API The drawing API can be used as an alternative to creating visual effects in the Flash authoring environment, drawing basic shapes at run time. But it can also be used to create visual effects of a variety and scope that are not possible to create by hand. Using the drawing API and a bit of mathematics, the ActionScript author can give life to many unexpected creations. Advanced use of the drawing API Flash Player 10 and later, Adobe AIR 1.5 and later Flash Player 10, Adobe AIR 1.5, and later Flash runtimes, support an advanced set of drawing features. The drawing API enhancements for these runtimes expand upon the drawing methods from previous releases so you can establish data sets to generate shapes, alter shapes at runtime, and create three-dimensional effects. The drawing API enhancements consolidate existing methods into alternative commands. These commands leverage vector arrays and enumeration classes to provide data sets for drawing methods. Using vector arrays allows for more complex shapes to render quickly and for developers to change the array values programmatically for dynamic shape rendering at runtime. The drawing features introduced in Flash Player 10 are described in the following sections: “Drawing Paths” on page 225, “Defining winding rules” on page 227, “Using graphics data classes” on page 229, and “About using drawTriangles()” on page 231. The following tasks are things you’ll likely want to accomplish using the advanced drawing API in ActionScript: • Using Vector objects to store data for drawing methods • Defining paths for drawing shapes programmatically • Defining winding rules to determine how overlapping shapes are filled • Using graphics data classes • Using triangles and drawing methods for three-dimensional effects Important concepts and terms The following reference list contains important terms that you will encounter in this section: • Vector: An array of values all of the same data type. A Vector object can store an array of values that drawing methods use to construct lines and shapes with a single command. For more information on Vector objects, see “Indexed arrays” on page 26. • Path: A path is made up of one or more straight or curved segments. The beginning and end of each segment are marked by coordinates, which work like pins holding a wire in place. A path can be closed (for example, a circle), or open, with distinct endpoints (for example, a wavy line). • Winding: The direction of a path as interpreted by the renderer; either positive (clockwise) or negative (counterclockwise). • GraphicsStroke: A class for setting the line style. While the term “stroke” isn’t part of the drawing API enhancements, the use of a class to designate a line style with its own fill property is part of the new drawing API. You can dynamically adjust a line’s style using the GraphicsStroke class. • Fill object: Objects created using display classes like flash.display.GraphicsBitmapFill and flash.display.GraphicsGradientFill that are passed to the drawing command Graphics.drawGraphicsData(). Fill objects and the enhanced drawing commands introduce a more object-oriented programming approach to replicating Graphics.beginBitmapFill() and Graphics.beginGradientFill(). Last updated 3/21/2011 225 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Drawing Paths Flash Player 10 and later, Adobe AIR 1.5 and later The section on drawing lines and curves (see “Drawing lines and curves” on page 212) introduced the commands for drawing a single line (Graphics.lineTo()) or curve (Graphics.curveTo()) and then moving the line to another point (Graphics.moveTo()) to form a shape. Some of the ActionScript drawing API enhancements, like Graphics.drawPath() and Graphics.drawTriangles(), utilize the existing drawing commands as parameters. So, you can provide a series of Graphics.lineTo(), Graphics.curveTo(), or Graphics.moveTo() commands for the Flash runtime to execute in a single statement. Two of the drawing API enhancements allow Graphics.drawPath() and Graphics.drawTriangles() to consolidate existing commands: • The GraphicsPathCommand enumeration class: The GraphicsPathCommand class associates several drawing commands with constant values. You use a series of these values as parameters for the Graphics.drawPath() method. Then with a single command, you can render an entire shape, or several shapes. You can also dynamically alter the values passed to these methods to change an existing shape. • Vector arrays: Vector arrays contain a series of values of a specific data type. So, you store a series of GraphicsPathCommand constants in a Vector object, and a series of coordinates in another Vector object. Graphics.drawPath() or Graphics.drawTriangles() assigns those values together to generate a drawing path or shape. You no longer need separate commands for each segment of a shape. For example, the Graphics.drawPath() method consolidates Graphics.moveTo(), Graphics.lineTo(), and Graphics.curveTo() into a single method. Instead of each method called separately, they are abstracted into numeric identifiers as defined in the GraphicsPathCommand class. A moveTo() operation is signified by a 1, while a lineTo() operation is a 2. Store an array of these values in a Vector. object for use in the commands parameter. Then, create another array containing coordinates in a Vector. object for the data parameter. Each GraphicsPathCommand value corresponds to the coordinate values stored in the data parameter where two consecutive numbers define a location in the target coordinate space. Note: The values in the vector are not Point objects; the vector is a series of numbers where each group of two numbers represents an x/y coordinate pair. The Graphics.drawPath() method matches each command with its respective point values (a collection of two or four numbers) to generate a path in the Graphics object: Last updated 3/21/2011 226 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API package{ import flash.display.*; public class DrawPathExample extends Sprite { public function DrawPathExample(){ var square_commands:Vector. = new Vector.(5,true); square_commands[0] = 1;//moveTo square_commands[1] = 2;//lineTo square_commands[2] = 2; square_commands[3] = 2; square_commands[4] = 2; var square_coord:Vector. = new Vector.(10,true); square_coord[0] = 20; //x square_coord[1] = 10; //y square_coord[2] = 50; square_coord[3] = 10; square_coord[4] = 50; square_coord[5] = 40; square_coord[6] = 20; square_coord[7] = 40; square_coord[8] = 20; square_coord[9] = 10; graphics.beginFill(0x442266);//set the color graphics.drawPath(square_commands, square_coord); } } } In the above example, each command and coordinate pair is assigned individually to show their position in the array, but they can be assigned in a single statement. The following example draws the same star by assigning the values for each array in a single push() statement: package{ import flash.display.*; public class DrawPathExample extends Sprite { public function DrawPathExample(){ var square_commands:Vector. = new Vector.(); square_commands.push(1, 2, 2, 2, 2); var square_coord:Vector. = new Vector.(); square_coord.push(20,10, 50,10, 50,40, 20,40, 20,10); graphics.beginFill(0x442266); graphics.drawPath(square_commands, square_coord); } } } Last updated 3/21/2011 227 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Defining winding rules Flash Player 10 and later, Adobe AIR 1.5 and later The enhanced drawing API also introduces the concept of path “winding”: the direction for a path. The winding for a path is either positive (clockwise) or negative (counter-clockwise). The order in which the renderer interprets the coordinates provided by the vector for the data parameter determines the winding. A 0 3 1 0 2 1 3 2 B C Positive and negative winding A. Arrows indicate drawing direction B. Positively wound (clockwise) C. Negatively wound (counter-clockwise) Additionally, notice that the Graphics.drawPath() method has an optional third parameter called “winding”: drawPath(commands:Vector., data:Vector., winding:String = "evenOdd"):void In this context, the third parameter is a string or a constant that specifies the winding or fill rule for intersecting paths. (The constant values are defined in the GraphicsPathWinding class as GraphicsPathWinding.EVEN_ODD or GraphicsPathWinding.NON_ZERO.) The winding rule is important when paths intersect. The even-odd rule is the standard winding rule and is the rule used by the legacy drawing API. The Even-odd rule is also the default rule for the Graphics.drawPath() method. With the even-odd winding rule, any intersecting paths alternate between open and closed fills. If two squares drawn with the same fill intersect, the area in which the intersection occurs is filled. Generally, adjacent areas are neither both filled nor both unfilled. The non-zero rule, on the other hand, depends on winding (drawing direction) to determine whether areas defined by intersecting paths are filled. When paths of opposite winding intersect, the area defined is unfilled, much like with even-odd. For paths of the same winding, the area that would be unfilled is filled: A B Winding rules for intersecting areas A. Even-odd winding rule B. Non-zero winding rule Last updated 3/21/2011 228 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Winding rule names Flash Player 10 and later, Adobe AIR 1.5 and later The names refer to a more specific rule that defines how these fills are managed. Positively wound paths are assigned a value of +1; negatively wound paths are assigned a value of -1. Starting from a point within an enclosed area of a shape, draw a line from that point extending out indefinitely. The number of times that line crosses a path, and the combined values of those paths, are used to determine the fill. For even-odd winding, the count of times the line crosses a path is used. When the count is odd, the area is filled. For even counts, the area is unfilled. For non-zero winding, the values assigned to the paths are used. When the combined values of the path are not 0, the area is filled. When the combined value is 0, the area is unfilled. A B Winding rule counts and fills A. Even-odd winding rule B. Non-zero winding rule Using winding rules Flash Player 10 and later, Adobe AIR 1.5 and later These fill rules are complicated, but in some situations they are necessary. For example, consider drawing a star shape. With the standard even-odd rule, the shape would require ten different lines. With the non-zero winding rule, those ten lines are reduced to five. Here is the ActionScript for a star with five lines and a non-zero winding rule: graphics.beginFill(0x60A0FF); graphics.drawPath( Vector.([1,2,2,2,2]), Vector.([66,10, 23,127, 122,50, 10,49, 109,127]), GraphicsPathWinding.NON_ZERO); And here is the star shape: A B C A star shape using different winding rules A. Even-odd 10 lines B. Even-odd 5 lines C. Non-zero 5 lines And, as images are animated or used as textures on three-dimensional objects and overlap, the winding rules become more important. Last updated 3/21/2011 229 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API Using graphics data classes Flash Player 10 and later, Adobe AIR 1.5 and later The enhanced drawing API introduces a collection of classes in the flash.display package of the type IGraphicsData (an interface each of the classes implement). The classes that implement the IGraphicsData interface serve as data containers for the methods of the drawing API. The following classes implement the IGraphicsData interface: • GraphicsBitmapFill • GraphicsEndFill • GraphicsGradientFill • GraphicsPath • GraphicsShaderFill • GraphicsSolidFill • GraphicsStroke • GraphicsTrianglePath With these classes, you can store complete drawings in a vector object array of IGraphicsData type (Vector.) that can be reused as the data source for other shape instances or to store drawing information for later use. Notice you have multiple fill classes for each style of fill, but only one stroke class. ActionScript has only one stroke IGraphicsData class because the stroke class uses the fill classes to define its style. So every stroke is actually the stroke class and a fill class. Otherwise, the API for these graphics data classes mirror the methods they represent in the flash.display.Graphics class: Graphics Method Data Class beginBitmapFill() GraphicsBitmapFill beginFill() GraphicsSolidFill beginGradientFill() GraphicsGradientFill beginShaderFill() GraphicsShaderFill lineBitmapStyle() GraphicsStroke + GraphicsBitmapFill lineGradientStyle() GraphicsStroke + GraphicsGradientFill lineShaderStyle() GraphicsStroke + GraphicsShaderFill lineStyle() GraphicsStroke + GraphicsSolidFill moveTo() GraphicsPath lineTo() curveTo() drawPath() drawTriangles() GraphicsTrianglePath Last updated 3/21/2011 230 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API In addition, the GraphicsPath class has its own GraphicsPath.moveTo(), GraphicsPath.lineTo(), GraphicsPath.curveTo(), GraphicsPath.wideLineTo(), and GraphicsPath.wideMoveTo() utility methods to easily define those commands for a GraphicsPath instance. These utility methods ease defining or updating the commands and data values directly. Once you have a collection of IGraphicsData instances, use Graphics.drawGraphicsData() method to render the graphics. The Graphics.drawGraphicsData() method runs a vector of IGraphicsData instances through the drawing API in sequential order: // stroke object var stroke:GraphicsStroke = new GraphicsStroke(3); stroke.joints = JointStyle.MITER; stroke.fill = new GraphicsSolidFill(0x102020);// solid stroke // fill object var fill:GraphicsGradientFill = new GraphicsGradientFill(); fill.colors = [0x0000FF, 0xEEFFEE]; fill.matrix = new Matrix(); fill.matrix.createGradientBox(70,70, Math.PI/2); // path object var path:GraphicsPath = new GraphicsPath(new Vector.(), new Vector.()); path.commands.push(1,2,2); path.data.push(125,0, 50,100, 175,0); // combine objects for complete drawing var drawing:Vector. = new Vector.(); drawing.push(stroke, fill, path); // draw the drawing graphics.drawGraphicsData(drawing); By modifying one value in the path used by the drawing in the example, the shape can be redrawn multiple times for a more complex image: // draw the drawing multiple times // change one value to modify each variation graphics.drawGraphicsData(drawing); path.data[2] += 200; graphics.drawGraphicsData(drawing); path.data[2] -= 150; graphics.drawGraphicsData(drawing); path.data[2] += 100; graphics.drawGraphicsData(drawing); path.data[2] -= 50;graphicsS.drawGraphicsData(drawing); Though IGraphicsData objects can define fill and stroke styles, the fill and stroke styles are not a requirement. In other words, Graphics class methods can be used to set styles while IGraphicsData objects can be used to draw a saved collection of paths, or vice-versa. Note: Use the Graphics.clear() method to clear out a previous drawing before starting a new one; unless you're adding on to the original drawing, as seen in the example above. As you change a single portion of a path or collection of IGraphicsData objects, redraw the entire drawing to see the changes. When using graphics data classes, the fill is rendered whenever three or more points are drawn, because the shape is inherently closed at that point. Even though the fill closes, the stroke does not, and this behavior is different than when using multiple Graphics.lineTo() or Graphics.moveTo() commands. Last updated 3/21/2011 231 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the drawing API About using drawTriangles() Flash Player 10 and later, Adobe AIR 1.5 and later Another advanced method introduced in Flash Player 10 and Adobe AIR 1.5, Graphics.drawTriangles(), is like the Graphics.drawPath() method. The Graphics.drawTriangles() method also uses a Vector. object to specify point locations for drawing a path. However, the real purpose for the Graphics.drawTriangles() method is to facilitate three-dimensional effects through ActionScript. For information about using Graphics.drawTriangles() to produce three-dimensional effects, see “Using triangles for 3D effects” on page 341. Last updated 3/21/2011 232 Chapter 12: Working with bitmaps Flash Player 9 and later, Adobe AIR 1.0 and later In addition to its vector drawing capabilities, ActionScript 3.0 includes the ability to create bitmap images or manipulate the pixel data of external bitmap images that are loaded into a SWF. With the ability to access and change individual pixel values, you can create your own filter-like image effects and use the built-in noise functions to create textures and random noise. Basics of working with bitmaps Flash Player 9 and later, Adobe AIR 1.0 and later When you work with digital images, you’re likely to encounter two main types of graphics: bitmap and vector. Bitmap graphics, also known as raster graphics, are composed of tiny squares (pixels) that are arranged in a rectangular grid formation. Vector graphics are composed of mathematically generated geometric shapes such as lines, curves, and polygons. Bitmap images are defined by the width and height of the image, measured in pixels, and the number of bits contained in each pixel, which represents the number of colors a pixel can contain. In the case of a bitmap image that utilizes the RGB color model, the pixels are made up of three bytes: red, green, and blue. Each of these bytes contains a value ranging from 0 to 255. When the bytes are combined within the pixel, they produce a color similar to an artist mixing paint colors. For example, a pixel containing byte values of red-255, green-102 and blue-0 would produce a vibrant orange color. The quality of a bitmap image is determined by combining the resolution of the image with its color depth bit value. Resolution relates to the number of pixels contained within an image. The greater the number of pixels, the higher the resolution and the finer the image appears. Color depth relates to the amount of information a pixel can contain. For example, an image that has a color depth value of 16 bits per pixel cannot represent the same number of colors as an image that has a color depth of 48 bits. As a result, the 48-bit image will have smoother degrees of shading than its 16bit counterpart. Because bitmap graphics are resolution-dependent, they don’t scale very well. This is most noticeable when bitmap images are scaled up in size. Scaling up a bitmap usually results in a loss of detail and quality. Bitmap file formats Bitmap images are grouped into a number of common file formats. These formats use different types of compression algorithms to reduce file size, as well as optimize image quality based on the end purpose of the image. The bitmap image formats supported by Adobe runtimes are BMP, GIF, JPG, PNG, and TIFF. BMP The BMP (bit mapped) format is a default image format used by the Microsoft Windows operating system. It does not use any form of compression algorithm and as such usually results in large file sizes. Last updated 3/21/2011 233 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps GIF The Graphics Interchange Format (GIF) was originally developed by CompuServe in 1987 as a means to transmit images with 256 colors (8-bit color). The format provides small file sizes and is ideal for web-based images. Because of this format’s limited color palette, GIF images are generally not suitable for photographs, which typically require high degrees of shading and color gradients. GIF images permit single-bit transparency, which allows colors to be mapped as clear (or transparent). This results in the background color of a web page showing through the image where the transparency has been mapped. JPEG Developed by the Joint Photographic Experts Group (JPEG), the JPEG (often written JPG) image format uses a lossy compression algorithm to allow 24-bit color depth with a small file size. Lossy compression means that each time the image is saved, the image loses quality and data but results in a smaller file size. The JPEG format is ideal for photographs because it is capable of displaying millions of colors. The ability to control the degree of compression applied to an image allows you to manipulate image quality and file size. PNG The Portable Network Graphics (PNG) format was produced as an open-source alternative to the patented GIF file format. PNGs support up to 64-bit color depth, allowing for up to 16 million colors. Because PNG is a relatively new format, some older browsers don’t support PNG files. Unlike JPGs, PNGs use lossless compression, which means that none of the image data is lost when the image is saved. PNG files also support alpha transparency, which allows for up to 256 levels of transparency. TIFF The Tagged Image File Format (TIFF) was the cross-platform format of choice before the PNG was introduced. The drawback with the TIFF format is that because of the many different varieties of TIFF, there is no single reader that can handle every version. In addition, no web browsers currently support the format. TIFF can use either lossy or lossless compression, and is able to handle device-specific color spaces (such as CMYK). Transparent bitmaps and opaque bitmaps Bitmap images that use either the GIF or PNG formats can have an extra byte (alpha channel) added to each pixel. This extra pixel byte represents the transparency value of the pixel. GIF images allow single-bit transparency, which means that you can specify a single color, from a 256-color palette, to be transparent. PNG images, on the other hand, can have up to 256 levels of transparency. This function is especially beneficial when images or text are required to blend into backgrounds. ActionScript 3.0 replicates this extra transparency pixel byte within the BitmapData class. Similar to the PNG transparency model, ActionScript offers up to 256 levels of transparency. Important concepts and terms The following list contains important terms that you will encounter when learning about bitmap graphics: Alpha The level of transparency (or more accurately, opacity) in a color or an image. The amount of alpha is often described as the alpha channel value. ARGB color A color scheme where each pixel’s color is a mixture of red, green, and blue color values, and its transparency is specified as an alpha value. Color channel Commonly, colors are represented as a mixture of a few basic colors—usually (for computer graphics) red, green, and blue. Each basic color is considered a color channel; the amount of color in each color channel, mixed together, determines the final color. Last updated 3/21/2011 234 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Color depth Also known as bit depth, this refers to the amount of computer memory that is devoted to each pixel, which in turn determines the number of possible colors that can be represented in the image. Pixel The smallest unit of information in a bitmap image—essentially a dot of color. Resolution The pixel dimensions of an image, which determines the level of fine-grained detail contained in the image. Resolution is often expressed in terms of width and height in number of pixels. RGB color A color scheme where each pixel’s color is represented as a mixture of red, green, and blue color values. The Bitmap and BitmapData classes Flash Player 9 and later, Adobe AIR 1.0 and later The main ActionScript 3.0 classes for working with bitmap images are the Bitmap class, which is used to display bitmap images on the screen, and the BitmapData class, which is used to access and manipulate the raw image data of a bitmap. More Help topics flash.display.Bitmap flash.display.BitmapData Understanding the Bitmap class Flash Player 9 and later, Adobe AIR 1.0 and later As a subclass of the DisplayObject class, the Bitmap class is the main ActionScript 3.0 class used for displaying bitmap images. These images may have been loaded via the flash.display.Loader class or created dynamically using the Bitmap() constructor. When loading an image from an external source, a Bitmap object can only use GIF, JPEG, or PNG format images. Once instantiated, the Bitmap instance can be considered a wrapper for a BitmapData object that needs to be rendered to the Stage. Because a Bitmap instance is a display object, all the characteristics and functionality of display objects can be used to manipulate a Bitmap instance as well. For more information about working with display objects, see “Display programming” on page 143. Pixel snapping and smoothing Flash Player 9 and later, Adobe AIR 1.0 and later In addition to the functionality common to all display objects, the Bitmap class provides some additional features that are specific to bitmap images. The pixelSnapping property of the Bitmap class determines whether or not a Bitmap object snaps to its nearest pixel. This property accepts one of three constants defined in the PixelSnapping class: ALWAYS, AUTO, and NEVER. The syntax for applying pixel snapping is as follows: myBitmap.pixelSnapping = PixelSnapping.ALWAYS; Often, when bitmap images are scaled, they become blurred and distorted. To help reduce this distortion, use the smoothing property of the BitmapData class. This Boolean property, when set to true, smooths, or anti-aliases, the pixels within the image when it is scaled. This gives the image a clearer and more natural appearance. Last updated 3/21/2011 235 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Understanding the BitmapData class Flash Player 9 and later, Adobe AIR 1.0 and later The BitmapData class, which is in the flash.display package, can be likened to a photographic snapshot of the pixels contained within a loaded or dynamically created bitmap image. This snapshot is represented by an array of pixel data within the object. The BitmapData class also contains a series of built-in methods that are useful for creation and manipulation of pixel data. To instantiate a BitmapData object, use the following code: var myBitmap:BitmapData = new BitmapData(width:Number, height:Number, transparent:Boolean, fillColor:uinit); The width and height parameters specify the size of the bitmap. In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData object is 8,191 pixels in width or height, and the total number of pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is 8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player 9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels in height and 2,880 in width. The transparent parameter specifies whether the bitmap data includes an alpha channel (true) or not (false). The fillColor parameter is a 32-bit color value that specifies the background color, as well as the transparency value (if it has been set to true). The following example creates a BitmapData object with an orange background that is 50 percent transparent: var myBitmap:BitmapData = new BitmapData(150, 150, true, 0x80FF3300); To render a newly created BitmapData object to the screen, assign it to or wrap it in a Bitmap instance. To do this, you can either pass the BitmapData object as a parameter of the Bitmap object’s constructor, or you can assign it to the bitmapData property of an existing Bitmap instance. You must also add the Bitmap instance to the display list by calling the addChild() or addChildAt() methods of the display object container that will contain the Bitmap instance. For more information on working with the display list, see “Adding display objects to the display list” on page 150. The following example creates a BitmapData object with a red fill, and displays it in a Bitmap instance: var myBitmapDataObject:BitmapData = new BitmapData(150, 150, false, 0xFF0000); var myImage:Bitmap = new Bitmap(myBitmapDataObject); addChild(myImage); Manipulating pixels Flash Player 9 and later, Adobe AIR 1.0 and later The BitmapData class contains a set of methods that allow you to manipulate pixel data values. Manipulating individual pixels Flash Player 9 and later, Adobe AIR 1.0 and later When changing the appearance of a bitmap image at a pixel level, you first need to get the color values of the pixels contained within the area you wish to manipulate. You use the getPixel() method to read these pixel values. Last updated 3/21/2011 236 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps The getPixel() method retrieves an RGB value from a set of x, y (pixel) coordinates that are passed as a parameter. If any of the pixels that you want to manipulate include transparency (alpha channel) information, you need to use the getPixel32() method. This method also retrieves an RGB value, but unlike with getPixel(), the value returned by getPixel32() contains additional data that represents the alpha channel (transparency) value of the selected pixel. Alternatively, if you simply want to change the color or transparency of a pixel contained within a bitmap, you can use the setPixel() or setPixel32() method. To set a pixel’s color, simply pass in the x, y coordinates and the color value to one of these methods. The following example uses setPixel() to draw a cross on a green BitmapData background. It then uses getPixel() to retrieve the color value from the pixel at the coordinate 50, 50 and traces the returned value. import flash.display.Bitmap; import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x009900); for (var i:uint = 0; i < 100; i++) { var red:uint = 0xFF0000; myBitmapData.setPixel(50, i, red); myBitmapData.setPixel(i, 50, red); } var myBitmapImage:Bitmap = new Bitmap(myBitmapData); addChild(myBitmapImage); var pixelValue:uint = myBitmapData.getPixel(50, 50); trace(pixelValue.toString(16)); If you want to read the value of a group of pixels, as opposed to a single pixel, use the getPixels() method. This method generates a byte array from a rectangular region of pixel data that is passed as a parameter. Each of the elements of the byte array (in other words, the pixel values) are unsigned integers—32-bit, unmultiplied pixel values. Conversely, to change (or set) the value of a group of pixels, use the setPixels() method. This method expects two parameters (rect and inputByteArray), which are combined to output a rectangular region (rect) of pixel data (inputByteArray). As data is read (and written) out of the inputByteArray, the ByteArray.readUnsignedInt() method is called for each of the pixels in the array. If, for some reason, the inputByteArray doesn't contain a full rectangle worth of pixel data, the method stops processing the image data at that point. It's important to remember that, for both getting and setting pixel data, the byte array expects 32-bit alpha, red, green, blue (ARGB) pixel values. The following example uses the getPixels() and setPixels() methods to copy a group of pixels from one BitmapData object to another: Last updated 3/21/2011 237 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps import import import import flash.display.Bitmap; flash.display.BitmapData; flash.utils.ByteArray; flash.geom.Rectangle; var bitmapDataObject1:BitmapData = new BitmapData(100, 100, false, 0x006666FF); var bitmapDataObject2:BitmapData = new BitmapData(100, 100, false, 0x00FF0000); var rect:Rectangle = new Rectangle(0, 0, 100, 100); var bytes:ByteArray = bitmapDataObject1.getPixels(rect); bytes.position = 0; bitmapDataObject2.setPixels(rect, bytes); var bitmapImage1:Bitmap = new Bitmap(bitmapDataObject1); addChild(bitmapImage1); var bitmapImage2:Bitmap = new Bitmap(bitmapDataObject2); addChild(bitmapImage2); bitmapImage2.x = 110; Pixel-level collision detection Flash Player 9 and later, Adobe AIR 1.0 and later The BitmapData.hitTest() method performs pixel-level collision detection between the bitmap data and another object or point. The BitmapData.hitTest() method accepts five parameters: • firstPoint (Point): This parameter refers to the pixel position of the upper-left corner of the first BitmapData upon which the hit test is being performed. • firstAlphaThreshold (uint): This parameter specifies the highest alpha channel value that is considered opaque for this hit test. • secondObject (Object): This parameter represents the area of impact. The secondObject object can be a Rectangle, Point, Bitmap, or BitmapData object. This object represents the hit area on which the collision detection is being performed. • secondBitmapDataPoint (Point): This optional parameter is used to define a pixel location in the second BitmapData object. This parameter is used only when the value of secondObject is a BitmapData object. The default is null. • secondAlphaThreshold (uint): This optional parameter represents the highest alpha channel value that is considered opaque in the second BitmapData object. The default value is 1. This parameter is only used when the value of secondObject is a BitmapData object and both BitmapData objects are transparent. When performing collision detection on opaque images, keep in mind that ActionScript treats the image as though it were a fully opaque rectangle (or bounding box). Alternatively, when performing pixel-level hit testing on images that are transparent, both of the images are required to be transparent. In addition to this, ActionScript uses the alpha threshold parameters to determine at what point the pixels change from being transparent to opaque. The following example creates three bitmap images and checks for pixel collision using two different collision points (one returns false, the other true): Last updated 3/21/2011 238 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.Point; var bmd1:BitmapData = new BitmapData(100, 100, false, 0x000000FF); var bmd2:BitmapData = new BitmapData(20, 20, false, 0x00FF3300); var bm1:Bitmap = new Bitmap(bmd1); this.addChild(bm1); // Create a red square. var redSquare1:Bitmap = new Bitmap(bmd2); this.addChild(redSquare1); redSquare1.x = 0; // Create a second red square. var redSquare2:Bitmap = new Bitmap(bmd2); this.addChild(redSquare2); redSquare2.x = 150; redSquare2.y = 150; // Define the var pt1:Point // Define the var pt2:Point // Define the var pt3:Point point = new point = new point = new at the top-left corner of the bitmap. Point(0, 0); at the center of redSquare1. Point(20, 20); at the center of redSquare2. Point(160, 160); trace(bmd1.hitTest(pt1, 0xFF, pt2)); // true trace(bmd1.hitTest(pt1, 0xFF, pt3)); // false Copying bitmap data Flash Player 9 and later, Adobe AIR 1.0 and later To copy bitmap data from one image to another, you can use several methods: clone(), copyPixels(), copyChannel(), and draw(). As its name suggests, the clone() method lets you clone, or sample, bitmap data from one BitmapData object to another. When called, the method returns a new BitmapData object that is an exact clone of the original instance it was copied from. The following example clones a copy of an orange (parent) square and places the clone beside the original parent square: Last updated 3/21/2011 239 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps import flash.display.Bitmap; import flash.display.BitmapData; var myParentSquareBitmap:BitmapData = new BitmapData(100, 100, false, 0x00ff3300); var myClonedChild:BitmapData = myParentSquareBitmap.clone(); var myParentSquareContainer:Bitmap = new Bitmap(myParentSquareBitmap); this.addChild(myParentSquareContainer); var myClonedChildContainer:Bitmap = new Bitmap(myClonedChild); this.addChild(myClonedChildContainer); myClonedChildContainer.x = 110; The copyPixels() method is a quick and easy way of copying pixels from one BitmapData object to another. The method takes a rectangular snapshot (defined by the sourceRect parameter) of the source image and copies it to another rectangular area (of equal size). The location of the newly “pasted” rectangle is defined within the destPoint parameter. The copyChannel() method samples a predefined color channel value (alpha, red, green, or blue) from a source BitmapData object and copies it into a channel of a destination BitmapData object. Calling this method does not affect the other channels in the destination BitmapData object. The draw() method draws, or renders, the graphical content from a source sprite, movie clip, or other display object on to a new bitmap. Using the matrix, colorTransform, blendMode, and destination clipRect parameters, you can modify the way in which the new bitmap is rendered. This method uses the vector renderer in Flash Player and AIR to generate the data. When you call draw(), you pass the source object (sprite, movie clip, or other display object) as the first parameter, as demonstrated here: myBitmap.draw(movieClip); If the source object has had any transformations (color, matrix, and so forth) applied to it after it was originally loaded, these transformations are not copied across to the new object. If you want to copy the transformations to the new bitmap, then you need to copy the value of the transform property from the original object to the transform property of the Bitmap object that uses the new BitmapData object. Making textures with noise functions Flash Player 9 and later, Adobe AIR 1.0 and later To modify the appearance of a bitmap, you can apply a noise effect to it, using either the noise() method or the perlinNoise() methods. A noise effect can be likened to the static that appears on an untuned television screen. To apply a noise effect to a a bitmap, use the noise() method. This method applies a random color value to pixels within a specified area of a bitmap image. This method accepts five parameters: • randomSeed (int): The random seed number that determines the pattern. Despite its name, this number actually creates the same results if the same number is passed. To get a true random result, use the Math.random() method to pass a random number for this parameter. Last updated 3/21/2011 240 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps • low (uint): This parameter refers to the lowest value to be generated for each pixel (0 to 255). The default value is 0. Setting this value lower results in a darker noise pattern, while setting it to a higher value results in a brighter pattern. • high (uint): This parameter refers to the highest value to be generated for each pixel (0 to 255). The default value is 255. Setting this value lower results in a darker noise pattern, while setting it to a higher value results in a brighter pattern. • channelOptions (uint): This parameter specifies to which color channel of the bitmap object the noise pattern will be applied. The number can be a combination of any of the four color channel ARGB values. The default value is 7. • grayScale (Boolean): When set to true, this parameter applies the randomSeed value to the bitmap pixels, effectively washing all color out of the image. The alpha channel is not affected by this parameter. The default value is false. The following example creates a bitmap image and applies a blue noise pattern to it: import flash.display.Bitmap; import flash.display.BitmapData; var myBitmap:BitmapData = new BitmapData(250, 250,false, 0xff000000); myBitmap.noise(500, 0, 255, BitmapDataChannel.BLUE,false); var image:Bitmap = new Bitmap(myBitmap); addChild(image); If you want to create a more organic-looking texture, use the perlinNoise() method. The perlinNoise() method produces realistic, organic textures that are ideal for smoke, clouds, water, fire, or even explosions. Because it is generated by an algorithm, the perlinNoise() method uses less memory than bitmap-based textures. However, it can still have an impact on processor usage, slowing down your content and causing the screen to be redrawn more slowly than the frame rate, especially on older computers. This is mainly due to the floating-point calculations that need to occur to process the perlin noise algorithms. The method accepts nine parameters (the first six are required): • baseX (Number): Determines the x (size) value of patterns created. • baseY (Number): Determines the y (size) value of the patterns created. • numOctaves (uint): Number of octaves or individual noise functions to combine to create this noise. Larger numbers of octaves create images with greater detail but also require more processing time. • randomSeed (int): The random seed number works exactly the same way as it does in the noise() function. To get a true random result, use the Math.random() method to pass a random number for this parameter. • stitch (Boolean): If set to true, this method attempts to stitch (or smooth) the transition edges of the image to create seamless textures for tiling as a bitmap fill. • fractalNoise (Boolean): This parameter relates to the edges of the gradients being generated by the method. If set to true, the method generates fractal noise that smooths the edges of the effect. If set to false, it generates turbulence. An image with turbulence has visible discontinuities in the gradient that can make it better approximate sharper visual effects, like flames and ocean waves. • channelOptions (uint): The channelOptions parameter works exactly the same way as it does in the noise() method. It specifies to which color channel (of the bitmap) the noise pattern is applied. The number can be a combination of any of the four color channel ARGB values. The default value is 7. • grayScale (Boolean): The grayScale parameter works exactly the same way as it does in the noise() method. If set to true, it applies the randomSeed value to the bitmap pixels, effectively washing all color out of the image. The default value is false. Last updated 3/21/2011 241 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps • offsets (Array): An array of points that correspond to x and y offsets for each octave. By manipulating the offset values, you can smoothly scroll the layers of the image. Each point in the offset array affects a specific octave noise function. The default value is null. The following example creates a 150 x 150 pixel BitmapData object that calls the perlinNoise() method to generate a green and blue cloud effect: import flash.display.Bitmap; import flash.display.BitmapData; var myBitmapDataObject:BitmapData = new BitmapData(150, 150, false, 0x00FF0000); var seed:Number = Math.floor(Math.random() * 100); var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE myBitmapDataObject.perlinNoise(100, 80, 6, seed, false, true, channels, false, null); var myBitmap:Bitmap = new Bitmap(myBitmapDataObject); addChild(myBitmap); Scrolling bitmaps Flash Player 9 and later, Adobe AIR 1.0 and later Imagine you have created a street mapping application where each time the user moves the map you are required to update the view (even if the map has been moved by just a few pixels). One way to create this functionality would be to re-render a new image containing the updated map view each time the user moves the map. Alternatively, you could create a large single image and use the scroll() method. The scroll() method copies an on-screen bitmap and then pastes it to a new offset location—specified by (x, y) parameters. If a portion of the bitmap happens to reside off-stage, this gives the effect that the image has shifted. When combined with a timer function (or an enterFrame event), you can make the image appear to be animating or scrolling. The following example takes the previous perlin noise example and generates a larger bitmap image (three-fourths of which is rendered off-stage). The scroll() method is then applied, along with an enterFrame event listener that offsets the image by one pixel in a diagonally downward direction. This method is called each time the frame is entered and as a result, the off screen portions of the image are rendered to the Stage as the image scrolls down. Last updated 3/21/2011 242 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps import flash.display.Bitmap; import flash.display.BitmapData; var myBitmapDataObject:BitmapData = new BitmapData(1000, 1000, false, 0x00FF0000); var seed:Number = Math.floor(Math.random() * 100); var channels:uint = BitmapDataChannel.GREEN | BitmapDataChannel.BLUE; myBitmapDataObject.perlinNoise(100, 80, 6, seed, false, true, channels, false, null); var myBitmap:Bitmap = new Bitmap(myBitmapDataObject); myBitmap.x = -750; myBitmap.y = -750; addChild(myBitmap); addEventListener(Event.ENTER_FRAME, scrollBitmap); function scrollBitmap(event:Event):void { myBitmapDataObject.scroll(1, 1); } Taking advantage of mipmapping Flash Player 9 and later, Adobe AIR 1.0 and later MIP maps (also known as mipmaps), are bitmaps grouped together and associated with a texture to increase runtime rendering quality and performance. Flash Player 9.115.0 and later versions and AIR implement this technology (the process is called mipmapping), by creating optimized versions of varying scale of each bitmap (starting at 50%). MIP maps are created for the following types of bitmaps: • a bitmap (JPEG, GIF, or PNG files) displayed using the ActionScript 3.0 Loader class • a bitmap in the library of a Flash Professional document • a BitmapData object • a bitmap displayed using the ActionScript 2.0 loadMovie() function MIP maps are not applied to filtered objects or bitmap-cached movie clips. However, MIP maps are applied if you have bitmap transformations within a filtered display object, even if the bitmap is within masked content. Mipmapping happens automatically, but you can follow a few guidelines to make sure your images take advantage of this optimization: • For video playback, set the smoothing property to true for the Video object (see the Video class). • For bitmaps, the smoothing property does not have to be set to true, but the quality improvements are more visible when bitmaps use smoothing. • Use bitmap sizes that are divisible by 4 or 8 for two-dimensional images (such as 640 x 128, which can be reduced as follows: 320 x 64 > 160 x 32 > 80 x 16 > 40 x 8 > 20 x 4 > 10 x 2 > 5 x 1) and 2^n for three-dimensional textures. MIP maps are generated from bitmaps that have a width and height that are 2^n (such as 256 x 256, 512 x 512, 1024 x 1024). Mipmapping does not occur for bitmap content with an odd width or height. Last updated 3/21/2011 243 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Bitmap example: Animated spinning moon Flash Player 9 and later, Adobe AIR 1.0 and later The Animated spinning moon example demonstrates techniques for working with Bitmap objects and bitmap image data (BitmapData objects). The example creates an animation of a spinning, spherical moon using a flat image of the moon’s surface as the raw image data. The following techniques are demonstrated: • Loading an external image and accessing its raw image data • Creating animation by repeatedly copying pixels from different parts of a source image • Creating a bitmap image by setting pixel values To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The Animated spinning moon application files can be found in the Samples/SpinningMoon folder. The application consists of the following files: File Description SpinningMoon.mxml The main application file in Flex (MXML) or Flash (FLA). or SpinningMoon.fla com/example/programmingas3/moon/MoonSphere.as Class that performs the functionality of loading, displaying, and animating the moon. moonMap.png Image file containing a photograph of the moon’s surface, which is loaded and used to create the animated, spinning moon. Loading an external image as bitmap data Flash Player 9 and later, Adobe AIR 1.0 and later The first main task this sample performs is loading an external image file, which is a photograph of the moon’s surface. The loading operation is handled by two methods in the MoonSphere class: the MoonSphere() constructor, where the loading process is initiated, and the imageLoadComplete() method, which is called when the external image is completely loaded. Loading an external image is similar to loading an external SWF; both use an instance of the flash.display.Loader class to perform the loading operation. The actual code in the MoonSphere() method that starts loading the image is as follows: var imageLoader:Loader = new Loader(); imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadComplete); imageLoader.load(new URLRequest("moonMap.png")); The first line declares the Loader instance named imageLoader. The third line actually starts the loading process by calling the Loader object’s load() method, passing a URLRequest instance representing the URL of the image to load. The second line sets up the event listener that will be triggered when the image has completely loaded. Notice that the addEventListener() method is not called on the Loader instance itself; instead, it’s called on the Loader object’s contentLoaderInfo property. The Loader instance itself doesn’t dispatch events relating to the content being loaded. Last updated 3/21/2011 244 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Its contentLoaderInfo property, however, contains a reference to the LoaderInfo object that’s associated with the content being loaded into the Loader object (the external image in this case). That LoaderInfo object does provide several events relating to the progress and completion of loading the external content, including the complete event (Event.COMPLETE) that will trigger a call to the imageLoadComplete() method when the image has completely loaded. While starting the external image loading is an important part of the process, it’s equally important to know what to do when it finishes loading. As shown in the code above, the imageLoadComplete() function is called when the image is loaded. That function does several things with the loaded image data, described subsequently. However, to use the image data, it needs to access that data. When a Loader object is used to load an external image, the loaded image becomes a Bitmap instance, which is attached as a child display object of the Loader object. In this case, the Loader instance is available to the event listener method as part of the event object that’s passed to the method as a parameter. The first lines of the imageLoadComplete() method are as follows: private function imageLoadComplete(event:Event):void { textureMap = event.target.content.bitmapData; ... } Notice that the event object parameter is named event, and it’s an instance of the Event class. Every instance of the Event class has a target property, which refers to the object triggering the event (in this case, the LoaderInfo instance on which the addEventListener() method was called, as described previously). The LoaderInfo object, in turn, has a content property that (once the loading process is complete) contains the Bitmap instance with the loaded bitmap image. If you want to display the image directly on the screen, you can attach this Bitmap instance (event.target.content) to a display object container. (You could also attach the Loader object to a display object container). However, in this sample, the loaded content is used as a source of raw image data rather than being displayed on the screen. Consequently, the first line of the imageLoadComplete() method reads the bitmapData property of the loaded Bitmap instance (event.target.content.bitmapData) and stores it in the instance variable named textureMap, which is used as a source of the image data to create the animation of the rotating moon. This is described next. Creating animation by copying pixels Flash Player 9 and later, Adobe AIR 1.0 and later A basic definition of animation is the illusion of motion, or change, created by changing an image over time. In this sample, the goal is to create the illusion of a spherical moon rotating around its vertical axis. However, for the purposes of the animation, you can ignore the spherical distortion aspect of the sample. Consider the actual image that’s loaded and used as the source of the moon image data: Last updated 3/21/2011 245 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps As you can see, the image is not one or several spheres; it’s a rectangular photograph of the surface of the moon. Because the photo was taken exactly at the moon’s equator, the parts of the image that are closer to the top and bottom of the image are stretched and distorted. To remove the distortion from the image and make it appear spherical, we will use a displacement map filter, as described later. However, because this source image is a rectangle, to create the illusion that the sphere is rotating, the code simply needs to slide the moon surface photo horizontally. Notice that the image actually contains two copies of the moon surface photograph next to each other. This image is the source image from which image data is copied repeatedly to create the appearance of motion. By having two copies of the image next to each other, a continuous, uninterrupted scrolling effect can more easily be created. Let’s walk through the process of the animation step-by-step to see how this works. The process actually involves two separate ActionScript objects. First, there is the loaded source image, which in the code is represented by the BitmapData instance named textureMap. As described previously, textureMap is populated with image data as soon as the external image loads, using this code: textureMap = event.target.content.bitmapData; The content of textureMap is the rectangle moon image. In addition, to create the animated rotation, the code uses a Bitmap instance named sphere, which is the actual display object that shows the moon image onscreen. Like textureMap, the sphere object is created and populated with its initial image data in the imageLoadComplete() method, using the following code: sphere = new Bitmap(); sphere.bitmapData = new BitmapData(textureMap.width / 2, textureMap.height); sphere.bitmapData.copyPixels(textureMap, new Rectangle(0, 0, sphere.width, sphere.height), new Point(0, 0)); As the code shows, sphere is instantiated. Its bitmapData property (the raw image data that is displayed by sphere) is created with the same height and half the width of textureMap. In other words, the content of sphere will be the size of one moon photo (since the textureMap image contains two moon photos side-by-side). Next the bitmapData property is filled with image data using its copyPixels() method. The parameters in the copyPixels() method call indicate several things: • The first parameter indicates that the image data is copied from textureMap. • The second parameter, a new Rectangle instance, specifies from which part of textureMap the image snapshot should be taken; in this case the snapshot is a rectangle starting from the top left corner of textureMap (indicated by the first two Rectangle() parameters: 0, 0) and the rectangle snapshot’s width and height match the width and height properties of sphere. • The third parameter, a new Point instance with x and y values of 0, defines the destination of the pixel data—in this case, the top-left corner (0, 0) of sphere.bitmapData. Represented visually, the code copies the pixels from textureMap outlined in the following image and pastes them onto sphere. In other words, the BitmapData content of sphere is the portion of textureMap highlighted here: Last updated 3/21/2011 246 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Remember, however, that this is just the initial state of sphere—the first image content that’s copied onto sphere. With the source image loaded and sphere created, the final task performed by the imageLoadComplete() method is to set up the animation. The animation is driven by a Timer instance named rotationTimer, which is created and started by the following code: var rotationTimer:Timer = new Timer(15); rotationTimer.addEventListener(TimerEvent.TIMER, rotateMoon); rotationTimer.start(); The code first creates the Timer instance named rotationTimer; the parameter passed to the Timer() constructor indicates that rotationTimer should trigger its timer event every 15 milliseconds. Next, the addEventListener() method is called, specifying that when the timer event (TimerEvent.TIMER) occurs, the method rotateMoon() is called. Finally, the timer is actually started by calling its start() method. Because of the way rotationTimer is defined, approximately every 15 milliseconds Flash Player calls the rotateMoon() method in the MoonSphere class, which is where the animation of the moon happens. The source code of the rotateMoon() method is as follows: private function rotateMoon(event:TimerEvent):void { sourceX += 1; if (sourceX > textureMap.width / 2) { sourceX = 0; } sphere.Data.copyPixels(textureMap, new Rectangle(sourceX, 0, sphere.width, sphere.height), new Point(0, 0)); event.updateAfterEvent(); } The code does three things: 1 The value of the variable sourceX (initially set to 0) increments by 1. Last updated 3/21/2011 247 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps sourceX += 1; As you’ll see, sourceX is used to determine the location in textureMap from which the pixels will be copied onto sphere, so this code has the effect of moving the rectangle one pixel to the right on textureMap. Going back to the visual representation, after several cycles of animation the source rectangle will have moved several pixels to the right, like this: After several more cycles, the rectangle will have moved even farther: This gradual, steady shift in the location from which the pixels are copied is the key to the animation. By slowly and continuously moving the source location to the right, the image that is displayed on the screen in sphere appears to continuously slide to the left. This is the reason why the source image (textureMap) needs to have two copies of the moon surface photo. Because the rectangle is continually moving to the right, most of the time it is not over one single moon photo but rather overlaps the two moon photos. 2 With the source rectangle slowly moving to the right, there is one problem. Eventually the rectangle will reach the right edge of textureMap and it will run out of moon photo pixels to copy onto sphere: The next lines of code address this issue: if (sourceX >= textureMap.width / 2) { sourceX = 0; } Last updated 3/21/2011 248 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps The code checks if sourceX (the left edge of the rectangle) has reached the middle of textureMap. If so, it resets sourceX back to 0, moving it back to the left edge of textureMap and starting the cycle over again: 3 With the appropriate sourceX value calculated, the final step in creating the animation is to actually copy the new source rectangle pixels onto sphere. The code that does this is very similar to the code that initially populated sphere (described previously); the only difference is that in this case, in the new Rectangle() constructor call, the left edge of the rectangle is placed at sourceX: sphere.bitmapData.copyPixels(textureMap, new Rectangle(sourceX, 0, sphere.width, sphere.height), new Point(0, 0)); Remember that this code is called repeatedly, every 15 milliseconds. As the source rectangle’s location is continuously shifted, and the pixels are copied onto sphere, the appearance on the screen is that the moon photo image represented by sphere continuously slides. In other words, the moon appears to rotate continuously. Creating the spherical appearance Flash Player 9 and later, Adobe AIR 1.0 and later The moon, of course, is a sphere and not a rectangle. Consequently, the sample needs to take the rectangular moon surface photo, as it continuously animates, and convert it into a sphere. This involves two separate steps: a mask is used to hide all the content except for a circular region of the moon surface photo, and a displacement map filter is used to distort the appearance of the moon photo to make it look three-dimensional. First, a circle-shaped mask is used to hide all the content of the MoonSphere object except for the sphere created by the filter. The following code creates the mask as a Shape instance and applies it as the mask of the MoonSphere instance: moonMask = new Shape(); moonMask.graphics.beginFill(0); moonMask.graphics.drawCircle(0, 0, radius); this.addChild(moonMask); this.mask = moonMask; Last updated 3/21/2011 249 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Note that since MoonSphere is a display object (it is based on the Sprite class), the mask can be applied directly to the MoonSphere instance using its inherited mask property. Simply hiding parts of the photo using a circle-shaped mask isn’t enough to create a realistic-looking rotating-sphere effect. Because of the way the photo of the moon’s surface was taken, its dimensions aren’t proportional; the portions of the image that are more toward the top or bottom of the image are more distorted and stretched compared to the portions in the equator. To distort the appearance of the moon photo to make it look three-dimensional, we’ll use a displacement map filter. A displacement map filter is a type of filter that is used to distort an image. In this case, the moon photo will be “distorted” to make it look more realistic, by squeezing the top and bottom of the image horizontally, while leaving the middle unchanged. Assuming the filter operates on a square-shaped portion of the photo, squeezing the top and bottom but not the middle will turn the square into a circle. A side effect of animating this distorted image is that the middle of the image seems to move farther in actual pixel distance than the areas close to the top and bottom, which creates the illusion that the circle is actually a three-dimensional object (a sphere). The following code is used to create the displacement map filter, named displaceFilter: var displaceFilter:DisplacementMapFilter; displaceFilter = new DisplacementMapFilter(fisheyeLens, new Point(radius, 0), BitmapDataChannel.RED, BitmapDataChannel.GREEN, radius, 0); The first parameter, fisheyeLens, is known as the map image; in this case it is a BitmapData object that is created programmatically. The creation of that image is described in “Creating a bitmap image by setting pixel values” on page 250. The other parameters describe the position in the filtered image at which the filter should be applied, which color channels will be used to control the displacement effect, and to what extent they will affect the displacement. Once the displacement map filter is created, it is applied to sphere, still within the imageLoadComplete() method: sphere.filters = [displaceFilter]; The final image, with mask and displacement map filter applied, looks like this: Last updated 3/21/2011 250 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps With every cycle of the rotating moon animation, the BitmapData content of sphere is overwritten by a new snapshot of the source image data. However, the filter does not need to be re-applied each time. This is because the filter is applied to the Bitmap instance (the display object) rather than to the bitmap data (the raw pixel information). Remember, the Bitmap instance is not the actual bitmap data; it is a display object that displays the bitmap data on the screen. To use an analogy, a Bitmap instance is like the slide projector that is used to display photographic slides on a screen, and a BitmapData object is like the actual photographic slide that can be presented through a slide projector. A filter can be applied directly to a BitmapData object, which would be comparable to drawing directly onto a photographic slide to alter the image. A filter can also be applied to any display object, including a Bitmap instance; this would be like placing a filter in front of the slide projector’s lens to distort the output shown on the screen (without altering the original slide at all). Because the raw bitmap data is accessible through a Bitmap instance’s bitmapData property, the filter could have been applied directly to the raw bitmap data. However, in this case, it makes sense to apply the filter to the Bitmap display object rather than to the bitmap data. For detailed information about using the displacement map filter in ActionScript, see “Filtering display objects” on page 255. Creating a bitmap image by setting pixel values Flash Player 9 and later, Adobe AIR 1.0 and later One important aspect of a displacement map filter is that it actually involves two images. One image, the source image, is the image that is actually altered by the filter. In this sample, the source image is the Bitmap instance named sphere. The other image used by the filter is known as the map image. The map image is not actually displayed on the screen. Instead, the color of each of its pixels is used as an input to the displacement function—the color of the pixel at a certain x, y coordinate in the map image determines how much displacement (physical shift in position) is applied to the pixel at that x, y coordinate in the source image. Consequently, to use the displacement map filter to create a sphere effect, the sample needs the appropriate map image—one that has a gray background and a circle that’s filled with a gradient of a single color (red) going horizontally from dark to light, as shown here: Because only one map image and filter are used in this sample, the map image is only created once, in the imageLoadComplete() method (in other words, when the external image finishes loading). The map image, named fisheyeLens, is created by calling the MoonSphere class’s createFisheyeMap() method: var fisheyeLens:BitmapData = createFisheyeMap(radius); Inside the createFisheyeMap() method, the map image is actually drawn one pixel at a time using the BitmapData class’s setPixel() method. The complete code for the createFisheyeMap() method is listed here, followed by a step-by-step discussion of how it works: Last updated 3/21/2011 251 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps private function createFisheyeMap(radius:int):BitmapData { var diameter:int = 2 * radius; var result:BitmapData = new BitmapData(diameter, diameter, false, 0x808080); // Loop through the pixels in the image one by one for (var i:int = 0; i < diameter; i++) { for (var j:int = 0; j < diameter; j++) { // Calculate the x and y distances of this pixel from // the center of the circle (as a percentage of the radius). var pctX:Number = (i - radius) / radius; var pctY:Number = (j - radius) / radius; // Calculate the linear distance of this pixel from // the center of the circle (as a percentage of the radius). var pctDistance:Number = Math.sqrt(pctX * pctX + pctY * pctY); // If the current pixel is inside the circle, // set its color. if (pctDistance < 1) { // Calculate the appropriate color depending on the // distance of this pixel from the center of the circle. var red:int; var green:int; var blue:int; var rgb:uint; red = 128 * (1 + 0.75 * pctX * pctX * pctX / (1 - pctY * pctY)); green = 0; blue = 0; rgb = (red << 16 | green << 8 | blue); // Set the pixel to the calculated color. result.setPixel(i, j, rgb); } } } return result; } First, when the method is called it receives a parameter, radius, indicating the radius of the circle-shaped image to create. Next, the code creates the BitmapData object on which the circle will be drawn. That object, named result, is eventually passed back as the return value of the method. As shown in the following code snippet, the result BitmapData instance is created with a width and height as big as the diameter of the circle, without transparency (false for the third parameter), and pre-filled with the color 0x808080 (middle gray): var result:BitmapData = new BitmapData(diameter, diameter, false, 0x808080); Last updated 3/21/2011 252 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps Next, the code uses two loops to iterate over each pixel of the image. The outer loop goes through each column of the image from left to right (using the variable i to represent the horizontal position of the pixel currently being manipulated), while the inner loop goes through each pixel of the current column from top to bottom (with the variable j representing the vertical position of the current pixel). The code for the loops (with the inner loop’s contents omitted) is shown here: for (var i:int = 0; i < diameter; i++) { for (var j:int = 0; j < diameter; j++) { ... } } As the loops cycle through the pixels one by one, at each pixel a value (the color value of that pixel in the map image) is calculated. This process involves four steps: 1 The code calculates the distance of the current pixel from the center of the circle along the x axis (i - radius). That value is divided by the radius to make it a percentage of the radius rather than an absolute distance ((i radius) / radius). That percentage value is stored in a variable named pctX, and the equivalent value for the y axis is calculated and stored in the variable pctY, as shown in this code: var pctX:Number = (i - radius) / radius; var pctY:Number = (j - radius) / radius; 2 Using a standard trigonometric formula, the Pythagorean theorem, the linear distance between the center of the circle and the current point is calculated from pctX and pctY. That value is stored in a variable named pctDistance, as shown here: var pctDistance:Number = Math.sqrt(pctX * pctX + pctY * pctY); 3 Next, the code checks whether the distance percentage is less than 1 (meaning 100% of the radius, or in other words, if the pixel being considered is within the radius of the circle). If the pixel falls inside the circle, it is assigned a calculated color value (omitted here, but described in step 4); if not, nothing further happens with that pixel so its color is left as the default middle gray: if (pctDistance < 1) { ... } 4 For those pixels that fall inside the circle, a color value is calculated for the pixel. The final color will be a shade of red ranging from black (0% red) at the left edge of the circle to bright (100%) red at the right edge of the circle. The color value is initially calculated in three parts (red, green, and blue), as shown here: red = 128 * (1 + 0.75 * pctX * pctX * pctX / (1 - pctY * pctY)); green = 0; blue = 0; Notice that only the red portion of the color (the variable red) actually has a value. The green and blue values (the variables green and blue) are shown here for clarity, but could be omitted. Since the purpose of this method is to create a circle that contains a red gradient, no green or blue values are needed. Once the three individual color values are determined, they are combined into a single integer color value using a standard bit-shifting algorithm, shown in this code: rgb = (red << 16 | green << 8 | blue); Finally, with the color value calculated, that value is actually assigned to the current pixel using the setPixel() method of the result BitmapData object, shown here: Last updated 3/21/2011 253 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps result.setPixel(i, j, rgb); Asynchronous decoding of bitmap images Flash Player 10.2 and later, Adobe AIR 2.6 and later When you work with bitmap images, you can asynchronously decode and load the bitmap images to improve your application’s perceived performance. Decoding a bitmap image asynchronously can take the same time as decoding the image synchronously in many cases. However, the bitmap image gets decoded in a separate thread before the associated Loader object sends the COMPLETE event. Hence, you can asynchronously decode larger images after loading them. The ImageDecodingPolicy class in the flash.system package, allows you to specify the bitmap loading scheme. The default loading scheme is synchronous. Bitmap Decoding Policy Bitmap Loading Scheme Description ImageDecodingPolicy.ON_DEMAND Synchronous Loaded images are decoded when the image data is accessed. Use this policy to decode smaller images. You can also use this policy when your application does not rely on complex effects and transitions. ImageDecodingPolicy.ON_LOAD Asynchronous Loaded images are decoded on load, before the COMPLETE event is dispatched. Ideal for larger images (greater than 10 MP). When you are developing AIR-based mobile applications with page transitions, use this bitmap loading policy to improve your application’s perceived performance. Note: If the file being loaded is a bitmap image and the decoding policy used is ON_LOAD, the image is decoded asynchronously before the COMPLETE event is dispatched. The following code shows the usage of the ImageDecodingPolicy class: var loaderContext:LoaderContext = new LoaderContext(); loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD var loader:Loader = new Loader(); loader.load(new URLRequest("http://www.adobe.com/myimage.png"), loaderContext); You can still use ON_DEMAND decoding with Loader.load() and Loader.loadBytes() methods. However, all the other methods that take a LoaderContext object as an argument, ignore any ImageDecodingPolicy value passed. The following example shows the difference in decoding a bitmap image synchronously and asynchronously: Last updated 3/21/2011 254 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with bitmaps package { import import import import import import flash.display.Loader; flash.display.Sprite; flash.events.Event; flash.net.URLRequest; flash.system.ImageDecodingPolicy; flash.system.LoaderContext; public class AsyncTest extends Sprite { private var loaderContext:LoaderContext; private var loader:Loader; private var urlRequest:URLRequest; public function AsyncTest() { //Load the image synchronously loaderContext = new LoaderContext(); //Default behavior. loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_DEMAND; loader = new Loader(); loadImageSync(); //Load the image asynchronously loaderContext = new LoaderContext(); loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD; loader = new Loader(); loadImageASync(); } private function loadImageASync():void{ trace("Loading image asynchronously..."); urlRequest = new URLRequest("http://www.adobe.com/myimage.png"); urlRequest.useCache = false; loader.load(urlRequest, loaderContext); loader.contentLoaderInfo.addEventListener (Event.COMPLETE, onAsyncLoadComplete); } private function onAsyncLoadComplete(event:Event):void{ trace("Async. Image Load Complete"); } private function loadImageSync():void{ trace("Loading image synchronously..."); urlRequest = new URLRequest("http://www.adobe.com/myimage.png"); urlRequest.useCache = false; loader.load(urlRequest, loaderContext); loader.contentLoaderInfo.addEventListener (Event.COMPLETE, onSyncLoadComplete); } private function onSyncLoadComplete(event:Event):void{ trace("Sync. Image Load Complete"); } } } Last updated 3/21/2011 255 Chapter 13: Filtering display objects Flash Player 9 and later, Adobe AIR 1.0 and later Historically, the application of filter effects to bitmap images has been the domain of specialized image-editing software such as Adobe Photoshop® and Adobe Fireworks®. ActionScript 3.0 includes the flash.filters package, which contains a series of bitmap effect filter classes. These effects allow developers to programmatically apply filters to bitmaps and display objects and achieve many of the same effects that are available in graphics manipulation applications. Basics of filtering display objects Flash Player 9 and later, Adobe AIR 1.0 and later One of the ways to add polish to an application is to add simple graphic effects. You can add a drop shadow behind a photo to create the illusion of 3-d, or a glow around a button to show that it is active. ActionScript 3.0 includes ten filters that you can apply to any display object or to a BitmapData instance. The built-in filters range from basic, such as the drop shadow and glow filters, to complex, such as the displacement map filter and the convolution filter. Note: In addition to the built-in filters, you can also program custom filters and effects using Pixel Bender. See “Working with Pixel Bender shaders” on page 288. Important concepts and terms The following reference list contains important terms that you might encounter when creating filters: Bevel An edge created by lightening pixels on two sides and darkening pixels on the opposite two sides. This effect creates the appearance of a three-dimensional border. The effect is commonly used for raised or indented buttons and similar graphics. Convolution Distorting pixels in an image by combining each pixel’s value with the values of some or all of its neighboring pixels, using various ratios. Displacement Shifting or moving pixels in an image to a new position. Matrix A grid of numbers used to perform certain mathematical calculations by applying the numbers in the grid to various values, then combining the results. More Help topics flash.filters package flash.display.DisplayObject.filters flash.display.BitmapData.applyFilter() Last updated 3/21/2011 256 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Creating and applying filters Flash Player 9 and later, Adobe AIR 1.0 and later Filters allow you to apply a range of effects to bitmap and display objects, ranging from drop shadows to bevels and blurs. Each filter is defined as a class, so applying filters involves creating instances of filter objects, which is no different from constructing any other object. Once you’ve created an instance of a filter object, it can easily be applied to a display object by using the object’s filters property, or in the case of a BitmapData object, by using the applyFilter() method. Creating a filter Flash Player 9 and later, Adobe AIR 1.0 and later To create a filter object, simply call the constructor method of your selected filter class. For example, to create a DropShadowFilter object, use the following code: import flash.filters.DropShadowFilter; var myFilter:DropShadowFilter = new DropShadowFilter(); Although not shown here, the DropShadowFilter() constructor (like all the filter classes’ constructors) accepts several optional parameters that can be used to customize the appearance of the filter effect. Applying a filter Flash Player 9 and later, Adobe AIR 1.0 and later Once you've constructed a filter object, you can apply it to a display object or a BitmapData object; how you apply the filter depends on the object to which you’re applying it. Applying a filter to a display object When you apply filter effects to a display object, you apply them through the filters property. The filters property of a display object is an Array instance, whose elements are the filter objects applied to the display object. To apply a single filter to a display object, create the filter instance, add it to an Array instance, and assign that Array object to the display object’s filters property: Last updated 3/21/2011 257 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import flash.display.Bitmap; import flash.display.BitmapData; import flash.filters.DropShadowFilter; // Create a bitmapData object and render it to screen var myBitmapData:BitmapData = new BitmapData(100,100,false,0xFFFF3300); var myDisplayObject:Bitmap = new Bitmap(myBitmapData); addChild(myDisplayObject); // Create a DropShadowFilter instance. var dropShadow:DropShadowFilter = new DropShadowFilter(); // Create the filters array, adding the filter to the array by passing it as // a parameter to the Array() constructor. var filtersArray:Array = new Array(dropShadow); // Assign the filters array to the display object to apply the filter. myDisplayObject.filters = filtersArray; If you want to assign multiple filters to the object, simply add all the filters to the Array instance before assigning it to the filters property. You can add multiple objects to an Array by passing them as parameters to its constructor. For example, this code applies a bevel filter and a glow filter to the previously created display object: import flash.filters.BevelFilter; import flash.filters.GlowFilter; // Create the filters and add them to an array. var bevel:BevelFilter = new BevelFilter(); var glow:GlowFilter = new GlowFilter(); var filtersArray:Array = new Array(bevel, glow); // Assign the filters array to the display object to apply the filter. myDisplayObject.filters = filtersArray; When you’re creating the array containing the filters, you can create it using the new Array() constructor (as shown in the previous examples) or you can use Array literal syntax, wrapping the filters in square brackets ([]). For instance, this line of code: var filters:Array = new Array(dropShadow, blur); does the same thing as this line of code: var filters:Array = [dropShadow, blur]; If you apply multiple filters to display objects, they are applied in a cumulative, sequential manner. For example, if a filters array has two elements, a bevel filter added first and a drop shadow filter added second, the drop shadow filter is applied to both the bevel filter and the display object. This is because of the drop shadow filter’s second position in the filters array. If you want to apply filters in a noncumulative manner, apply each filter to a new copy of the display object. If you’re only assigning one or a few filters to a display object, you can create the filter instance and assign it to the object in a single statement. For example, the following line of code applies a blur filter to a display object called myDisplayObject: myDisplayObject.filters = [new BlurFilter()]; The previous code creates an Array instance using Array literal syntax (square braces), creates a BlurFilter instance as an element in the Array, and assigns that Array to the filters property of the display object named myDisplayObject. Last updated 3/21/2011 258 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Removing filters from a display object Removing all filters from a display object is as simple as assigning a null value to the filters property: myDisplayObject.filters = null; If you’ve applied multiple filters to an object and want to remove only one of the filters, you must go through several steps to change the filters property array. For more information, see “Potential issues for working with filters” on page 258. Applying a filter to a BitmapData object Applying a filter to a BitmapData object requires the use of the BitmapData object’s applyFilter() method: var rect:Rectangle = new Rectangle(); var origin:Point = new Point(); myBitmapData.applyFilter(sourceBitmapData, rect, origin, new BlurFilter()); The applyFilter() method applies a filter to a source BitmapData object, producing a new, filtered image. This method does not modify the original source image; instead, the result of the filter being applied to the source image is stored in the BitmapData instance on which the applyFilter() method is called. How filters work Flash Player 9 and later, Adobe AIR 1.0 and later Display object filtering works by caching a copy of the original object as a transparent bitmap. Once a filter has been applied to a display object, the runtime caches the object as a bitmap for as long as the object has a valid filter list. This source bitmap is then used as the original image for all subsequently applied filter effects. Each display object usually contains two bitmaps: one with the original unfiltered source display object and another for the final image after filtering. The final image is used when rendering. As long as the display object does not change, the final image does not need updating. Potential issues for working with filters Flash Player 9 and later, Adobe AIR 1.0 and later There are several potential sources of confusion or trouble to keep in mind when you’re working with filters. Filters and bitmap caching To apply a filter to a display object, bitmap caching must be enabled for that object. When you apply a filter to a display object whose cacheAsBitmap property is set to false, the object’s cacheAsBitmap property is automatically set to true. If you later remove all the filters from the display object, the cacheAsBitmap property is reset to the last value it was set to. Last updated 3/21/2011 259 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Changing filters at run time If a display object already has one or more filters applied to it, you can’t change the set of filters by adding additional filters to or removing filters from the filters property array. Instead, to add to or change the set of filters being applied, you must make your changes to a separate array, then assign that array to the filters property of the display object for the filters to be applied to the object. The simplest way to do this is to read the filters property array into an Array variable and make your modifications to this temporary array. You then reassign this array back to the filters property of the display object. In more complex cases, you might need to keep a separate master array of filters. You make any changes to that master filter array, and reassign the master array to the display object’s filters property after each change. Adding an additional filter The following code demonstrates the process of adding an additional filter to a display object that already has one or more filters applied to it. Initially, a glow filter is applied to the display object named myDisplayObject; later, when the display object is clicked, the addFilters() function is called. In this function, two additional filters are applied to myDisplayObject: import flash.events.MouseEvent; import flash.filters.*; myDisplayObject.filters = [new GlowFilter()]; function addFilters(event:MouseEvent):void { // Make a copy of the filters array. var filtersCopy:Array = myDisplayObject.filters; // Make desired changes to the filters (in this case, adding filters). filtersCopy.push(new BlurFilter()); filtersCopy.push(new DropShadowFilter()); // Apply the changes by reassigning the array to the filters property. myDisplayObject.filters = filtersCopy; } myDisplayObject.addEventListener(MouseEvent.CLICK, addFilters); Removing one filter from a set of filters If a display object has multiple filters applied to it, and you want to remove one of the filters while the other filters continue to be applied to the object, you copy the filters into a temporary array, remove the unwanted filter from that array, and reassign the temporary array to the display object’s filters property. Several ways to remove one or more elements from any array are described in “Retrieving values and removing array elements” on page 31. The most straightforward situation is to remove the top-most filter on the object (the last filter applied to the object). You use the Array class’s pop() method to remove the filter from the array: Last updated 3/21/2011 260 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects // Example of removing the top-most filter from a display object // named "filteredObject". var tempFilters:Array = filteredObject.filters; // Remove the last element from the Array (the top-most filter). tempFilters.pop(); // Apply the new set of filters to the display object. filteredObject.filters = tempFilters; Similarly, to remove the bottom-most filter (the first one applied to the object) you use the same code, substituting the Array class’s shift() method in place of the pop() method. To remove a filter from the middle of an array of filters (assuming that the array has more than two filters) you can use the splice() method. You must know the index (the position in the array) of the filter you want to remove. For example, the following code removes the second filter (the filter at index 1) from a display object: // Example of removing a filter from the middle of a stack of filters // applied to a display object named "filteredObject". var tempFilters:Array = filteredObject.filters; // Remove the second filter from the array. It's the item at index 1 // because Array indexes start from 0. // The first "1" indicates the index of the filter to remove; the // second "1" indicates how many elements to remove. tempFilters.splice(1, 1); // Apply the new set of filters to the display object. filteredObject.filters = tempFilters; Determining a filter’s index You need to know which filter to remove from the array, so that you know the index of the filter. You must either know (by virtue of the way the application is designed), or calculate the index of the filter to remove. The best approach is to design your application so that the filter you want to remove is always in the same position in the set of filters. For example, if you have a single display object with a convolution filter and a drop-shadow filter applied to it (in that order), and you want to remove the drop-shadow filter but keep the convolution filter, the filter is in a known position (the top-most filter) so that you can know ahead of time which Array method to use (in this case Array.pop() to remove the drop-shadow filter). If the filter you want to remove is always a certain type, but not necessarily always in the same position in the set of filters, you can check the data type of each filter in the array to determine which one to remove. For example, the following code determines which of a set of filters is a glow filter, and removes that filter from the set. Last updated 3/21/2011 261 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects // Example of removing a glow filter from a set of filters, where the //filter you want to remove is the only GlowFilter instance applied // to the filtered object. var tempFilters:Array = filteredObject.filters; // Loop through the filters to find the index of the GlowFilter instance. var glowIndex:int; var numFilters:int = tempFilters.length; for (var i:int = 0; i < numFilters; i++) { if (tempFilters[i] is GlowFilter) { glowIndex = i; break; } } // Remove the glow filter from the array. tempFilters.splice(glowIndex, 1); // Apply the new set of filters to the display object. filteredObject.filters = tempFilters; In a more complex case, such as if the filter to remove is selected at runtime, the best approach is to keep a separate, persistent copy of the filter array that serves as the master list of filters. Any time you make a change to the set of filters, change the master list then apply that filter array as the filters property of the display object. For example, in the following code listing, multiple convolution filters are applied to a display object to create different visual effects, and at a later point in the application one of those filters is removed while the others are retained. In this case, the code keeps a master copy of the filters array, as well as a reference to the filter to remove. Finding and removing the specific filter is similar to the preceding approach, except that instead of making a temporary copy of the filters array, the master copy is manipulated and then applied to the display object. Last updated 3/21/2011 262 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects // // // // Example of removing a filter from a set of filters, where there may be more than one of that type of filter applied to the filtered object, and you only want to remove one. // A master list of filters is stored in a separate, // persistent Array variable. var masterFilterList:Array; // At some point, you store a reference to the filter you // want to remove. var filterToRemove:ConvolutionFilter; // ... assume the filters have been added to masterFilterList, // which is then assigned as the filteredObject.filters: filteredObject.filters = masterFilterList; // ... later, when it's time to remove the filter, this code gets called: // Loop through the filters to find the index of masterFilterList. var removeIndex:int = -1; var numFilters:int = masterFilterList.length; for (var i:int = 0; i < numFilters; i++) { if (masterFilterList[i] == filterToRemove) { removeIndex = i; break; } } if (removeIndex >= 0) { // Remove the filter from the array. masterFilterList.splice(removeIndex, 1); // Apply the new set of filters to the display object. filteredObject.filters = masterFilterList; } In this approach (when you’re comparing a stored filter reference to the items in the filters array to determine which filter to remove), you must keep a separate copy of the filters array—the code does not work if you compare the stored filter reference to the elements in a temporary array copied from the display object’s filters property. This is because internally, when you assign an array to the filters property, the runtime makes a copy of each filter object in the array. Those copies (rather than the original objects) are applied to the display object, and when you read the filters property into a temporary array, the temporary array contains references to the copied filter objects rather than references to the original filter objects. Consequently, if in the preceding example you try to determine the index of filterToRemove by comparing it to the filters in a temporary filters array, no match is found. Filters and object transformations No filtered region—a drop shadow, for example—outside of a display object’s bounding box rectangle is considered to be part of the surface for the purposes of hit detection (determining if an instance overlaps or intersects with another instance). Because the DisplayObject class’s hit detection methods are vector-based, you cannot perform a hit detection on the bitmap result. For example, if you apply a bevel filter to a button instance, hit detection is not available on the beveled portion of the instance. Last updated 3/21/2011 263 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Scaling, rotating, and skewing are not supported by filters; if the filtered display object itself is scaled (if scaleX and scaleY are not 100%), the filter effect does not scale with the instance. This means that the original shape of the instance rotates, scales, or skews; however, the filter does not rotate, scale, or skew with the instance. You can animate an instance with a filter to create realistic effects, or nest instances and use the BitmapData class to animate filters to achieve this effect. Filters and Bitmap objects When you apply any filter to a BitmapData object, the cacheAsBitmap property is automatically set to true. In this way, the filter is actually applied to the copy of the object rather than to the original. This copy is then placed on the main display (over the original object) as close as possible to the nearest pixel. If the bounds of the original bitmap change, the filtered copy bitmap is recreated from the original, rather than being stretched or distorted. If you clear all filters for a display object, the cacheAsBitmap property is reset to what it was before the filter was applied. Available display filters Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript 3.0 includes ten filter classes that you can apply to display objects and BitmapData objects: • Bevel filter (BevelFilter class) • Blur filter (BlurFilter class) • Drop shadow filter (DropShadowFilter class) • Glow filter (GlowFilter class) • Gradient bevel filter (GradientBevelFilter class) • Gradient glow filter (GradientGlowFilter class) • Color matrix filter (ColorMatrixFilter class) • Convolution filter (ConvolutionFilter class) • Displacement map filter (DisplacementMapFilter class) • Shader filter (ShaderFilter class) The first six filters are simple filters that can be used to create one specific effect, with some customization of the effect available. Those six filters can be applied using ActionScript, and can also be applied to objects in Flash Professional using the Filters panel. Consequently, even if you’re applying filters using ActionScript, if you have Flash Professional you can use the visual interface to quickly try out different filters and settings to figure out how to create a desired effect. The final four filters are available in ActionScript only. Those filters, the color matrix filter, convolution filter, displacement map filter, and shader filter, are much more flexible in the types of effects that they can be used to create. Rather than being optimized for a single effect, they provide power and flexibility. For example, by selecting different values for its matrix, the convolution filter can be used to create effects such as blurring, embossing, sharpening, finding color edges, transformations, and more. Last updated 3/21/2011 264 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Each of the filters, whether simple or complex, can be customized using their properties. Generally, you have two choices for setting filter properties. All the filters let you set the properties by passing parameter values to the filter object’s constructor. Alternatively, whether or not you set the filter properties by passing parameters, you can adjust the filters later by setting values for the filter object’s properties. Most of the example code listings set the properties directlyto make the example easier to follow. Nevertheless, you could usually achieve the same result in fewer lines of code by passing the values as parameters in the filter object’s constructor. For more details on the specifics of each filter, its properties and its constructor parameters, see the listings for the flash.filters package in the ActionScript 3.0 Reference for the Adobe Flash Platform. Bevel filter Flash Player 9 and later, Adobe AIR 1.0 and later The BevelFilter class allows you to add a 3D beveled edge to the filtered object. This filter makes the hard corners or edges of your object look like they have been chiseled, or beveled, away. The BevelFilter class properties allow you to customize the appearance of the bevel. You can set highlight and shadow colors, bevel edge blurs, bevel angles, and bevel edge placement; you can even create a knockout effect. The following example loads an external image and applies a bevel filter to it. import import import import import flash.display.*; flash.filters.BevelFilter; flash.filters.BitmapFilterQuality; flash.filters.BitmapFilterType; flash.net.URLRequest; // Load an image onto the Stage. var imageLoader:Loader = new Loader(); var url:String = "http://www.helpexamples.com/flash/images/image3.jpg"; var urlReq:URLRequest = new URLRequest(url); imageLoader.load(urlReq); addChild(imageLoader); // Create the bevel filter and set filter properties. var bevel:BevelFilter = new BevelFilter(); bevel.distance = 5; bevel.angle = 45; bevel.highlightColor = 0xFFFF00; bevel.highlightAlpha = 0.8; bevel.shadowColor = 0x666666; bevel.shadowAlpha = 0.8; bevel.blurX = 5; bevel.blurY = 5; bevel.strength = 5; bevel.quality = BitmapFilterQuality.HIGH; bevel.type = BitmapFilterType.INNER; bevel.knockout = false; // Apply filter to the image. imageLoader.filters = [bevel]; Last updated 3/21/2011 265 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Blur filter Flash Player 9 and later, Adobe AIR 1.0 and later The BlurFilter class smears, or blurs, a display object and its contents. Blur effects are useful for giving the impression that an object is out of focus or for simulating fast movement, as in a motion blur. By setting the quality property of the blur filter too low, you can simulate a softly out-of-focus lens effect. Setting the quality property to high results in a smooth blur effect similar to a Gaussian blur. The following example creates a circle object using the drawCircle() method of the Graphics class and applies a blur filter to it: import flash.display.Sprite; import flash.filters.BitmapFilterQuality; import flash.filters.BlurFilter; // Draw a circle. var redDotCutout:Sprite = new Sprite(); redDotCutout.graphics.lineStyle(); redDotCutout.graphics.beginFill(0xFF0000); redDotCutout.graphics.drawCircle(145, 90, 25); redDotCutout.graphics.endFill(); // Add the circle to the display list. addChild(redDotCutout); // Apply the blur filter to the rectangle. var blur:BlurFilter = new BlurFilter(); blur.blurX = 10; blur.blurY = 10; blur.quality = BitmapFilterQuality.MEDIUM; redDotCutout.filters = [blur]; Drop shadow filter Flash Player 9 and later, Adobe AIR 1.0 and later Drop shadows give the impression that there is a separate light source situated above a target object. The position and intensity of this light source can be modified to produce a variety of different drop shadow effects. The DropShadowFilter class uses an algorithm that is similar to the blur filter’s algorithm. The main difference is that the drop shadow filter has a few more properties that you can modify to simulate different light-source attributes (such as alpha, color, offset and brightness). The drop shadow filter also allows you to apply custom transformation options on the style of the drop shadow, including inner or outer shadow and knockout (also known as cutout) mode. The following code creates a square box sprite and applies a drop shadow filter to it: Last updated 3/21/2011 266 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import flash.display.Sprite; import flash.filters.DropShadowFilter; // Draw a box. var boxShadow:Sprite = new Sprite(); boxShadow.graphics.lineStyle(1); boxShadow.graphics.beginFill(0xFF3300); boxShadow.graphics.drawRect(0, 0, 100, 100); boxShadow.graphics.endFill(); addChild(boxShadow); // Apply the drop shadow filter to the box. var shadow:DropShadowFilter = new DropShadowFilter(); shadow.distance = 10; shadow.angle = 25; // You can also set other properties, such as the shadow color, // alpha, amount of blur, strength, quality, and options for // inner shadows and knockout effects. boxShadow.filters = [shadow]; Glow filter Flash Player 9 and later, Adobe AIR 1.0 and later The GlowFilter class applies a lighting effect to display objects, making it appear that a light is being shined up from underneath the object to create a soft glow. Similar to the drop shadow filter, the glow filter includes properties to modify the distance, angle, and color of the light source to produce varying effects. The GlowFilter also has several options for modifying the style of the glow, including inner or outer glow and knockout mode. The following code creates a cross using the Sprite class and applies a glow filter to it: Last updated 3/21/2011 267 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import flash.display.Sprite; import flash.filters.BitmapFilterQuality; import flash.filters.GlowFilter; // Create a cross graphic. var crossGraphic:Sprite = new Sprite(); crossGraphic.graphics.lineStyle(); crossGraphic.graphics.beginFill(0xCCCC00); crossGraphic.graphics.drawRect(60, 90, 100, 20); crossGraphic.graphics.drawRect(100, 50, 20, 100); crossGraphic.graphics.endFill(); addChild(crossGraphic); // Apply the glow filter to the cross shape. var glow:GlowFilter = new GlowFilter(); glow.color = 0x009922; glow.alpha = 1; glow.blurX = 25; glow.blurY = 25; glow.quality = BitmapFilterQuality.MEDIUM; crossGraphic.filters = [glow]; Gradient bevel filter Flash Player 9 and later, Adobe AIR 1.0 and later The GradientBevelFilter class lets you apply an enhanced bevel effect to display objects or BitmapData objects. Using a gradient color on the bevel greatly improves the spatial depth of the bevel, giving edges a more realistic, 3D appearance. The following code creates a rectangle object using the drawRect() method of the Shape class and applies a gradient bevel filter to it. Last updated 3/21/2011 268 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import flash.display.Shape; import flash.filters.BitmapFilterQuality; import flash.filters.GradientBevelFilter; // Draw a rectangle. var box:Shape = new Shape(); box.graphics.lineStyle(); box.graphics.beginFill(0xFEFE78); box.graphics.drawRect(100, 50, 90, 200); box.graphics.endFill(); // Apply a gradient bevel to the rectangle. var gradientBevel:GradientBevelFilter = new GradientBevelFilter(); gradientBevel.distance = 8; gradientBevel.angle = 225; // opposite of 45 degrees gradientBevel.colors = [0xFFFFCC, 0xFEFE78, 0x8F8E01]; gradientBevel.alphas = [1, 0, 1]; gradientBevel.ratios = [0, 128, 255]; gradientBevel.blurX = 8; gradientBevel.blurY = 8; gradientBevel.quality = BitmapFilterQuality.HIGH; // Other properties let you set the filter strength and set options // for inner bevel and knockout effects. box.filters = [gradientBevel]; // Add the graphic to the display list. addChild(box); Gradient glow filter Flash Player 9 and later, Adobe AIR 1.0 and later The GradientGlowFilter class lets you apply an enhanced glow effect to display objects or BitmapData objects. The effect gives you greater color control of the glow, and in turn produces a more realistic glow effect. Additionally, the gradient glow filter allows you to apply a gradient glow to the inner, outer, or upper edges of an object. The following example draws a circle on the Stage, and applies a gradient glow filter to it. As you move the mouse further to the right and down, the amount of blur increases in the horizontal and vertical directions respectively. In addition, any time you click on the Stage, the strength of the blur increases. Last updated 3/21/2011 269 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import import import import flash.events.MouseEvent; flash.filters.BitmapFilterQuality; flash.filters.BitmapFilterType; flash.filters.GradientGlowFilter; // Create a new Shape instance. var shape:Shape = new Shape(); // Draw the shape. shape.graphics.beginFill(0xFF0000, 100); shape.graphics.moveTo(0, 0); shape.graphics.lineTo(100, 0); shape.graphics.lineTo(100, 100); shape.graphics.lineTo(0, 100); shape.graphics.lineTo(0, 0); shape.graphics.endFill(); // Position the shape on the Stage. addChild(shape); shape.x = 100; shape.y = 100; // Define a gradient glow. var gradientGlow:GradientGlowFilter = new GradientGlowFilter(); gradientGlow.distance = 0; gradientGlow.angle = 45; gradientGlow.colors = [0x000000, 0xFF0000]; gradientGlow.alphas = [0, 1]; gradientGlow.ratios = [0, 255]; gradientGlow.blurX = 10; gradientGlow.blurY = 10; gradientGlow.strength = 2; gradientGlow.quality = BitmapFilterQuality.HIGH; gradientGlow.type = BitmapFilterType.OUTER; // Define functions to listen for two events. function onClick(event:MouseEvent):void { gradientGlow.strength++; shape.filters = [gradientGlow]; } function onMouseMove(event:MouseEvent):void { gradientGlow.blurX = (stage.mouseX / stage.stageWidth) * 255; gradientGlow.blurY = (stage.mouseY / stage.stageHeight) * 255; shape.filters = [gradientGlow]; } stage.addEventListener(MouseEvent.CLICK, onClick); stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); Last updated 3/21/2011 270 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Example: Combining basic filters Flash Player 9 and later, Adobe AIR 1.0 and later The following code example uses several basic filters, combined with a Timer for creating repeating actions, to create an animated traffic light simulation. import import import import import import import import var var var var var var var var var var var var flash.display.Shape; flash.events.TimerEvent; flash.filters.BitmapFilterQuality; flash.filters.BitmapFilterType; flash.filters.DropShadowFilter; flash.filters.GlowFilter; flash.filters.GradientBevelFilter; flash.utils.Timer; count:Number = 1; distance:Number = 8; angleInDegrees:Number = 225; // opposite of 45 degrees colors:Array = [0xFFFFCC, 0xFEFE78, 0x8F8E01]; alphas:Array = [1, 0, 1]; ratios:Array = [0, 128, 255]; blurX:Number = 8; blurY:Number = 8; strength:Number = 1; quality:Number = BitmapFilterQuality.HIGH; type:String = BitmapFilterType.INNER; knockout:Boolean = false; // Draw the rectangle background for the traffic light. var box:Shape = new Shape(); box.graphics.lineStyle(); box.graphics.beginFill(0xFEFE78); box.graphics.drawRect(100, 50, 90, 200); box.graphics.endFill(); // Draw the 3 circles for the three lights. var stopLight:Shape = new Shape(); stopLight.graphics.lineStyle(); stopLight.graphics.beginFill(0xFF0000); stopLight.graphics.drawCircle(145,90,25); stopLight.graphics.endFill(); var cautionLight:Shape = new Shape(); cautionLight.graphics.lineStyle(); cautionLight.graphics.beginFill(0xFF9900); cautionLight.graphics.drawCircle(145,150,25); cautionLight.graphics.endFill(); var goLight:Shape = new Shape(); goLight.graphics.lineStyle(); goLight.graphics.beginFill(0x00CC00); goLight.graphics.drawCircle(145,210,25); goLight.graphics.endFill(); // Add the graphics to the display list. addChild(box); Last updated 3/21/2011 271 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects addChild(stopLight); addChild(cautionLight); addChild(goLight); // Apply a gradient bevel to the traffic light rectangle. var gradientBevel:GradientBevelFilter = new GradientBevelFilter(distance, angleInDegrees, colors, alphas, ratios, blurX, blurY, strength, quality, type, knockout); box.filters = [gradientBevel]; // Create the inner shadow (for lights when off) and glow // (for lights when on). var innerShadow:DropShadowFilter = new DropShadowFilter(5, 45, 0, 0.5, 3, 3, 1, 1, true, false); var redGlow:GlowFilter = new GlowFilter(0xFF0000, 1, 30, 30, 1, 1, false, false); var yellowGlow:GlowFilter = new GlowFilter(0xFF9900, 1, 30, 30, 1, 1, false, false); var greenGlow:GlowFilter = new GlowFilter(0x00CC00, 1, 30, 30, 1, 1, false, false); // Set the starting state of the lights (green on, red/yellow off). stopLight.filters = [innerShadow]; cautionLight.filters = [innerShadow]; goLight.filters = [greenGlow]; // Swap the filters based on the count value. function trafficControl(event:TimerEvent):void { if (count == 4) { count = 1; } switch (count) { case 1: stopLight.filters = [innerShadow]; cautionLight.filters = [yellowGlow]; goLight.filters = [innerShadow]; break; case 2: stopLight.filters = [redGlow]; cautionLight.filters = [innerShadow]; goLight.filters = [innerShadow]; break; case 3: stopLight.filters = [innerShadow]; cautionLight.filters = [innerShadow]; goLight.filters = [greenGlow]; break; } count++; } // Create a timer to swap the filters at a 3 second interval. var timer:Timer = new Timer(3000, 9); timer.addEventListener(TimerEvent.TIMER, trafficControl); timer.start(); Last updated 3/21/2011 272 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Color matrix filter Flash Player 9 and later, Adobe AIR 1.0 and later The ColorMatrixFilter class is used to manipulate the color and alpha values of the filtered object. This allows you to create saturation changes, hue rotation (shifting a palette from one range of colors to another), luminance-to-alpha changes, and other color manipulation effects using values from one color channel and potentially applying them to other channels. Conceptually, the filter goes through the pixels in the source image one by one and separates each pixel into its red, green, blue, and alpha components. It then multiplies values provided in the color matrix by each of these values, adding the results together to determine the resulting color value that will be displayed on the screen for that pixel. The matrix property of the filter is an array of 20 numbers that are used in calculating the final color. For details of the specific algorithm used to calculate the color values, see the entry describing the ColorMatrixFilter class’s matrix property in the ActionScript 3.0 Reference for the Adobe Flash Platform. Further information and examples of the color matrix filter can be found in the article “Using Matrices for Transformations, Color Adjustments, and Convolution Effects in Flash” on the Adobe Developer Center website. Convolution filter Flash Player 9 and later, Adobe AIR 1.0 and later The ConvolutionFilter class can be used to apply a wide range of imaging transformations to BitmapData objects or display objects, such as blurring, edge detection, sharpening, embossing, and beveling. The convolution filter conceptually goes through each pixel in the source image one by one and determines the final color of that pixel using the value of the pixel and its surrounding pixels. A matrix, specified as an array of numeric values, indicates to what degree the value of each particular neighboring pixel affects the final resulting value. Consider the most commonly used type of matrix, which is a three by three matrix. The matrix includes nine values: N N N N P N N N N When the convolution filter is applied to a certain pixel, it will look at the color value of the pixel itself (“P” in the example), as well as the values of the surrounding pixels (labeled “N” in the example). However, by setting values in the matrix, you specify how much priority certain pixels have in affecting the resulting image. For example, the following matrix, applied using a convolution filter, will leave an image exactly as it was: 0 0 0 0 1 0 0 0 0 The reason the image is unchanged is because the original pixel’s value has a relative strength of 1 in determining the final pixel color, while the surrounding pixels’ values have relative strength of 0—meaning their colors don’t affect the final image. Similarly, this matrix will cause the pixels of an image to shift one pixel to the left: 0 0 0 0 0 0 0 1 0 Notice that in this case, the pixel itself has no effect on the final value of the pixel displayed in that location on the final image—only the value of the pixel to the right is used to determine the pixel’s resulting value. Last updated 3/21/2011 273 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects In ActionScript, you create the matrix as a combination of an Array instance containing the values and two properties specifying the number of rows and columns in the matrix. The following example loads an image and, when the image finishes loading, applies a convolution filter to the image using the matrix in the previous listing: // Load an image onto the Stage. var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg"); loader.load(url); this.addChild(loader); function applyFilter(event:MouseEvent):void { // Create the convolution matrix. var matrix:Array = [0, 0, 0, 0, 0, 1, 0, 0, 0]; var convolution:ConvolutionFilter = new ConvolutionFilter(); convolution.matrixX = 3; convolution.matrixY = 3; convolution.matrix = matrix; convolution.divisor = 1; loader.filters = [convolution]; } loader.addEventListener(MouseEvent.CLICK, applyFilter); Something that isn’t obvious in this code is the effect of using values other than 1 or 0 in the matrix. For example, the same matrix, with the number 8 instead of 1 in the right-hand position, performs the same action (shifting the pixels to the left). In addition, it affects the colors of the image, making them 8 times brighter. This is because the final pixel color values are calculated by multiplying the matrix values by the original pixel colors, adding the values together, and dividing by the value of the filter’s divisor property. Notice that in the example code, the divisor property is set to 1. As a general rule, if you want the brightness of the colors to stay about the same as in the original image, you should make the divisor equal to the sum of the matrix values. So with a matrix where the values add up to 8, and a divisor of 1, the resulting image is going to be roughly 8 times brighter than the original image. Although the effect of this matrix isn’t very noticeable, other matrix values can be used to create various effects. Here are several standard sets of matrix values for different effects using a three by three matrix: • Basic blur (divisor 5): 0 1 0 1 1 1 0 1 0 • Sharpening (divisor 1): 0, -1, 0 -1, 5, -1 0, -1, 0 • Edge detection (divisor 1): 0, -1, 0 -1, 4, -1 0, -1, 0 • Embossing effect (divisor 1): Last updated 3/21/2011 274 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects -2, -1, 0 -1, 1, 1 0, 1, 2 Notice that with most of these effects, the divisor is 1. This is because the negative matrix values added to the positive matrix values result in 1 (or 0 in the case of edge detection, but the divisor property’s value cannot be 0). Displacement map filter Flash Player 9 and later, Adobe AIR 1.0 and later The DisplacementMapFilter class uses pixel values from a BitmapData object (known as the displacement map image) to perform a displacement effect on a new object. The displacement map image is typically different than the actual display object or BitmapData instance to which the filter is being applied. A displacement effect involves displacing pixels in the filtered image—in other words, shifting them away from their original location to some extent. This filter can be used to create a shifted, warped, or mottled effect. The location and amount of displacement applied to a given pixel is determined by the color value of the displacement map image. When working with the filter, in addition to specifying the map image, you specify the following values to control how the displacement is calculated from the map image: • Map point: The location on the filtered image at which the upper-left corner of the displacement filter will be applied. You can use this if you only want to apply the filter to part of an image. • X component: Which color channel of the map image affects the x position of pixels. • Y component: Which color channel of the map image affects the y position of pixels. • X scale: A multiplier value that specifies how strong the x axis displacement is. • Y scale: A multiplier value that specifies how strong the y axis displacement is. • Filter mode: Determines what to do in any empty spaces created by pixels being shifted away. The options, defined as constants in the DisplacementMapFilterMode class, are to display the original pixels (filter mode IGNORE), to wrap the pixels around from the other side of the image (filter mode WRAP, which is the default), to use the nearest shifted pixel (filter mode CLAMP), or to fill in the spaces with a color (filter mode COLOR). To understand how the displacement map filter works, consider a basic example. In the following code, an image is loaded, and when it finishes loading it is centered on the Stage and a displacement map filter is applied to it, causing the pixels in the entire image to shift horizontally to the left. Last updated 3/21/2011 275 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import import import import import import flash.display.BitmapData; flash.display.Loader; flash.events.MouseEvent; flash.filters.DisplacementMapFilter; flash.geom.Point; flash.net.URLRequest; // Load an image onto the Stage. var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image3.jpg"); loader.load(url); this.addChild(loader); var mapImage:BitmapData; var displacementMap:DisplacementMapFilter; // This function is called when the image finishes loading. function setupStage(event:Event):void { // Center the loaded image on the Stage. loader.x = (stage.stageWidth - loader.width) / 2; loader.y = (stage.stageHeight - loader.height) / 2; // Create the displacement map image. mapImage = new BitmapData(loader.width, loader.height, false, 0xFF0000); // Create the displacement filter. displacementMap = new DisplacementMapFilter(); displacementMap.mapBitmap = mapImage; displacementMap.mapPoint = new Point(0, 0); displacementMap.componentX = BitmapDataChannel.RED; displacementMap.scaleX = 250; loader.filters = [displacementMap]; } loader.contentLoaderInfo.addEventListener(Event.COMPLETE, setupStage); The properties used to define the displacement are as follows: • Map bitmap: The displacement bitmap is a new BitmapData instance created by the code. Its dimensions match the dimensions of the loaded image (so the displacement is applied to the entire image). It is filled with solid red pixels. • Map point: This value is set to the point 0, 0—again, causing the displacement to be applied to the entire image. • X component: This value is set to the constant BitmapDataChannel.RED, meaning the red value of the map bitmap will determine how much the pixels are displaced (how much they move) along the x axis. • X scale: This value is set to 250. The full amount of displacement (from the map image being completely red) only displaces the image by a small amount (roughly one-half of a pixel), so if this value was set to 1 the image would only shift .5 pixels horizontally. By setting it to 250, the image shifts by approximately 125 pixels. Last updated 3/21/2011 276 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects These settings cause the filtered image’s pixels to shift 250 pixels to the left. The direction (left or right) and amount of shift is based on the color value of the pixels in the map image. Conceptually, the filter goes through the pixels of the filtered image one by one (at least, the pixels in the region where the filter is applied, which in this case means all the pixels), and does the following with each pixel: 1 It finds the corresponding pixel in the map image. For example, when the filter calculates the displacement amount for the pixel in the upper-left corner of the filtered image, it looks at the pixel in the upper-left corner of the map image. 2 It determines the value of the specified color channel in the map pixel. In this case, the x component color channel is the red channel, so the filter looks to see what the value of the red channel of the map image is at the pixel in question. Since the map image is solid red, the pixel’s red channel is 0xFF, or 255. This is used as the displacement value. 3 It compares the displacement value to the “middle” value (127, which is halfway between 0 and 255). If the displacement value is lower than the middle value, the pixel shifts in a positive direction (to the right for x displacement; down for y displacement). On the other hand, if the displacement value is higher than the middle value (as in this example), the pixel shifts in a negative direction (to the left for x displacement; up for y displacement). To be more precise, the filter subtracts the displacement value from 127, and the result (positive or negative) is the relative amount of displacement that is applied. 4 Finally, it determines the actual amount of displacement by determining what percentage of full displacement the relative displacement value represents. In this case, full red means 100% displacement. That percentage is then multiplied by the x scale or y scale value to determine the number of pixels of displacement that will be applied. In this example, 100% times a multiplier of 250 determines the amount of displacement—roughly 125 pixels to the left. Because no values are specified for y component and y scale, the defaults (which cause no displacement) are used— that’s why the image doesn’t shift at all in the vertical direction. The default filter mode setting, WRAP, is used in the example, so as the pixels shift to the left the empty space on the right is filled in by the pixels that shifted off the left edge of the image. You can experiment with this value to see the different effects. For example, if you add the following line to the portion of code where the displacement properties are being set (before the line loader.filters = [displacementMap]), it will make the image look as though it has been smeared across the Stage: displacementMap.mode = DisplacementMapFilterMode.CLAMP; For a more complex example, the following listing uses a displacement map filter to create a magnifying glass effect on an image: Last updated 3/21/2011 277 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects import import import import import import import import import import import import flash.display.Bitmap; flash.display.BitmapData; flash.display.BitmapDataChannel; flash.display.GradientType; flash.display.Loader; flash.display.Shape; flash.events.MouseEvent; flash.filters.DisplacementMapFilter; flash.filters.DisplacementMapFilterMode; flash.geom.Matrix; flash.geom.Point; flash.net.URLRequest; // Create the gradient circles that will together form the // displacement map image var radius:uint = 50; var type:String = GradientType.LINEAR; var redColors:Array = [0xFF0000, 0x000000]; var blueColors:Array = [0x0000FF, 0x000000]; var alphas:Array = [1, 1]; var ratios:Array = [0, 255]; var xMatrix:Matrix = new Matrix(); xMatrix.createGradientBox(radius * 2, radius * 2); var yMatrix:Matrix = new Matrix(); yMatrix.createGradientBox(radius * 2, radius * 2, Math.PI / 2); var xCircle:Shape = new Shape(); xCircle.graphics.lineStyle(0, 0, 0); xCircle.graphics.beginGradientFill(type, redColors, alphas, ratios, xMatrix); xCircle.graphics.drawCircle(radius, radius, radius); var yCircle:Shape = new Shape(); yCircle.graphics.lineStyle(0, 0, 0); yCircle.graphics.beginGradientFill(type, blueColors, alphas, ratios, yMatrix); yCircle.graphics.drawCircle(radius, radius, radius); // Position the circles at the bottom of the screen, for reference. this.addChild(xCircle); xCircle.y = stage.stageHeight - xCircle.height; this.addChild(yCircle); yCircle.y = stage.stageHeight - yCircle.height; yCircle.x = 200; // Load an image onto the Stage. var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("http://www.helpexamples.com/flash/images/image1.jpg"); loader.load(url); this.addChild(loader); // Create the map image by combining the two gradient circles. var map:BitmapData = new BitmapData(xCircle.width, xCircle.height, false, 0x7F7F7F); map.draw(xCircle); var yMap:BitmapData = new BitmapData(yCircle.width, yCircle.height, false, 0x7F7F7F); yMap.draw(yCircle); map.copyChannel(yMap, yMap.rect, new Point(0, 0), BitmapDataChannel.BLUE, BitmapDataChannel.BLUE); Last updated 3/21/2011 278 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects yMap.dispose(); // Display the map image on the Stage, for reference. var mapBitmap:Bitmap = new Bitmap(map); this.addChild(mapBitmap); mapBitmap.x = 400; mapBitmap.y = stage.stageHeight - mapBitmap.height; // This function creates the displacement map filter at the mouse location. function magnify():void { // Position the filter. var filterX:Number = (loader.mouseX) - (map.width / 2); var filterY:Number = (loader.mouseY) - (map.height / 2); var pt:Point = new Point(filterX, filterY); var xyFilter:DisplacementMapFilter = new DisplacementMapFilter(); xyFilter.mapBitmap = map; xyFilter.mapPoint = pt; // The red in the map image will control x displacement. xyFilter.componentX = BitmapDataChannel.RED; // The blue in the map image will control y displacement. xyFilter.componentY = BitmapDataChannel.BLUE; xyFilter.scaleX = 35; xyFilter.scaleY = 35; xyFilter.mode = DisplacementMapFilterMode.IGNORE; loader.filters = [xyFilter]; } // This function is called when the mouse moves. If the mouse is // over the loaded image, it applies the filter. function moveMagnifier(event:MouseEvent):void { if (loader.hitTestPoint(loader.mouseX, loader.mouseY)) { magnify(); } } loader.addEventListener(MouseEvent.MOUSE_MOVE, moveMagnifier); Last updated 3/21/2011 279 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects The code first generates two gradient circles, which are combined together to form the displacement map image. The red circle creates the x axis displacement (xyFilter.componentX = BitmapDataChannel.RED), and the blue circle creates the y axis displacement (xyFilter.componentY = BitmapDataChannel.BLUE). To help you understand what the displacement map image looks like, the code adds the original circles as well as the combined circle that serves as the map image to the bottom of the screen. The code then loads an image and, as the mouse moves, applies the displacement filter to the portion of the image that’s under the mouse. The gradient circles used as the displacement map image causes the displaced region to spread out away from the pointer. Notice that the gray regions of the displacement map image don’t cause any displacement. The gray color is 0x7F7F7F. The blue and red channels of that shade of gray exactly match the middle shade of those color channels, so there is no displacement in a gray area of the map image. Likewise, in the center of the circle there is no displacement. Although the color there isn’t gray, that color’s blue channel and red channel are identical to the blue channel and red channel of medium gray, and since blue and red are the colors that cause displacement, no displacement happens there. Shader filter Flash Player 10 and later, Adobe AIR 1.5 and later The ShaderFilter class lets you use a custom filter effect defined as a Pixel Bender shader. Because the filter effect is written as a Pixel Bender shader, the effect can be completely customized. The filtered content is passed in to the shader as an image input, and the result of the shader operation becomes the filter result. Note: The Shader filter is available in ActionScript starting with Flash Player 10 and Adobe AIR 1.5. To apply a shader filter to an object, you first create a Shader instance representing the Pixel Bender shader that you are using. For details on the procedure for creating a Shader instance and on how to specify input image and parameter values, see “Working with Pixel Bender shaders” on page 288. When using a shader as a filter, there are three important things to keep in mind: • The shader must be defined to accept at least one input image. • The filtered object (the display object or BitmapData object to which the filter is applied) is passed to the shader as the first input image value. Because of this, do not manually specify a value for the first image input. Last updated 3/21/2011 280 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects • If the shader defines more that one input image, the additional inputs must be specified manually (that is, by setting the input property of any ShaderInput instance that belongs to the Shader instance). Once you have a Shader object for your shader, you create a ShaderFilter instance. This is the actual filter object that you use like any other filter. To create a ShaderFilter that uses a Shader object, call the ShaderFilter() constructor and pass the Shader object as an argument, as shown in this listing: var myFilter:ShaderFilter = new ShaderFilter(myShader); For a complete example of using a shader filter, see “Using a shader as a filter” on page 306. Filtering display objects example: Filter Workbench Flash Player 9 and later, Adobe AIR 1.0 and later The Filter Workbench provides a user interface to apply different filters to images and other visual content and see the resulting code that can be used to generate the same effect in ActionScript. In addition to providing a tool for experimenting with filters, this application demonstrates the following techniques: • Creating instances of various filters • Applying multiple filters to a display object To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The Filter Workbench application files can be found in the Samples/FilterWorkbench folder. The application consists of the following files: File Description com/example/programmingas3/filterWorkbench/FilterWorkbenchController.as Class that provides the main functionality of the application, including switching content to which filters are applied, and applying filters to content. com/example/programmingas3/filterWorkbench/IFilterFactory.as Interface defining common methods that are implemented by each of the filter factory classes. This interface defines the common functionality that the FilterWorkbenchController class uses to interact with the individual filter factory classes. in folder com/example/programmingas3/filterWorkbench/: Set of classes, each of which implements the IFilterFactory interface. Each of these classes provides the functionality of creating and setting values for a single type of filter. The filter property panels in the application use these factory classes to create instances of their particular filters, which the FilterWorkbenchController class retrieves and applies to the image content. BevelFactory.as BlurFactory.as ColorMatrixFactory.as ConvolutionFactory.as DropShadowFactory.as GlowFactory.as GradientBevelFactory.as GradientGlowFactory.as com/example/programmingas3/filterWorkbench/IFilterPanel.as Last updated 3/21/2011 Interface defining common methods that are implemented by classes that define the user interface panels that are used to manipulate filter values in the application. 281 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects File Description com/example/programmingas3/filterWorkbench/ColorStringFormatter.as Utility class that includes a method to convert a numeric color value to hexadecimal String format com/example/programmingas3/filterWorkbench/GradientColor.as Class that serves as a value object, combining into a single object the three values (color, alpha, and ratio) that are associated with each color in the GradientBevelFilter and GradientGlowFilter User interface (Flex) FilterWorkbench.mxml The main file defining the application’s user interface. flexapp/FilterWorkbench.as Class that provides the functionality for the main application’s user interface; this class is used as the code-behind class for the application MXML file. In folder flexapp/filterPanels: Set of MXML components that provide the functionality for each panel that is used to set options for a single filter. BevelPanel.mxml BlurPanel.mxml ColorMatrixPanel.mxml ConvolutionPanel.mxml DropShadowPanel.mxml GlowPanel.mxml GradientBevelPanel.mxml GradientGlowPanel.mxml flexapp/ImageContainer.as A display object that serves as a container for the loaded image on the screen flexapp/controls/BGColorCellRenderer.as Custom cell renderer used to change the background color of a cell in the DataGrid component flexapp/controls/QualityComboBox.as Custom control defining a combo box that can be used for the Quality setting in several filter panels. flexapp/controls/TypeComboBox.as Custom control defining a combo box that can be used for the Type setting in several filter panels. User interface (Flash) FilterWorkbench.fla The main file defining the application’s user interface. flashapp/FilterWorkbench.as Class that provides the functionality for the main application’s user interface; this class is used as the document class for the application FLA file. Last updated 3/21/2011 282 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects File Description In folder flashapp/filterPanels: Set of classes that provide the functionality for each panel that is used to set options for a single filter. BevelPanel.as For each class, there is also an associated MovieClip symbol in the library of the main application FLA file, whose name matches the name of the class (for example, the symbol “BlurPanel” is linked to the class defined in BlurPanel.as). The components that make up the user interface are positioned and named within those symbols. BlurPanel.as ColorMatrixPanel.as ConvolutionPanel.as DropShadowPanel.as GlowPanel.as GradientBevelPanel.as GradientGlowPanel.as flashapp/ImageContainer.as A display object that serves as a container for the loaded image on the screen flashapp/BGColorCellRenderer.as Custom cell renderer used to change the background color of a cell in the DataGrid component flashapp/ButtonCellRenderer.as Custom cell renderer used to include a button component in a cell in the DataGrid component Filtered image content com/example/programmingas3/filterWorkbench/ImageType.as This class serves as a value object containing the type and URL of a single image file to which the application can load and apply filters. The class also includes a set of constants representing the actual image files available. images/sampleAnimation.swf, Images and other visual content to which filters are applied in the application. images/sampleImage1.jpg, images/sampleImage2.jpg Experimenting with ActionScript filters Flash Player 9 and later, Adobe AIR 1.0 and later The Filter Workbench application is designed to help you experiment with various filter effects and generate the relevant ActionScript code for that effect. The application lets you select from three different files containing visual content, including bitmap images and an animation created by Flash, and apply eight different ActionScript filters to the selected image, either individually or in combination with other filters. The application includes the following filters: • Bevel (flash.filters.BevelFilter) • Blur (flash.filters.BlurFilter) • Color matrix (flash.filters.ColorMatrixFilter) • Convolution (flash.filters.ConvolutionFilter) • Drop shadow (flash.filters.DropShadowFilter) • Glow (flash.filters.GlowFilter) • Gradient bevel (flash.filters.GradientBevelFilter) Last updated 3/21/2011 283 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects • Gradient glow (flash.filters.GradientGlowFilter) Once a user has selected an image and a filter to apply to that image, the application displays a panel with controls for setting the specific properties of the selected filter. For example, the following image shows the application with the Bevel filter selected: As the user adjusts the filter properties, the preview updates in real time. The user can also apply multiple filters by customizing one filter, clicking the Apply button, customizing another filter, clicking the Apply button, and so forth. There are a few features and limitations in the application’s filter panels: • The color matrix filter includes a set of controls for directly manipulating common image properties including brightness, contrasts, saturation, and hue. In addition, custom color matrix values can be specified. • The convolution filter, which is only available using ActionScript, includes a set of commonly used convolution matrix values, or custom values can be specified. However, while the ConvolutionFilter class accepts a matrix of any size, the Filter Workbench application uses a fixed 3 x 3 matrix, the most commonly used filter size. • The displacement map filter and shader filter, which are only available in ActionScript, are not available in the Filter Workbench application. Creating filter instances Flash Player 9 and later, Adobe AIR 1.0 and later The Filter Workbench application includes a set of classes, one for each of the available filters, which are used by the individual panels to create the filters. When a user selects a filter, the ActionScript code associated with the filter panel creates an instance of the appropriate filter factory class. (These classes are known as factory classes because their purpose is to create instances of other objects, much like a real-world factory creates individual products.) Last updated 3/21/2011 284 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects Whenever the user changes a property value on the panel, the panel’s code calls the appropriate method in the factory class. Each factory class includes specific methods that the panel uses to create the appropriate filter instance. For example, if the user selects the Blur filter, the application creates a BlurFactory instance. The BlurFactory class includes a modifyFilter() method that accepts three parameters: blurX, blurY, and quality, which together are used to create the desired BlurFilter instance: private var _filter:BlurFilter; public function modifyFilter(blurX:Number = 4, blurY:Number = 4, quality:int = 1):void { _filter = new BlurFilter(blurX, blurY, quality); dispatchEvent(new Event(Event.CHANGE)); } On the other hand, if the user selects the Convolution filter, that filter allows for much greater flexibility and consequently has a larger set of properties to control. In the ConvolutionFactory class, the following code is called when the user selects a different value on the filter panel: private var _filter:ConvolutionFilter; public function modifyFilter(matrixX:Number = 0, matrixY:Number = 0, matrix:Array = null, divisor:Number = 1.0, bias:Number = 0.0, preserveAlpha:Boolean = true, clamp:Boolean = true, color:uint = 0, alpha:Number = 0.0):void { _filter = new ConvolutionFilter(matrixX, matrixY, matrix, divisor, bias, preserveAlpha, clamp, color, alpha); dispatchEvent(new Event(Event.CHANGE)); } Notice that in each case, when the filter values are changed, the factory object dispatches an Event.CHANGE event to notify listeners that the filter’s values have changed. The FilterWorkbenchController class, which does the work of actually applying filters to the filtered content, listens for that event to ascertain when it needs to retrieve a new copy of the filter and re-apply it to the filtered content. The FilterWorkbenchController class doesn’t need to know specific details of each filter factory class—it just needs to know that the filter has changed and to be able to access a copy of the filter. To support this, the application includes an interface, IFilterFactory, that defines the behavior a filter factory class needs to provide so the application’s FilterWorkbenchController instance can do its job. The IFilterFactory defines the getFilter() method that’s used in the FilterWorkbenchController class: function getFilter():BitmapFilter; Notice that the getFilter() interface method definition specifies that it returns a BitmapFilter instance rather than a specific type of filter. The BitmapFilter class does not define a specific type of filter. Rather, BitmapFilter is the base class on which all the filter classes are built. Each filter factory class defines a specific implementation of the getFilter() method in which it returns a reference to the filter object it has built. For example, here is an abbreviated version of the ConvolutionFactory class’s source code: Last updated 3/21/2011 285 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects public class ConvolutionFactory extends EventDispatcher implements IFilterFactory { // ------- Private vars ------private var _filter:ConvolutionFilter; ... // ------- IFilterFactory implementation ------public function getFilter():BitmapFilter { return _filter; } ... } In the ConvolutionFactory class’s implementation of the getFilter() method, it returns a ConvolutionFilter instance, although any object that calls getFilter() doesn’t necessarily know that—according to the definition of the getFilter() method that ConvolutionFactory follows, it must return any BitmapFilter instance, which could be an instance of any of the ActionScript filter classes. Applying filters to display objects Flash Player 9 and later, Adobe AIR 1.0 and later As explained previously, the Filter Workbench application uses an instance of the FilterWorkbenchController class (hereafter referred to as the “controller instance”), which performs the actual task of applying filters to the selected visual object. Before the controller instance can apply a filter, it first needs to know what image or visual content the filter should be applied to. When the user selects an image, the application calls the setFilterTarget() method in the FilterWorkbenchController class, passing in one of the constants defined in the ImageType class: public function setFilterTarget(targetType:ImageType):void { ... _loader = new Loader(); ... _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, targetLoadComplete); ... } Using that information the controller instance loads the designated file, storing it in an instance variable named _currentTarget once it loads: private var _currentTarget:DisplayObject; private function targetLoadComplete(event:Event):void { ... _currentTarget = _loader.content; ... } When the user selects a filter, the application calls the controller instance’s setFilter() method, giving the controller a reference to the relevant filter factory object, which it stores in an instance variable named _filterFactory. Last updated 3/21/2011 286 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects private var _filterFactory:IFilterFactory; public function setFilter(factory:IFilterFactory):void { ... _filterFactory = factory; _filterFactory.addEventListener(Event.CHANGE, filterChange); } Notice that, as described previously, the controller instance doesn’t know the specific data type of the filter factory instance that it is given; it only knows that the object implements the IFilterFactory instance, meaning it has a getFilter() method and it dispatches a change (Event.CHANGE) event when the filter changes. When the user changes a filter’s properties in the filter’s panel, the controller instance finds out that the filter has changed through the filter factory’s change event, which calls the controller instance’s filterChange() method. That method, in turn, calls the applyTemporaryFilter() method: private function filterChange(event:Event):void { applyTemporaryFilter(); } private function applyTemporaryFilter():void { var currentFilter:BitmapFilter = _filterFactory.getFilter(); // Add the current filter to the set temporarily _currentFilters.push(currentFilter); // Refresh the filter set of the filter target _currentTarget.filters = _currentFilters; // Remove the current filter from the set // (This doesn't remove it from the filter target, since // the target uses a copy of the filters array internally.) _currentFilters.pop(); } The work of applying the filter to the display object occurs within the applyTemporaryFilter() method. First, the controller retrieves a reference to the filter object by calling the filter factory’s getFilter() method. var currentFilter:BitmapFilter = _filterFactory.getFilter(); The controller instance has an Array instance variable named _currentFilters, in which it stores all the filters that have been applied to the display object. The next step is to add the newly updated filter to that array: _currentFilters.push(currentFilter); Next, the code assigns the array of filters to the display object’s filters property, which actually applies the filters to the image: _currentTarget.filters = _currentFilters; Finally, since this most recently added filter is still the “working” filter, it shouldn’t be permanently applied to the display object, so it is removed from the _currentFilters array: Last updated 3/21/2011 287 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Filtering display objects _currentFilters.pop(); Removing this filter from the array doesn’t affect the filtered display object, because a display object makes a copy of the filters array when it is assigned to the filters property, and it uses that internal array rather than the original one. For this reason, any changes that are made to the array of filters don’t affect the display object until the array is assigned to the display object’s filters property again. Last updated 3/21/2011 288 Chapter 14: Working with Pixel Bender shaders Flash Player 10 and later, Adobe AIR 1.5 and later Adobe Pixel Bender Toolkit allows developers to write shaders that create graphical effects and perform other image and data processing. The Pixel Bender bytecode can be executed in ActionScript to apply the effect to image data or visual content. Using Pixel Bender shaders in ActionScript gives you the capability to create custom visual effects and perform data processing beyond the built-in capabilities in ActionScript. Note: Pixel Bender support is available starting in Flash Player 10 and Adobe AIR 1.5. Pixel Bender blends, filters, and fills are not supported under GPU rendering. More Help topics Adobe Pixel Bender Technology Center Pixel Bender Developer's Guide Pixel Bender Reference flash.display.Shader flash.filters.ShaderFilter Pixel Bender basics for Flash Pixel Bender basics for Flex Basics of Pixel Bender shaders Flash Player 10 and later, Adobe AIR 1.5 and later Adobe Pixel Bender is a programming language that is used to create or manipulate image content. Using Pixel Bender you create a kernel, also known as a shader. The shader defines a single function that executes on each of the pixels of an image individually. The result of each call to the function is the output color at that pixel coordinate in the image. Input images and parameter values can be specified to customize the operation. In a single execution of a shader, input and parameter values are constant. The only thing that varies is the coordinate of the pixel whose color is the result of the function call. Where possible, the shader function is called for multiple output pixel coordinates in parallel. This improves shader performance and can provide high-performance processing. In ActionScript, three types of effects can be easily created using a shader: • drawing fill • blend mode • filter Last updated 3/21/2011 289 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders A shader can also be executed in stand-alone mode. Using stand-alone mode a shader’s result is accessed directly rather than pre-specifying its intended use. The result can be accessed as image data or as binary or number data. The data need not be image data at all. In this way you can give a shader a set of data as an input. The shader processes the data, and you can access the result data returned by the shader. Pixel Bender support is available starting in Flash Player 10 and Adobe AIR 1.5. Pixel Bender blends, filters, and fills are not supported under GPU rendering. On mobile devices, Pixel Bender shaders do run under CPU rendering. However, performance is not at the same level as on a desktop computer. Many shader programs may only execute at a few frames per second. Important concepts and terms The following reference list contains important terms that you will encounter when creating and using Pixel Bender shaders: Kernel For Pixel Bender, a kernel is the same thing as a shader. Using Pixel Bender your code defines a kernel, which defines a single function that executes on each of the pixels of an image individually. Pixel Bender bytecode When a Pixel Bender kernel is compiled it is transformed into Pixel Bender bytecode. The bytecode is accessed and executed at run time. Pixel Bender language The programming language used to create a Pixel Bender kernel. Pixel Bender Toolkit The application that is used to create a Pixel Bender bytecode file from Pixel Bender source code. The toolkit allows you to write, test, and compile Pixel Bender source code. Shader For the purposes of this document, a shader is a set of functionality written in the Pixel Bender language. A shader’s code creates a visual effect or performs a calculation. In either case, the shader returns a set of data (usually the pixels of an image). The shader performs the same operation on each data point, with the only difference being the coordinates of the output pixel.The shader is not written in ActionScript. It is written in the Pixel Bender language and compiled into Pixel Bender bytecode. It can be embedded into a SWF file at compile time or loaded as an external file at run time. In either case it is accessed in ActionScript by creating a Shader object and linking that object to the shader bytecode. Shader input A complex input, usually bitmap image data, that is provided to a shader to use in its calculations. For each input variable defined in a shader, a single value (that is, a single image or set of binary data) is used for the entire execution of the shader. Shader parameter A single value (or limited set of values) that is provided to a shader to use in its calculations. Each parameter value is defined for a single shader execution, and the same value is used throughout the shader execution. Working through the code examples You may want to test the example code listings that are provided. Testing the code involves running the code and viewing the results in the SWF that’s created. All the examples create content using the drawing API which uses or is modified by the shader effect. Most of the example code listings include two parts. One part is the Pixel Bender source code for the shader used in the example. You must first use the Pixel Bender Toolkit to compile the source code to a Pixel Bender bytecode file. Follow these steps to create the Pixel Bender bytecode file: 1 Open Adobe Pixel Bender Toolkit. If necessary, from the Build menu choose “Turn on Flash Player warnings and errors.” 2 Copy the Pixel Bender code listing and paste it into the code editor pane of the Pixel Bender Toolkit. 3 From the File menu, choose “Export kernel filter for Flash Player.” Last updated 3/21/2011 290 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders 4 Save the Pixel Bender bytecode file in the same directory as the Flash document. The file’s name should match the name specified in the example description. The ActionScript part of each example is written as a class file. To test the example in Flash Professional: 1 Create an empty Flash document and save it to your computer. 2 Create a new ActionScript file and save it in the same directory as the Flash document. The file’s name should match the name of the class in the code listing. For instance, if the code listing defines a class named MyApplication, use the name MyApplication.as to save the ActionScript file. 3 Copy the code listing into the ActionScript file and save the file. 4 In the Flash document, click a blank part of the Stage or work space to activate the document Property inspector. 5 In the Property inspector, in the Document Class field, enter the name of the ActionScript class you copied from the text. 6 Run the program using Control > Test Movie You will see the results of the example in the preview window. These techniques for testing example code listings are explained in more detail in “How to Use ActionScript Examples” on page 1061. Loading or embedding a shader Flash Player 10 and later, Adobe AIR 1.5 and later The first step in using a Pixel Bender shader in ActionScript is to get access to the shader in your ActionScript code. Because a shader is created using the Adobe Pixel Bender Toolkit, and written in the Pixel Bender language, it cannot be directly accessed in ActionScript. Instead, you create an instance of the Shader class that represents the Pixel Bender shader to ActionScript. The Shader object allows you to find out information about the shader, such as whether it expects parameters or input image values. You pass the Shader object to other objects to actually use the shader. For example, to use the shader as a filter you assign the Shader object to a ShaderFilter object’s shader property. Alternatively, to use the shader as a drawing fill, you pass the Shader object as an argument to the Graphics.beginShaderFill() method. Your ActionScript code can access a shader created by Adobe Pixel Bender Toolkit (a .pbj file) in two ways: • Loaded at run time: the shader file can be loaded as an external asset using a URLLoader object. This technique is like loading an external asset such as a text file. The following example demonstrates loading a shader bytecode file at run time and linking it to a Shader instance: Last updated 3/21/2011 291 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest("myShader.pbj")); var shader:Shader; function onLoadComplete(event:Event):void { // Create a new shader and set the loaded data as its bytecode shader = new Shader(); shader.byteCode = loader.data; // You can also pass the bytecode to the Shader() constructor like this: // shader = new Shader(loader.data); // do something with the shader } • Embedded in the SWF file: the shader file can be embedded in the SWF file at compile time using the [Embed] metadata tag. The [Embed] metadata tag is only available if you use the Flex SDK to compile the SWF file. The [Embed] tag’s source parameter points to the shader file, and its mimeType parameter is "application/octetstream", as in this example: [Embed(source="myShader.pbj", mimeType="application/octet-stream")] var MyShaderClass:Class; // ... // create a shader and set the embedded shader as its bytecode var shader:Shader = new Shader(); shader.byteCode = new MyShaderClass(); // You can also pass the bytecode to the Shader() constructor like this: // var shader:Shader = new Shader(new MyShaderClass()); // do something with the shader In either case, you link the raw shader bytecode (the URLLoader.data property or an instance of the [Embed] data class) to the Shader instance. As the previous examples demonstrate, you can assign the bytecode to the Shader instance in two ways. You can pass the shader bytecode as an argument to the Shader() constructor. Alternatively, you can set it as the Shader instance's byteCode property. Once a Pixel Bender shader has been created and linked to a Shader object, you can use the shader to create effects in several ways. You can use it as a filter, a blend mode, a bitmap fill, or for stand-alone processing of bitmap or other data. You can also use the Shader object’s data property to access the shader’s metadata, specify input images, and set parameter values. Accessing shader metadata Flash Player 10 and later, Adobe AIR 1.5 and later While creating a Pixel Bender shader kernel, the author can specify metadata about the shader in the Pixel Bender source code. While using a shader in ActionScript, you can examine the shader and extract its metadata. Last updated 3/21/2011 292 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders When you create a Shader instance and link it to a Pixel Bender shader, a ShaderData object containing data about the shader is created and stored in the Shader object’s data property. The ShaderData class doesn’t define any properties of its own. However, at run time a property is dynamically added to the ShaderData object for each metadata value defined in the shader source code. The name given to each property is the same as the name specified in the metadata. For example, suppose the source code of a Pixel Bender shader includes the following metadata definition: namespace : "Adobe::Example"; vendor : "Bob Jones"; version : 1; description : "Creates a version of the specified image with the specified brightness."; The ShaderData object created for that shader is created with the following properties and values: • namespace (String): "Adobe::Example" • vendor (String): "Bob Jones" • version (String): "1" • description (String): "Creates a version of the specified image with the specified brightness" Because metadata properties are dynamically added to the ShaderData object, you can use a for..in loop to examine the ShaderData object. Using this technique you can find out whether the shader has any metadata and what the metadata values are. In addition to metadata properties, a ShaderData object can have properties representing inputs and parameters that are defined in the shader. When you use a for..in loop to examine a ShaderData object, check the data type of each property to determine whether the property is an input (a ShaderInput instance), a parameter (a ShaderParameter instance), or a metadata value (a String instance). The following example shows how to use a for..in loop to examine the dynamic properties of a shader’s data property. Each metadata value is added to a Vector instance named metadata. Note that this example assumes a Shader instance named myShader is already created: var shaderData:ShaderData = myShader.data; var metadata:Vector. = new Vector.(); for (var prop:String in shaderData) { if (!(shaderData[prop] is ShaderInput) && !(shaderData[prop] is ShaderParameter)) { metadata[metadata.length] = shaderData[prop]; } } // do something with the metadata For a version of this example that also extracts shader inputs and parameters, see “Identifying shader inputs and parameters” on page 293. For more information about input and parameter properties, see “Specifying shader input and parameter values” on page 293. Last updated 3/21/2011 293 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders Specifying shader input and parameter values Flash Player 10 and later, Adobe AIR 1.5 and later Many Pixel Bender shaders are defined to use one or more input images that are used in the shader processing. For example, it’s common for a shader to accept a source image and output that image with a particular effect applied to it. Depending on how the shader is used the input value may be specified automatically or you may need to explicitly provide a value. Similarly, many shaders specify parameters that are used to customize the output of the shader. You must also explicitly set a value for each parameter before using the shader. You use the Shader object’s data property to set shader inputs and parameters and to determine whether a particular shader expects inputs or parameters. The data property is a ShaderData instance. Identifying shader inputs and parameters Flash Player 10 and later, Adobe AIR 1.5 and later The first step in specifying shader input and parameter values is to find out whether the particular shader you’re using expects any input images or parameters. Each Shader instance has a data property containing a ShaderData object. If the shader defines any inputs or parameters, they are accessed as properties of that ShaderData object. The properties’ names match the names specified for the inputs and parameters in the shader source code. For example, if a shader defines an input named src, the ShaderData object has a property named src representing that input. Each property that represents an input is a ShaderInput instance, and each property that represents a parameter is a ShaderParameter instance. Ideally, the author of the shader provides documentation for the shader, indicating what input image values and parameters the shader expects, what they represent, the appropriate values, and so forth. However, if the shader isn’t documented (and you don’t have its source code) you can inspect the shader data to identify the inputs and parameters. The properties representing inputs and parameters are dynamically added to the ShaderData object. Consequently, you can use a for..in loop to examine the ShaderData object to find out whether its associated shader defines any inputs or parameters. As described in “Accessing shader metadata” on page 291, any metadata value defined for a shader is also accessed as a dynamic property added to the Shader.data property. When you use this technique to identify shader inputs and parameters, check the data type of the dynamic properties. If a property is a ShaderInput instance it represents an input. If it is a ShaderParameter instance it represents a parameter. Otherwise, it is a metadata value. The following example shows how to use a for..in loop to examine the dynamic properties of a shader’s data property. Each input (ShaderInput object) is added to a Vector instance named inputs. Each parameter (ShaderParameter object) is added to a Vector instance named parameters. Finally, any metadata properties are added to a Vector instance named metadata. Note that this example assumes a Shader instance named myShader is already created: Last updated 3/21/2011 294 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders var var var var shaderData:ShaderData = myShader.data; inputs:Vector. = new Vector.(); parameters:Vector. = new Vector.(); metadata:Vector. = new Vector.(); for (var prop:String in shaderData) { if (shaderData[prop] is ShaderInput) { inputs[inputs.length] = shaderData[prop]; } else if (shaderData[prop] is ShaderParameter) { parameters[parameters.length] = shaderData[prop]; } else { metadata[metadata.length] = shaderData[prop]; } } // do something with the inputs or properties Specifying shader input values Flash Player 10 and later, Adobe AIR 1.5 and later Many shaders expect one or more input images that are used in the shader processing. However, in many cases an input is specified automatically when the Shader object is used. For example, suppose a shader requires one input, and that shader is used as a filter. When the filter is applied to a display object or BitmapData object, that object is automatically set as the input. In that case you do not explicitly set an input value. However, in some cases, especially if a shader defines multiple inputs, you do explicitly set a value for an input. Each input that is defined in a shader is represented in ActionScript by a ShaderInput object. The ShaderInput object is a property of the ShaderData instance in the Shader object’s data property, as described in “Identifying shader inputs and parameters” on page 293. For example, suppose a shader defines an input named src, and that shader is linked to a Shader object named myShader. In that case you access the ShaderInput object corresponding to the src input using the following identifier: myShader.data.src Each ShaderInput object has an input property that is used to set the value for the input. You set the input property to a BitmapData instance to specify image data. You can also set the input property to a BitmapData or Vector. instance to specify binary or number data. For details and restrictions on using a BitmapData or Vector. instance as an input, see the ShaderInput.input listing in the ActionScript 3.0 Reference for the Adobe Flash Platform. In addition to the input property, a ShaderInput object has properties that can be used to determine what type of image the input expects. These properties include the width, height, and channels properties. Each ShaderInput object also has an index property that is useful for determining whether an explicit value must be provided for the input. If a shader expects more inputs than the number that are automatically set, then you set values for those inputs. For details on the different ways to use a shader, and whether input values are automatically set, see “Using a shader” on page 298. Last updated 3/21/2011 295 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders Specifying shader parameter values Flash Player 10 and later, Adobe AIR 1.5 and later Some shaders define parameter values that the shader uses in creating its result. For example, a shader that alters the brightness of an image might specify a brightness parameter that determines how much the operation affects the brightness. A single parameter defined in a shader can expect a single value or multiple values, according to the parameter definition in the shader. Each parameter that is defined in a shader is represented in ActionScript by a ShaderParameter object. The ShaderParameter object is a property of the ShaderData instance in the Shader object’s data property, as described in “Identifying shader inputs and parameters” on page 293. For example, suppose a shader defines a parameter named brightness, and that shader is represented by a Shader object named myShader. In that case you access the ShaderParameter corresponding to the brightness parameter using the following identifier: myShader.data.brightness To set a value (or values) for the parameter, create an ActionScript array containing the value or values and assign that array to the ShaderParameter object’s value property. The value property is defined as an Array instance because it’s possible that a single shader parameter requires multiple values. Even if the shader parameter only expects a single value, you must wrap the value in an Array object to assign it to the ShaderParameter.value property. The following listing demonstrates setting a single value as the value property: myShader.data.brightness.value = [75]; If the Pixel Bender source code for the shader defines a default value for the parameter, an array containing the default value or values is created and assigned to the ShaderParameter object’s value property when the Shader object is created. Once an array has been assigned to the value property (including if it’s the default array) the parameter value can be changed by changing the value of the array element. You do not need to create a new array and assign it to the value property. The following example demonstrates setting a shader’s parameter value in ActionScript. In this example the shader defines a parameter named color. The color parameter is declared as a float4 variable in the Pixel Bender source code, which means it is an array of four floating point numbers. In the example, the color parameter value is changed continuously, and each time it changes the shader is used to draw a colored rectangle on the screen. The result is an animated color change. Note: The code for this example was written by Ryan Taylor. Thank you Ryan for sharing this example. You can see Ryan’s portfolio and read his writing at www.boostworthy.com/. The ActionScript code centers around three methods: • init(): In the init() method the code loads the Pixel Bender bytecode file containing the shader. When the file loads, the onLoadComplete() method is called. • onLoadComplete(): In the onLoadComplete() method the code creates the Shader object named shader. It also creates a Sprite instance named texture. In the renderShader() method, the code draws the shader result into texture once per frame. • onEnterFrame(): The onEnterFrame() method is called once per frame, creating the animation effect. In this method, the code sets the shader parameter value to the new color, then calls the renderShader() method to draw the shader result as a rectangle. • renderShader(): In the renderShader() method, the code calls the Graphics.beginShaderFill() method to specify a shader fill. It then draws a rectangle whose fill is defined by the shader output (the generated color) For more information on using a shader in this way, see “Using a shader as a drawing fill” on page 298. The following is the ActionScript code for this example. Use this class as the main application class for an ActionScriptonly project in Flash Builder, or as the document class for the FLA file in Flash Professional: Last updated 3/21/2011 296 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders package { import import import import import import flash.display.Shader; flash.display.Sprite; flash.events.Event; flash.net.URLLoader; flash.net.URLLoaderDataFormat; flash.net.URLRequest; public class ColorFilterExample extends Sprite { private const DELTA_OFFSET:Number = Math.PI * 0.5; private var loader:URLLoader; private var shader:Shader; private var texture:Sprite; private var delta:Number = 0; public function ColorFilterExample() { init(); } private function init():void { loader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest("ColorFilter.pbj")); } private function onLoadComplete(event:Event):void { shader = new Shader(loader.data); texture = new Sprite(); addChild(texture); addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { Last updated 3/21/2011 297 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders shader.data.color.value[0] = 0.5 + Math.cos(delta - DELTA_OFFSET) * 0.5; shader.data.color.value[1] = 0.5 + Math.cos(delta) * 0.5; shader.data.color.value[2] = 0.5 + Math.cos(delta + DELTA_OFFSET) * 0.5; // The alpha channel value (index 3) is set to 1 by the kernel's default // value. This value doesn't need to change. delta += 0.1; renderShader(); } private function renderShader():void { texture:graphics.clear(); texture.graphics.beginShaderFill(shader); texture.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); texture.graphics.endFill(); } } } The following is the source code for the ColorFilter shader kernel, used to create the “ColorFilter.pbj” Pixel Bender bytecode file: kernel ColorFilter < namespace : "boostworthy::Example"; vendor : "Ryan Taylor"; version : 1; description : "Creates an image where every pixel has the specified color value."; > { output pixel4 result; parameter float4 color < minValue:float4(0, 0, 0, 0); maxValue:float4(1, 1, 1, 1); defaultValue:float4(0, 0, 0, 1); >; void evaluatePixel() { result = color; } } If you’re using a shader whose parameters aren’t documented, you can figure out how many elements of what type must be included in the array by checking the ShaderParameter object’s type property. The type property indicates the data type of the parameter as defined in the shader itself. For a list of the number and type of elements expected by each parameter type, see the ShaderParameter.value property listing in the ActionScript 3.0 Reference. Last updated 3/21/2011 298 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders Each ShaderParameter object also has an index property that indicates where the parameter fits in the order of the shader’s parameters. In addition to these properties, a ShaderParameter object can have additional properties containing metadata values provided by the shader’s author. For example, the author can specify metadata values such as minimum, maximum, and default values for a parameter. Any metadata values that the author specifies are added to the ShaderParameter object as dynamic properties. To examine those properties, use a for..in loop to loop over the ShaderParameter object’s dynamic properties to identify its metadata. The following example shows how to use a for..in loop to identify a ShaderParameter object’s metadata. Each metadata value is added to a Vector instance named metadata. Note that this example assumes a Shader instance named myShader is already created, and that it is known to have a parameter named brightness: var brightness:ShaderParameter = myShader.data.brightness; var metadata:Vector. = new Vector.(); for (var prop:String in brightness) { if (brightness[prop] is String) { metadata[metadata.length] = brightness[prop]; } } // do something with the metadata Using a shader Flash Player 10 and later, Adobe AIR 1.5 and later Once a Pixel Bender shader is available in ActionScript as a Shader object, it can be used in several ways: • Shader drawing fill: The shader defines the fill portion of a shape drawn using the drawing api • Blend mode: The shader defines the blend between two overlapping display objects • Filter: The shader defines a filter that modifies the appearance of visual content • Stand-alone shader processing: The shader processing runs without specifying the intended use of the output. The shader can optionally run in the background, with the result is available when the processing completes. This technique can be used to generate bitmap data and also to process non-visual data. Using a shader as a drawing fill Flash Player 10 and later, Adobe AIR 1.5 and later When you use a shader to create a drawing fill, you use the drawing api methods to create a vector shape. The shader’s output is used to fill in the shape, in the same way that any bitmap image can be used as a bitmap fill with the drawing api. To create a shader fill, at the point in your code at which you want to start drawing the shape, call the Graphics object’s beginShaderFill() method. Pass the Shader object as the first argument to the beginShaderFill() method, as shown in this listing: var canvas:Sprite = new Sprite(); canvas.graphics.beginShaderFill(myShader); canvas.graphics.drawRect(10, 10, 150, 150); canvas.graphics.endFill(); // add canvas to the display list to see the result Last updated 3/21/2011 299 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders When you use a shader as a drawing fill, you set any input image values and parameter values that the shader requires. The following example demonstrates using a shader as a drawing fill. In this example, the shader creates a three-point gradient. This gradient has three colors, each at the point of a triangle, with a gradient blend between them. In addition, the colors rotate to create an animated spinning color effect. Note: The code for this example was written by Petri Leskinen. Thank you Petri for sharing this example. You can see more of Petri’s examples and tutorials at http://pixelero.wordpress.com/. The ActionScript code is in three methods: • init(): The init() method is called when the application loads. In this method the code sets the initial values for the Point objects representing the points of the triangle. The also code creates a Sprite instance named canvas. Later, in the updateShaderFill(), the code draws the shader result into canvas once per frame. Finally, the code loads the shader bytecode file. • onLoadComplete(): In the onLoadComplete() method the code creates the Shader object named shader. It also sets the initial parameter values. Finally, the code adds the updateShaderFill() method as a listener for the enterFrame event, meaning that it is called once per frame to create an animation effect. • updateShaderFill(): The updateShaderFill() method is called once per frame, creating the animation effect. In this method, the code calculates and sets the shader parameters’ values. The code then calls the beginShaderFill() method to create a shader fill and calls other drawing api methods to draw the shader result in a triangle. The following is the ActionScript code for this example. Use this class as the main application class for an ActionScriptonly project in Flash Builder, or as the document class for a FLA file in Flash Professional: Last updated 3/21/2011 300 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders package { import import import import import import import flash.display.Shader; flash.display.Sprite; flash.events.Event; flash.geom.Point; flash.net.URLLoader; flash.net.URLLoaderDataFormat; flash.net.URLRequest; public class ThreePointGradient extends Sprite { private var canvas:Sprite; private var shader:Shader; private var loader:URLLoader; private var topMiddle:Point; private var bottomLeft:Point; private var bottomRight:Point; private var colorAngle:Number = 0.0; private const d120:Number = 120 / 180 * Math.PI; // 120 degrees in radians public function ThreePointGradient() { init(); } private function init():void { canvas = new Sprite(); addChild(canvas); var size:int = 400; topMiddle = new Point(size / 2, 10); bottomLeft = new Point(0, size - 10); bottomRight = new Point(size, size - 10); loader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest("ThreePointGradient.pbj")); } private function onLoadComplete(event:Event):void { shader = new Shader(loader.data); shader.data.point1.value = [topMiddle.x, topMiddle.y]; shader.data.point2.value = [bottomLeft.x, bottomLeft.y]; shader.data.point3.value = [bottomRight.x, bottomRight.y]; addEventListener(Event.ENTER_FRAME, updateShaderFill); } private function updateShaderFill(event:Event):void Last updated 3/21/2011 301 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders { colorAngle += .06; var c1:Number = 1 / 3 + 2 / 3 * Math.cos(colorAngle); var c2:Number = 1 / 3 + 2 / 3 * Math.cos(colorAngle + d120); var c3:Number = 1 / 3 + 2 / 3 * Math.cos(colorAngle - d120); shader.data.color1.value = [c1, c2, c3, 1.0]; shader.data.color2.value = [c3, c1, c2, 1.0]; shader.data.color3.value = [c2, c3, c1, 1.0]; canvas.graphics.clear(); canvas.graphics.beginShaderFill(shader); canvas.graphics.moveTo(topMiddle.x, topMiddle.y); canvas.graphics.lineTo(bottomLeft.x, bottomLeft.y); canvas.graphics.lineTo(bottomRight.x, bottomLeft.y); canvas.graphics.endFill(); } } } The following is the source code for the ThreePointGradient shader kernel, used to create the “ThreePointGradient.pbj” Pixel Bender bytecode file: kernel ThreePointGradient < namespace : "Petri Leskinen::Example"; vendor : "Petri Leskinen"; version : 1; description : "Creates a gradient fill using three specified points and colors."; > { parameter float2 point1 // coordinates of the first point < minValue:float2(0, 0); maxValue:float2(4000, 4000); defaultValue:float2(0, 0); >; parameter float4 color1 // color at the first point, opaque red by default < defaultValue:float4(1.0, 0.0, 0.0, 1.0); >; parameter float2 point2 // coordinates of the second point < minValue:float2(0, 0); maxValue:float2(4000, 4000); defaultValue:float2(0, 500); >; parameter float4 color2 // color at the second point, opaque green by default < defaultValue:float4(0.0, 1.0, 0.0, 1.0); Last updated 3/21/2011 302 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders >; parameter float2 point3 // coordinates of the third point < minValue:float2(0, 0); maxValue:float2(4000, 4000); defaultValue:float2(0, 500); >; parameter float4 color3 // color at the third point, opaque blue by default < defaultValue:float4(0.0, 0.0, 1.0, 1.0); >; output pixel4 dst; void evaluatePixel() { float2 d2 = point2 - point1; float2 d3 = point3 - point1; // transformation to a new coordinate system // transforms point 1 to origin, point2 to (1, 0), and point3 to (0, 1) float2x2 mtrx = float2x2(d3.y, -d2.y, -d3.x, d2.x) / (d2.x * d3.y - d3.x * d2.y); float2 pNew = mtrx * (outCoord() - point1); // repeat the edge colors on the outside pNew.xy = clamp(pNew.xy, 0.0, 1.0); // set the range to 0.0 ... 1.0 // interpolating the output color or alpha value dst = mix(mix(color1, color2, pNew.x), color3, pNew.y); } } Note: If you use a shader fill when rendering under the graphics processing unit (GPU), the filled area will be colored cyan. For more information about drawing shapes using the drawing api, see “Using the drawing API” on page 211. Using a shader as a blend mode Flash Player 10 and later, Adobe AIR 1.5 and later Using a shader as a blend mode is like using other blend modes. The shader defines the appearance resulting from two display objects being blended together visually. To use a shader as a blend mode, assign your Shader object to the blendShader property of the foreground display object. Assigning a value other than null to the blendShader property automatically sets the display object’s blendMode property to BlendMode.SHADER. The following listing demonstrates using a shader as a blend mode. Note that this example assumes that there is a display object named foreground contained in the same parent on the display list as other display content, with foreground overlapping the other content: foreground.blendShader = myShader; Last updated 3/21/2011 303 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders When you use a shader as a blend mode, the shader must be defined with at least two inputs. As the example shows, you do not set the input values in your code. Instead, the two blended images are automatically used as shader inputs. The foreground image is set as the second image. (This is the display object to which the blend mode is applied.) A background image is created by taking the composite of all the pixels behind the foreground image’s bounding box. This background image is set as the first input image. If you use a shader that expects more than two inputs, you provide a value for any input beyond the first two. The following example demonstrates using a shader as a blend mode. This example uses a lighten blend mode based on luminosity. The result of the blend is that the lightest pixel value from either of the blended objects becomes the pixel that’s displayed. Note: The code for this example was written by Mario Klingemann. Thank you Mario for sharing this example. You can see more of Mario’s work and read his writing at www.quasimondo.com/. The important ActionScript code is in these two methods: • init(): The init() method is called when the application loads. In this method the code loads the shader bytecode file. • onLoadComplete(): In the onLoadComplete() method the code creates the Shader object named shader. It then draws three objects. The first, backdrop, is a dark gray background behind the blended objects. The second, backgroundShape, is a green gradient ellipse. The third object, foregroundShape, is an orange gradient ellipse. The foregroundShape ellipse is the foreground object of the blend. The background image of the blend is formed by the part of backdrop and the part of backgroundShape that are overlapped by the foregroundShape object’s bounding box. The foregroundShape object is the front-most object in the display list. It partially overlaps backgroundShape and completely overlaps backdrop. Because of this overlap, without a blend mode applied, the orange ellipse (foregroundShape) shows completely and part of the green ellipse (backgroundShape) is hidden by it: However, with the blend mode applied, the brighter part of the green ellipse “shows through” because it is lighter than the portion of foregroundShape that overlaps it: The following is the ActionScript code for this example. Use this class as the main application class for an ActionScriptonly project in Flash Builder, or as the document class for the FLA file in Flash Professional: Last updated 3/21/2011 304 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders package { import import import import import import import import import import import flash.display.BlendMode; flash.display.GradientType; flash.display.Graphics; flash.display.Shader; flash.display.Shape; flash.display.Sprite; flash.events.Event; flash.geom.Matrix; flash.net.URLLoader; flash.net.URLLoaderDataFormat; flash.net.URLRequest; public class LumaLighten extends Sprite { private var shader:Shader; private var loader:URLLoader; public function LumaLighten() { init(); } private function init():void { loader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest("LumaLighten.pbj")); } private function onLoadComplete(event:Event):void { shader = new Shader(loader.data); var backdrop:Shape = new Shape(); var g0:Graphics = backdrop.graphics; g0.beginFill(0x303030); g0.drawRect(0, 0, 400, 200); g0.endFill(); addChild(backdrop); var backgroundShape:Shape = new Shape(); var g1:Graphics = backgroundShape.graphics; var c1:Array = [0x336600, 0x80ff00]; var a1:Array = [255, 255]; var r1:Array = [100, 255]; var m1:Matrix = new Matrix(); m1.createGradientBox(300, 200); g1.beginGradientFill(GradientType.LINEAR, c1, a1, r1, m1); g1.drawEllipse(0, 0, 300, 200); Last updated 3/21/2011 305 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders g1.endFill(); addChild(backgroundShape); var foregroundShape:Shape = new Shape(); var g2:Graphics = foregroundShape.graphics; var c2:Array = [0xff8000, 0x663300]; var a2:Array = [255, 255]; var r2:Array = [100, 255]; var m2:Matrix = new Matrix(); m2.createGradientBox(300, 200); g2.beginGradientFill(GradientType.LINEAR, c2, a2, r2, m2); g2.drawEllipse(100, 0, 300, 200); g2.endFill(); addChild(foregroundShape); foregroundShape.blendShader = shader; foregroundShape.blendMode = BlendMode.SHADER; } } } The following is the source code for the LumaLighten shader kernel, used to create the “LumaLighten.pbj” Pixel Bender bytecode file: kernel LumaLighten < namespace : "com.quasimondo.blendModes"; vendor : "Quasimondo.com"; version : 1; description : "Luminance based lighten blend mode"; > { input image4 background; input image4 foreground; output pixel4 dst; const float3 LUMA = float3(0.212671, 0.715160, 0.072169); void evaluatePixel() { float4 a = sampleNearest(foreground, outCoord()); float4 b = sampleNearest(background, outCoord()); float luma_a = a.r * LUMA.r + a.g * LUMA.g + a.b * LUMA.b; float luma_b = b.r * LUMA.r + b.g * LUMA.g + b.b * LUMA.b; dst = luma_a > luma_b ? a : b; } } For more information on using blend modes, see “Applying blending modes” on page 177. Last updated 3/21/2011 306 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders Note: When a Pixel Bender shader program is run as a blend in Flash Player or AIR, the sampling and outCoord() functions behave differently than in other contexts.In a blend, a sampling function will always return the current pixel being evaluated by the shader. You cannot, for example, use add an offset to outCoord() in order to sample a neighboring pixel. Likewise, if you use the outCoord() function outside a sampling function, its coordinates always evaluate to 0. You cannot, for example, use the position of a pixel to influence how the blended images are combined. Using a shader as a filter Flash Player 10 and later, Adobe AIR 1.5 and later Using a shader as a filter is like using any of the other filters in ActionScript. When you use a shader as a filter, the filtered image (a display object or BitmapData object) is passed to the shader. The shader uses the input image to create the filter output, which is usually a modified version of the original image. If the filtered object is a display object the shader’s output is displayed on the screen in place of the filtered display object. If the filtered object is a BitmapData object, the shader’s output becomes the content of the BitmapData object whose applyFilter() method is called. To use a shader as a filter, you first create the Shader object as described in “Loading or embedding a shader” on page 290. Next you create a ShaderFilter object linked to the Shader object. The ShaderFilter object is the filter that is applied to the filtered object. You apply it to an object in the same way that you apply any filter. You pass it to the filters property of a display object or you call the applyFilter() method on a BitmapData object. For example, the following code creates a ShaderFilter object and applies the filter to a display object named homeButton. var myFilter:ShaderFilter = new ShaderFilter(myShader); homeButton.filters = [myFilter]; When you use a shader as a filter, the shader must be defined with at least one input. As the example shows, you do not set the input value in your code. Instead, the filtered display object or BitmapData object is set as the input image. If you use a shader that expects more than one input, you provide a value for any input beyond the first one. In some cases, a filter changes the dimensions of the original image. For example, a typical drop shadow effect adds extra pixels containing the shadow that’s added to the image. When you use a shader that changes the image dimensions, set the leftExtension, rightExtension, topExtension, and bottomExtension properties to indicate by how much you want the image size to change. The following example demonstrates using a shader as a filter. The filter in this example inverts the red, green, and blue channel values of an image. The result is the “negative” version of the image. Note: The shader that this example uses is the invertRGB.pbk Pixel Bender kernel that is included with the Pixel Bender Toolkit. You can load the source code for the kernel from the Pixel Bender Toolkit installation directory. Compile the source code and save the bytecode file in the same directory as the source code. The important ActionScript code is in these two methods: • init(): The init() method is called when the application loads. In this method the code loads the shader bytecode file. Last updated 3/21/2011 307 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders • onLoadComplete(): In the onLoadComplete() method the code creates the Shader object named shader. It then creates and draws the contents of an object named target. The target object is a rectangle filled with a linear gradient color that is red on the left, yellow-green in the middle, and light blue on the right. The unfiltered object looks like this: With the filter applied the colors are inverted, making the rectangle look like this: The shader that this example uses is the “invertRGB.pbk” sample Pixel Bender kernel that is included with the Pixel Bender Toolkit. The source code is available in the file “invertRGB.pbk” in the Pixel Bender Toolkit installation directory. Compile the source code and save the bytecode file with the name “invertRGB.pbj” in the same directory as your ActionScript source code. The following is the ActionScript code for this example. Use this class as the main application class for an ActionScriptonly project in Flash Builder, or as the document class for the FLA file in Flash Professional: package { import import import import import import import import import import import flash.display.GradientType; flash.display.Graphics; flash.display.Shader; flash.display.Shape; flash.display.Sprite; flash.filters.ShaderFilter; flash.events.Event; flash.geom.Matrix; flash.net.URLLoader; flash.net.URLLoaderDataFormat; flash.net.URLRequest; public class InvertRGB extends Sprite { private var shader:Shader; private var loader:URLLoader; public function InvertRGB() Last updated 3/21/2011 308 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders { init(); } private function init():void { loader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.BINARY; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest("invertRGB.pbj")); } private function onLoadComplete(event:Event):void { shader = new Shader(loader.data); var target:Shape = new Shape(); addChild(target); var g:Graphics = target.graphics; var c:Array = [0x990000, 0x445500, 0x007799]; var a:Array = [255, 255, 255]; var r:Array = [0, 127, 255]; var m:Matrix = new Matrix(); m.createGradientBox(w, h); g.beginGradientFill(GradientType.LINEAR, c, a, r, m); g.drawRect(10, 10, w, h); g.endFill(); var invertFilter:ShaderFilter = new ShaderFilter(shader); target.filters = [invertFilter]; } } } For more information on applying filters, see “Creating and applying filters” on page 256. Using a shader in stand-alone mode Flash Player 10 and later, Adobe AIR 1.5 and later When you use a shader in stand-alone mode, the shader processing runs independent of how you intend to use the output. You specify a shader to execute, set input and parameter values, and designate an object into which the result data is placed. You can use a shader in stand-alone mode for two reasons: • Processing non-image data: In stand-alone mode, you can choose to pass arbitrary binary or number data to the shader rather than bitmap image data. You can choose to have the shader result be returned as binary data or number data in addition to bitmap image data. • Background processing: When you run a shader in stand-alone mode, by default the shader executes asynchronously. This means that the shader runs in the background while your application continues to run, and your code is notified when the shader processing finishes. You can use a shader that takes a long time to run and it doesn’t freeze up the application user interface or other processing while the shader is running. Last updated 3/21/2011 309 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with Pixel Bender shaders You use a ShaderJob object to execute a shader in stand-alone mode. First you create the ShaderJob object and link it to the Shader object representing the shader to execute: var job:ShaderJob = new ShaderJob(myShader); Next, you set any input or parameter values that the shader expects. If you are executing the shader in the background, you also register a listener for the ShaderJob object’s complete event. Your listener is called when the shader finishes its work: function completeHandler(event:ShaderEvent):void { // do something with the shader result } job.addEventListener(ShaderEvent.COMPLETE, completeHandler); Next, you create an object into which the shader operation result is written when the operation finishes. You assign that object to the ShaderJob object’s target property: var jobResult:BitmapData = new BitmapData(100, 75); job.target = jobResult; Assign a BitmapData instance to the target property if you are using the ShaderJob to perform image processing. If you are processing binary or number data, assign a ByteArray object or Vector. instance to the target property. In that case, you must set the ShaderJob object’s width and height properties to specify the amount of data to output to the target object. Note: You can set the ShaderJob object’s shader, target,width, and height properties in one step by passing arguments to the ShaderJob() constructor, like this:var job:ShaderJob = new ShaderJob(myShader, myTarget, myWidth, myHeight); When you are ready to execute the shader, you call the ShaderJob object’s start() method: job.start(); By default calling start() causes the ShaderJob to execute asynchronously. In that case program execution continues immediately with the next line of code rather than waiting for the shader to finish. When the shader operation finishes, the ShaderJob object calls its complete event listeners, notifying them that it is done. At that point (that is, in the body of your complete event listener) the target object contains the shader operation result. Note: Instead of using the target property object, you can retrieve the shader result directly from the event object that’s passed to your listener method. The event object is a ShaderEvent instance. The ShaderEvent object has three properties that can be used to access the result, depending on the data type of the object you set as the target property: ShaderEvent.bitmapData, ShaderEvent.byteArray, and ShaderEvent.vector. Alternatively, you can pass a true argument to the start() method. In that case the shader operation executes synchronously. All code (including interaction with the user interface and any other events) pauses while the shader executes. When the shader finishes, the target object contains the shader result and the program continues with the next line of code. job.start(true); Last updated 3/21/2011 310 Chapter 15: Working with movie clips Flash Player 9 and later, Adobe AIR 1.0 and later The MovieClip class is the core class for animation and movie clip symbols that you create in your Adobe® Flash® development environment. It has all the behaviors and functionality of display objects, but with additional properties and methods for controlling its timeline. Basics of movie clips Flash Player 9 and later, Adobe AIR 1.0 and later Movie clips are a key element for people who create animated content with the Flash authoring tool and want to control that content with ActionScript. Whenever you create a movie clip symbol in Flash, Flash adds the symbol to the library of that Flash document. By default, this symbol becomes an instance of the MovieClip class, and as such has the properties and methods of the MovieClip class. When an instance of a movie clip symbol is placed on the Stage, the movie clip automatically progresses through its timeline (if it has more than one frame) unless its playback is altered using ActionScript. It is this timeline that distinguishes the MovieClip class, allowing you to create animation through motion or shape tweens through the Flash authoring tool. By contrast, with a display object that is an instance of the Sprite class, you can create animation only by programmatically changing the object’s values. In previous versions of ActionScript, the MovieClip class was the base class of all instances on the Stage. In ActionScript 3.0, a movie clip is only one of many display objects that can appear on the screen. If a timeline is not necessary for the function of a display object, using the Shape class or Sprite class in lieu of the MovieClip class may improve rendering performance. For more information on choosing the appropriate display object for a task, see “Choosing a DisplayObject subclass” on page 163. Important concepts and terms The following reference list contains important terms related to movie clips: AVM1 SWF A SWF file created using ActionScript 1.0 or ActionScript 2.0, usually targeting Flash Player 8 or earlier. AVM2 SWF A SWF file created using ActionScript 3.0 for Adobe Flash Player 9 or later or Adobe AIR. External SWF A SWF file that is created separately from the project SWF file and is intended to be loaded into the project SWF file and played back within that SWF file. Frame The smallest division of time on the timeline. As with a motion picture filmstrip, each frame is like a snapshot of the animation in time, and when frames are played quickly in sequence, the effect of animation is created. Timeline The metaphorical representation of the series of frames that make up a movie clip’s animation sequence. The timeline of a MovieClip object is equivalent to the timeline in the Flash authoring tool. Playhead A marker identifying the location (frame) in the timeline that is being displayed at a given moment. Last updated 3/21/2011 311 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips Working with MovieClip objects Flash Player 9 and later, Adobe AIR 1.0 and later When you publish a SWF file, Flash converts all movie clip symbol instances on the Stage to MovieClip objects. You can make a movie clip symbol available to ActionScript by giving it an instance name in the Instance Name field of the Property inspector. When the SWF file is created, Flash generates the code that creates the MovieClip instance on the Stage and declares a variable using the instance name. If you have named movie clips that are nested inside other named movie clips, those child movie clips are treated like properties of the parent movie clip—you can access the child movie clip using dot syntax. For example, if a movie clip with the instance name childClip is nested within another clip with the instance name parentClip, you can make the child clip’s timeline animation play by calling this code: parentClip.childClip.play(); Note: : Children instances placed on the Stage in the Flash authoring tool cannot be accessed by code from within the constructor of a parent instance since they have not been created at that point in code execution. Before accessing the child, the parent must instead either create the child instance by code or delay access to a callback function that listens for the child to dispatch its Event.ADDED_TO_STAGE event. While some legacy methods and properties of the ActionScript 2.0 MovieClip class remain the same, others have changed. All properties prefixed with an underscore have been renamed. For example, _width and _height properties are now accessed as width and height, while _xscale and _yscale are now accessed as scaleX and scaleY. For a complete list of the properties and methods of the MovieClip class, consult the ActionScript 3.0 Reference for the Adobe Flash Platform . Controlling movie clip playback Flash Player 9 and later, Adobe AIR 1.0 and later Flash uses the metaphor of a timeline to convey animation or a change in state. Any visual element that employs a timeline must be either a MovieClip object or extend from the MovieClip class. While ActionScript can direct any movie clip to stop, play, or go to another point on the timeline, it cannot be used to dynamically create a timeline or add content at specific frames; this is only possible using the Flash authoring tool. When a MovieClip is playing, it progresses along its timeline at a speed dictated by the frame rate of the SWF file. Alternatively, you can override this setting by setting the Stage.frameRate property in ActionScript. Playing movie clips and stopping playback Flash Player 9 and later, Adobe AIR 1.0 and later The play() and stop() methods allow basic control of a movie clip across its timeline. For example, suppose you have a movie clip symbol on the Stage which contains an animation of a bicycle moving across the screen, with its instance name set to bicycle. If the following code is attached to a keyframe on the main timeline, bicycle.stop(); the bicycle will not move (its animation will not play). The bicycle’s movement could start through some other user interaction. For example, if you had a button named startButton, the following code on a keyframe on the main timeline would make it so that clicking the button causes the animation to play: Last updated 3/21/2011 312 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips // This function will be called when the button is clicked. It causes the // bicycle animation to play. function playAnimation(event:MouseEvent):void { bicycle.play(); } // Register the function as a listener with the button. startButton.addEventListener(MouseEvent.CLICK, playAnimation); Fast-forwarding and rewinding Flash Player 9 and later, Adobe AIR 1.0 and later The play() and stop() methods are not the only way of controlling playback in a movie clip. You can also move the playhead forward or backward along the timeline manually by using the nextFrame() and prevFrame() methods. Calling either of these methods stops playback and moves the playhead one frame forward or backward, respectively. Using the play() method is analogous to calling nextFrame() every time the movie clip object’s enterFrame event is triggered. Along these lines, you could make the bicycle movie clip play backwards by creating an event listener for the enterFrame event and telling bicycle to go to its previous frame in the listener function, as follows: // This function is called when the enterFrame event is triggered, meaning // it's called once per frame. function everyFrame(event:Event):void { if (bicycle.currentFrame == 1) { bicycle.gotoAndStop(bicycle.totalFrames); } else { bicycle.prevFrame(); } } bicycle.addEventListener(Event.ENTER_FRAME, everyFrame); In normal playback, if a movie clip contains more than a single frame, it will loop indefinitely when playing; that is, it will return to Frame 1 if it progresses past its final frame. When you use prevFrame() or nextFrame(), this behavior does not happen automatically (calling prevFrame() when the playhead is on Frame 1 doesn’t move the playhead to the last frame). The if condition in the example above checks to see if the playhead has progressed backwards to the first frame, and sets the playhead ahead to its final frame, effectively creating a continuous loop of the movie clip playing backwards. Jumping to a different frame and using frame labels Flash Player 9 and later, Adobe AIR 1.0 and later Sending a movie clip to a new frame is a simple affair. Calling either gotoAndPlay() or gotoAndStop() will jump the movie clip to the frame number specified as a parameter. Alternatively, you can pass a string that matches the name of a frame label. Any frame on the timeline can be assigned a label. To do this, select a frame on the timeline and then enter a name in the Frame Label field on the Property inspector. Last updated 3/21/2011 313 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips The advantages of using frame labels instead of numbers are particularly evident when creating a complex movie clip. When the number of frames, layers, and tweens in an animation becomes large, consider labeling important frames with explanatory descriptions that represent shifts in the behavior of the movie clip (for example, “off,” “walking,” or “running”). This improves code readability and also provides flexibility, since ActionScript calls that go to a labeled frame are pointers to a single reference—the label—rather than a specific frame number. If later on you decide to move a particular segment of the animation to a different frame, you will not need to change your ActionScript code as long as you keep the same label for the frames in the new location. To represent frame labels in code, ActionScript 3.0 includes the FrameLabel class. Each instance of this class represents a single frame label, and has a name property representing the name of the frame label as specified in the Property inspector, and a frame property representing the frame number of the frame where the label is placed on the timeline. In order to get access to the FrameLabel instances associated with a movie clip instance, the MovieClip class includes two properties that directly return FrameLabel objects. The currentLabels property returns an array that consists of all FrameLabel objects across the entire timeline of a movie clip. The currentLabel property returns a string containing the name of the frame label encountered most recently along the timeline. Suppose you were creating a movie clip named robot and had labeled the various states of its animation. You could set up a condition that checks the currentLabel property to access the current state of robot, as in the following code: if (robot.currentLabel == "walking") { // do something } Working with scenes Flash Player 9 and later, Adobe AIR 1.0 and later In the Flash authoring environment, you can use scenes to demarcate a series of timelines that a SWF file will progress through. Using the second parameter of the gotoAndPlay() or gotoAndStop() methods, you can specify a scene to send the playhead to. All FLA files start with only the initial scene, but you can create new scenes. Using scenes is not always the best approach because scenes have a number of drawbacks. A Flash document that contains multiple scenes can be difficult to maintain, particularly in multiauthor environments. Multiple scenes can also be inefficient in bandwidth, because the publishing process merges all scenes into a single timeline. This causes a progressive download of all scenes, even if they are never played. For these reasons, use of multiple scenes is often discouraged except for organizing lengthy multiple timeline-based animations. The scenes property of the MovieClip class returns an array of Scene objects representing all the scenes in the SWF file. The currentScene property returns a Scene object that represents the scene that is currently playing. The Scene class has several properties that give information about a scene. The labels property returns an array of FrameLabel objects representing the frame labels in that scene. The name property returns the scene’s name as a string. The numFrames property returns an int representing the total number of frames in the scene. Last updated 3/21/2011 314 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips Creating MovieClip objects with ActionScript Flash Player 9 and later, Adobe AIR 1.0 and later One way of adding content to the screen in Flash is by dragging assets from the library onto the Stage, but that is not the only workflow. For complex projects, experienced developers commonly prefer to create movie clips programatically. This approach brings several advantages: easier re-use of code, faster compile-time speed, and more sophisticated modifications that are available only to ActionScript. The display list API of ActionScript 3.0 streamlines the process of dynamically creating MovieClip objects. The ability to instantiate a MovieClip instance directly, separate from the process of adding it to the display list, provides flexibility and simplicity without sacrificing control. In ActionScript 3.0, when you create a movie clip (or any other display object) instance programatically, it is not visible on the screen until it is added to the display list by calling the addChild() or the addChildAt() method on a display object container. This allows you to create a movie clip, set its properties, and even call methods before it is rendered to the screen. For more information on working with the display list, see “Working with display object containers” on page 151. Exporting library symbols for ActionScript Flash Player 9 and later, Adobe AIR 1.0 and later By default, instances of movie clip symbols in a Flash document’s library cannot be dynamically created (that is, created using only ActionScript). This is because each symbol that is exported for use in ActionScript adds to the size of your SWF file, and it’s recognized that some symbols might not be intended for use on the stage. For this reason, in order to make a symbol available in ActionScript, you must specify that the symbol should be exported for ActionScript. To export a symbol for ActionScript: 1 Select the symbol in the Library panel and open its Symbol Properties dialog box. 2 If necessary, activate the Advanced settings. 3 In the Linkage section, activate the Export for ActionScript checkbox. This will activate the Class and Base Class fields. By default, the Class field is populated with the symbol name, with spaces removed (for example, a symbol named “Tree House” would become “TreeHouse”). To specify that the symbol should use a custom class for its behavior, enter the full name of the class including its package in this field. If you want to be able to create instances of the symbol in ActionScript, but don’t need to add any additional behavior, you can leave the class name as-is. The Base Class field’s value defaults to flash.display.MovieClip. If you want your symbol to extend the functionality of another customer class, you can specify that class’s name instead, as long as that class extends the Sprite (or MovieClip) class. 4 Press the OK button to save the changes. At this point, if Flash can’t find an external ActionScript file with a definition for the specified class (for instance, if you didn’t need to add additional behavior for the symbol), a warning is displayed: A definition for this class could not be found in the classpath, so one will be automatically generated in the SWF file upon export. Last updated 3/21/2011 315 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips You can disregard this warning if your library symbol does not require unique functionality beyond the functionality of the MovieClip class. If you do not provide a class for your symbol, Flash will create a class for your symbol equivalent to this one: package { import flash.display.MovieClip; public class ExampleMovieClip extends MovieClip { public function ExampleMovieClip() { } } } If you do want to add extra ActionScript functionality to your symbol, add the appropriate properties and methods to the code structure below. For example, suppose you have a movie clip symbol containing a circle of 50 pixels width and 50 pixels height, and the symbol is specified to be exported for ActionScript with a class named Circle. The following code, when placed in a Circle.as file, extends the MovieClip class and provides the symbol with the additional methods getArea() and getCircumference(): package { import flash.display.MovieClip; public class Circle extends MovieClip { public function Circle() { } public function getArea():Number { // The formula is Pi times the radius squared. return Math.PI * Math.pow((width / 2), 2); } public function getCircumference():Number { // The formula is Pi times the diameter. return Math.PI * width; } } } The following code, placed on a keyframe on Frame 1 of the Flash document, will create an instance of the symbol and display it on the screen: var c:Circle = new Circle(); addChild(c); trace(c.width); trace(c.height); trace(c.getArea()); trace(c.getCircumference()); Last updated 3/21/2011 316 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips This code demonstrates ActionScript-based instantiation as an alternative to dragging individual assets onto the Stage. It creates a circle that has all of the properties of a movie clip and also has the custom methods defined in the Circle class. This is a very basic example—your library symbol can specify any number of properties and methods in its class. ActionScript-based instantiation is powerful, because it allows you to dynamically create large quantities of instances that would be tedious to arrange manually. It is also flexible, because you can customize each instance’s properties as it is created. You can get a sense of both of these benefits by using a loop to dynamically create several Circle instances. With the Circle symbol and class described previously in your Flash document’s library, place the following code on a keyframe on Frame 1: import flash.geom.ColorTransform; var totalCircles:uint = 10; var i:uint; for (i = 0; i < totalCircles; i++) { // Create a new Circle instance. var c:Circle = new Circle(); // Place the new Circle at an x coordinate that will space the circles // evenly across the Stage. c.x = (stage.stageWidth / totalCircles) * i; // Place the Circle instance at the vertical center of the Stage. c.y = stage.stageHeight / 2; // Change the Circle instance to a random color c.transform.colorTransform = getRandomColor(); // Add the Circle instance to the current timeline. addChild(c); } function getRandomColor():ColorTransform { // Generate random values for the red, green, and blue color channels. var red:Number = (Math.random() * 512) - 255; var green:Number = (Math.random() * 512) - 255; var blue:Number = (Math.random() * 512) - 255; // Create and return a ColorTransform object with the random colors. return new ColorTransform(1, 1, 1, 1, red, green, blue, 0); } This demonstrates how you can create and customize multiple instances of a symbol quickly using code. Each instance is positioned based on the current count within the loop, and each instance is given a random color by setting its transform property (which Circle inherits by extending the MovieClip class). Loading an external SWF file Flash Player 9 and later, Adobe AIR 1.0 and later In ActionScript 3.0, SWF files are loaded using the Loader class. To load an external SWF file, your ActionScript needs to do four things: 1 Create a new URLRequest object with the url of the file. 2 Create a new Loader object. Last updated 3/21/2011 317 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips 3 Call the Loader object’s load() method, passing the URLRequest instance as a parameter. 4 Call the addChild() method on a display object container (such as the main timeline of a Flash document) to add the Loader instance to the display list. Ultimately, the code looks like this: var request:URLRequest = new URLRequest("http://www.[yourdomain].com/externalSwf.swf"); var loader:Loader = new Loader() loader.load(request); addChild(loader); This same code can be used to load an external image file such as a JPEG, GIF, or PNG image, by specifying the image file’s url rather than a SWF file’s url. A SWF file, unlike an image file, may contain ActionScript. Thus, although the process of loading a SWF file may be identical to loading an image, when loading an external SWF file both the SWF file doing the loading and the SWF file being loaded must reside in the same security sandbox if Flash Player or AIR is playing the SWF and you plan to use ActionScript to communicate in any way to the external SWF file. Additionally, if the external SWF file contains classes that share the same namespace as classes in the loading SWF file, you may need to create a new application domain for the loaded SWF file in order to avoid namespace conflicts. For more information on security and application domain considerations, see “Working with application domains” on page 139 and “Loading content” on page 1024. When the external SWF file is successfully loaded, it can be accessed through the Loader.content property. If the external SWF file is published for ActionScript 3.0, this will be either a movie clip or a sprite, depending on which class it extends. Considerations for loading an older SWF file Flash Player 9 and later, Adobe AIR 1.0 and later If the external SWF file has been published with an older version of ActionScript, there are important limitations to consider. Unlike an ActionScript 3.0 SWF file that runs in AVM2 (ActionScript Virtual Machine 2), a SWF file published for ActionScript 1.0 or 2.0 runs in AVM1 (ActionScript Virtual Machine 1). There are important differences when loading an ActionScript 1.0 or 2.0 SWF file into an ActionScript 3.0 SWF file (compared to loading an ActionScript 3.0 SWF file). Flash Player provides full backward compatibility with previously published content. Any content that runs in previous versions of Flash Player runs in Flash Player versions that support ActionScript 3.0. However, the following limitations apply: • ActionScript 3.0 code can load a SWF file written in ActionScript 1.0 or 2.0. When an ActionScript 1.0 or 2.0 SWF file is successfully loaded, the loaded object (the Loader.content property) is an AVM1Movie object. An AVM1Movie instance is not the same as a MovieClip instance. It is a display object, but unlike a movie clip, it does not include timeline-related methods or properties. The parent AVM2 SWF file cannot access the properties, methods, or objects of the loaded AVM1Movie object. • SWF files written in ActionScript 1.0 or 2.0 cannot load SWF files written in ActionScript 3.0. This means that SWF files authored in Flash 8 or Flex Builder 1.5 or earlier versions cannot load ActionScript 3.0 SWF files. The only exception to this rule is that an ActionScript 2.0 SWF file can replace itself with an ActionScript 3.0 SWF file, as long as the ActionScript 2.0 SWF file hasn't previously loaded anything into any of its levels. An ActionScript 2.0 SWF file can do this through a call to loadMovieNum(), passing a value of 0 to the level parameter. • In general, SWF files written in ActionScript 1.0 or 2.0 must be migrated if they are to work together with SWF files written in ActionScript 3.0. For example, suppose you created a media player using ActionScript 2.0. The media player loads various content that was also created using ActionScript 2.0. You cannot create new content in ActionScript 3.0 and load it in the media player. You must migrate the video player to ActionScript 3.0. Last updated 3/21/2011 318 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips If, however, you create a media player in ActionScript 3.0, that media player can perform simple loads of your ActionScript 2.0 content. The following tables summarize the limitations of previous versions of Flash Player in relation to loading newer content and executing code, as well as the limitations for cross-scripting between SWF files written in different versions of ActionScript. Supported functionality Flash Player 7 Flash Player 8 Flash Player 9 and 10 Can load SWFs published for 7 and earlier 8 and earlier 9 (or 10) and earlier Contains this AVM AVM1 AVM1 AVM1 and AVM2 Runs SWFs written in ActionScript 1.0 and 2.0 1.0 and 2.0 1.0 and 2.0, and 3.0 In the following table, “Supported functionality” refers to content running in Flash Player 9 or later. Content running in Flash Player 8 or earlier can load, display, execute, and cross-script only ActionScript 1.0 and 2.0. Supported functionality Content created in ActionScript 1.0 and 2.0 Content created in ActionScript 3.0 Can load content and execute code in content created in ActionScript 1.0 and 2.0 only Can cross script content created in ActionScript 1.0 and 2.0 only (ActionScript 3.0 ActionScript 1.0 and 2.0 through through Local Connection) LocalConnection. ActionScript 1.0 and 2.0, and ActionScript 3.0 ActionScript 3.0 Movie clip example: RuntimeAssetsExplorer Flash Player 9 and later, Adobe AIR 1.0 and later The Export for ActionScript functionality can be especially advantageous for libraries that may be useful across more than one project. If Flash Player or AIR executes a SWF file, symbols that have been exported to ActionScript are available to any SWF file within the same security sandbox as the SWF that loads it. In this way, a single Flash document can generate a SWF file that is designated for the sole purpose of holding graphical assets. This technique is particularly useful for larger projects where designers working on visual assets can work in parallel with developers who create a “wrapper” SWF file that then loads the graphical assets SWF file at run time. You can use this method to maintain a series of versioned files where graphical assets are not dependent upon the progress of programming development. The RuntimeAssetsExplorer application loads any SWF file that is a subclass of RuntimeAsset and allows you to browse the available assets of that SWF file. The example illustrates the following: • Loading an external SWF file using Loader.load() • Dynamic creation of a library symbol exported for ActionScript • ActionScript control of MovieClip playback Before beginning, note that each of the SWF files to run in Flash Player must be located in the same security sandbox. For more information, see “Security sandboxes” on page 1010. To get the application files for this sample, download the Flash Professional Samples. The RuntimeAssetsExplorer application files can be found in the folder Samples/RuntimeAssetsExplorer. The application consists of the following files: Last updated 3/21/2011 319 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips File Description RuntimeAssetsExample.mxml The user interface for the application for Flex (MXML) or Flash (FLA). or RuntimeAssetsExample.fla RuntimeAssetsExample.as Document class for the Flash (FLA) application. GeometricAssets.as An example class that implements the RuntimeAsset interface. GeometricAssets.fla A FLA file linked to the GeometricAssets class (the document class of the FLA) containing symbols that are exported for ActionScript. com/example/programmingas3/runtimeassetexplorer/RuntimeLibrary.as An interface that defines the required methods expected of all run-time asset SWF files that will be loaded into the explorer container. com/example/programmingas3/runtimeassetexplorer/AnimatingBox.as The class of the library symbol in the shape of a rotating box. com/example/programmingas3/runtimeassetexplorer/AnimatingStar.as The class of the library symbol in the shape of a rotating star. Establishing a run-time library interface Flash Player 9 and later, Adobe AIR 1.0 and later In order for the explorer to properly interact with a SWF library, the structure of the run-time asset libraries must be formalized. We will accomplish this by creating an interface, which is similar to a class in that it’s a blueprint of methods that demarcate an expected structure, but unlike a class it includes no method bodies. The interface provides a way for both the run-time library and the explorer to communicate to one another. Each SWF of run-time assets that is loaded in our browser will implement this interface. For more information about interfaces and how they can be useful, see Interfaces in Learning ActionScript 3.0. The RuntimeLibrary interface will be very simple—we merely require a function that can provide the explorer with an array of classpaths for the symbols to be exported and available in the run-time library. To this end, the interface has a single method: getAssets(). package com.example.programmingas3.runtimeassetexplorer { public interface RuntimeLibrary { function getAssets():Array; } } Creating the asset library SWF file Flash Player 9 and later, Adobe AIR 1.0 and later By defining the RuntimeLibrary interface, it’s possible to create multiple asset library SWF files that can be loaded into another SWF file. Making an individual SWF library of assets involves four tasks: • Creating a class for the asset library SWF file • Creating classes for individual assets contained in the library Last updated 3/21/2011 320 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips • Creating the actual graphic assets • Associating graphic elements with classes and publishing the library SWF Creating a class to implement the RuntimeLibrary interface Next, we’ll create the GeometricAssets class that will implement the RuntimeLibrary interface. This will be the document class of the FLA. The code for this class is very similar to the RuntimeLibrary interface—the difference between them is that in the class definition the getAssets() method has a method body. package { import flash.display.Sprite; import com.example.programmingas3.runtimeassetexplorer.RuntimeLibrary; public class GeometricAssets extends Sprite implements RuntimeLibrary { public function GeometricAssets() { } public function getAssets():Array { return [ "com.example.programmingas3.runtimeassetexplorer.AnimatingBox", "com.example.programmingas3.runtimeassetexplorer.AnimatingStar" ]; } } } If we were to create a second run-time library, we could create another FLA based upon another class (for example, AnimationAssets) that provides its own getAssets() implementation. Creating classes for each MovieClip asset For this example, we’ll merely extend the MovieClip class without adding any functionality to the custom assets. The following code for AnimatingStar is analogous to that of AnimatingBox: package com.example.programmingas3.runtimeassetexplorer { import flash.display.MovieClip; public class AnimatingStar extends MovieClip { public function AnimatingStar() { } } } Publishing the library We’ll now connect the MovieClip-based assets to the new class by creating a new FLA and entering GeometricAssets into the Document Class field of the Property inspector. For the purposes of this example, we’ll create two very basic shapes that use a timeline tween to make one clockwise rotation over 360 frames. Both the animatingBox and animatingStar symbols are set to Export for ActionScript and have the Class field set to the respective classpaths specified in the getAssets() implementation. The default base class of flash.display.MovieClip remains, as we want to subclass the standard MovieClip methods. After setting up your symbol’s export settings, publish the FLA. You now have your first run-time library. This SWF file could be loaded into another AVM2 SWF file and the AnimatingBox and AnimatingStar symbols would be available to the new SWF file. Last updated 3/21/2011 321 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with movie clips Loading the library into another SWF file Flash Player 9 and later, Adobe AIR 1.0 and later The last functional piece to deal with is the user interface for the asset explorer. In this example, the path to the runtime library is hard-coded as a variable named ASSETS_PATH. Alternatively, you could use the FileReference class—for example, to create an interface that browses for a particular SWF file on your hard drive. When the run-time library is successfully loaded, Flash Player calls the runtimeAssetsLoadComplete() method: private function runtimeAssetsLoadComplete(event:Event):void { var rl:* = event.target.content; var assetList:Array = rl.getAssets(); populateDropdown(assetList); stage.frameRate = 60; } In this method, the variable rl represents the loaded SWF file. The code calls the getAssets() method of the loaded SWF file, obtaining the list of assets that are available, and uses them to populate a ComboBox component with a list of available assets by calling the populateDropDown() method. That method in turn stores the full classpath of each asset. Clicking the Add button on the user interface triggers the addAsset() method: private { var var var ... } function addAsset():void className:String = assetNameCbo.selectedItem.data; AssetClass:Class = getDefinitionByName(className) as Class; mc:MovieClip = new AssetClass(); which gets the classpath of whichever asset is currently selected in the ComboBox (assetNameCbo.selectedItem.data), and uses the getDefinitionByName() function (from the flash.utils package) to obtain an actual reference to the asset’s class in order to create a new instance of that asset. Last updated 3/21/2011 322 Chapter 16: Working with inverse kinematics Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later Inverse kinematics (IK) is a great technique for creating realistic motion. IK lets you create coordinated movements within a chain of connected parts called an IK armature, so that the parts move together in a lifelike way. The parts of the armature are its bones and joints. Given the end point of the armature, IK calculates the angles for the joints that are required to reach that end point. Calculating those angles manually yourself would be challenging. The beauty of this feature is that you can create armatures interactively using Adobe® Flash® Professional. Then animate them using ActionScript. The IK engine included with Flash Professional performs the calculations to describe the movement of the armature. You can limit the movement to certain parameters in your ActionScript code. New to the Flash Professional CS5 version of IK is the concept of bone spring, typically associated with high-end animation applications. Used with the new dynamic Physics Engine, this feature lets you configure life-like movement. And, this effect is visible both at runtime and during authoring. To create inverse kinematics armatures, you must have a license for Flash Professional. More Help topics fl.ik package Basics of Inverse Kinematics Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later Inverse kinematics (IK) lets you create life-like animation by linking parts so they move in relation to one another in a realistic manner. For example, using IK you can move a leg to a certain position by articulating the movements of the joints in the leg required to achieve the desired pose. IK uses a framework of bones chained together in a structure called an IK armature. The fl.ik package helps you create animations resembling natural motion. It lets you animate multiple IK armatures seamlessly without having to know a lot about the physics behind the IK algorithms. Create the IK armature with its ancillary bones and joints with Flash Professional. Then you can access the IK classes to animate them at runtime. See the Using inverse kinematics section in Using Flash Professional for detailed instructions on how to create an IK armature. Important concepts and terms The following reference list contains important terms that are relevant to this feature: Armature A kinematic chain, consisting of bones and joints, used in computer animation to simulate realistic motion. Bone A rigid segment in an armature, analogous to a bone in an animal skeleton. Last updated 3/21/2011 323 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics Inverse Kinematics (IK) Process of determining the parameters of a jointed flexible object called a kinematic chain or armature. Joint The location at which two bones make contact, constructed to enable movement of the bones; analogous to a joint in an animal. Physics Engine A package of physics-related algorithms used to provide life-like actions to animation. Spring The quality of a bone that moves and reacts when the parent bone is moved and then incrementally diminishes over time. Animating IK Armatures Overview Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later After creating an IK armature in Flash Professional, use the fl.ik classes to limit its movement, track its events, and animate it at runtime. The following figure shows a movie clip named Wheel. The axle is an instance of an IKArmature named Axle. The IKMover class moves the armature in synchronization with the rotation of wheel. The IKBone, ikBone2, in the armature is attached to the wheel at its tail joint. A B C A. Wheel B. Axle C. ikBone2 Last updated 3/21/2011 324 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics At runtime, the wheel spins in association with the __motion_Wheel motion tween discussed in Describing the animation. An IKMover object initiates and controls the movement of the axle. The following figure shows two snapshots of the axle armature attached to the spinning wheel at different frames in the rotation. At runtime, the following ActionScript: • Gets information about the armature and its components • Instantiates an IKMover object • Moves the axle in conjunction with the rotation of the wheel import fl.ik.* var var var var tree:IKArmature = IKManager.getArmatureByName("Axle"); bone:IKBone = tree.getBoneByName("ikBone2"); endEffector:IKJoint = bone.tailJoint; pos:Point = endEffector.position; var ik:IKMover = new IKMover(endEffector, pos); ik.limitByDistance = true; ik.distanceLimit = 0.1; ik.limitByIteration = true; ik.iterationLimit = 10; Wheel.addEventListener(Event.ENTER_FRAME, frameFunc); function frameFunc(event:Event) { if (Wheel != null) { var mat:Matrix = Wheel.transform.matrix; var pt = new Point(90, 0); pt = mat.transformPoint(pt); ik.moveTo(pt); } } Last updated 3/21/2011 325 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics The IK classes used to move the axle are: • IKArmature: describes the armature, a tree structure consisting of bones and joints; must be created with Flash Professional • IKManager: container class for all the IK armatures in the document; must be created with Flash Professional • IKBone: a segment of an IK armature • IKJoint: a connection between two IK bones • IKMover: initiates and controls IK movement of armatures For complete and detailed descriptions of these classes, see the ik package. Getting information about an IK armature Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later First, declare variables for the armature, the bone, and the joint that make up the parts that you want to move. The following code uses the getArmatureByName() method of the IKManager class to assign the value of the Axle armature to the IKArmature variable tree. The Axle armature was previously created with Flash Professional. var tree:IKArmature = IKManager.getArmatureByName("Axle"); Similarly, the following code uses the getBoneByName() method of the IKArmature class to assign to the IKBone variable the value of the ikBone2 bone. var bone:IKBone = tree.getBoneByName("ikBone2"); The tail joint of the ikBone2 bone is the part of the armature that attaches to the spinning wheel. The following line declares the variable endEffector and assigns to it the tailjoint property of the ikBone2 bone: var endEffector:IKJoint = home.tailjoint; The variable pos is a point that stores the current position of the endEffector joint. var pos:Point = endEffector.position; In this example, pos is the position of the joint at the end of the axle where it connects to the wheel. The original value of this variable is obtained from the position property of the IKJoint. Instantiating an IK Mover and Limiting Its Movement Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later An instance of the IKMover class moves the axle. The following line instantiates the IKMover object ik, passing to its constructor the element to move and the starting point for the movement: var ik:IKMover = new IKMover(endEffector, pos); The properties of the IKMover class let you limit the movement of an armature. You can limit movement based on the distance, iterations, and time of the movement. Last updated 3/21/2011 326 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics The following pairs of properties enforce these limits. The pairs consist of a Boolean value that indicates whether the movement is limited and an integer that specifies the limit: Boolean property Integer property Limit set limitByDistance:Boolean distanceLimit:int Sets the maximum distance in pixels that the IK engine moves for each iteration. limitByIteration:Boolean iterationLimit:int Sets the maximum number of iterations the IK engine performs for each movement. limitByTime:Boolean timeLimit:int Sets the maximum time in milliseconds allotted to the IK engine to perform the movement. By default, all the Boolean values are set to false, so movement is not limited unless you explicitly set a Boolean value to true. To enforce a limit, set the appropriate property to true and then specify a value for the corresponding integer property. If you set the limit to a value without setting its corresponding Boolean property, the limit is ignored. In this case, the IK engine continues to move the object until another limit or the target position of the IKMover is reached. In the following example, the maximum distance of the armature movement is set to 0.1 pixels per iteration. The maximum number of iterations for every movement is set to ten. ik.limitByDistance = true; ik.distanceLimit = 0.1; ik.limitByIteration = true; ik.iterationLimit = 10; Moving an IK Armature Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later The IKMover moves the axle inside the event listener for the wheel. On each enterFrame event of the wheel, a new target position for the armature is calculated. Using its moveTo() method, the IKMover moves the tail joint to its target position or as far as it can within the constraints set by its limitByDistance, limitByIteration, and limitByTime properties. Wheel.addEventListener(Event.ENTER_FRAME, frameFunc); function frameFunc(event:Event) { if (Wheel != null) { var mat:Matrix = Wheel.transform.matrix; var pt = new Point(90,0); pt = mat.transformPoint(pt); ik.moveTo(pt); } } Last updated 3/21/2011 327 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics Using Springs Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS5 or later Inverse kinematics in Flash Professional CS5 supports bone spring. Bone spring can be set during authoring, and bone spring attributes can be added or modified at runtime. Spring is a property of a bone and its joints. It has two attributes: IKJoint.springStrength, which sets the amount of spring, and IKJoint.springDamping, which adds resistance to the strength value and changes the rate of decay of the spring. Spring strength is a percent value from the default 0 (completely rigid) to 100 (very loose and controlled by physics). Bones with spring react to the movement of their joint. If no other translation (rotation, x, or y) is enabled, the spring settings have no effect. Spring damping is a percent value from the default 0 (no resistance) to 100 (heavily damped). Damping changes the amount of time between a bone’s initial movement and its return to a rest position. You can check to see if springs are enabled for an IKArmature object by checking its IKArmature.springsEnabled property. The other spring properties and methods belong to individual IKJoint objects. A joint can be enabled for angular rotation and translation along the x- and y-axes. You can position a rotational joint’s spring angle with IKJoint.setSpringAngle and a translational joint’s spring position with IKJoint.setSpringPt. This example selects a bone by name and identifies its tailJoint. The code tests the parent armature to see if springs are enabled and then sets spring properties for the joint. var arm:IKArmature = IKManager.getArmatureAt(0); var bone:IKBone = arm.getBoneByName("c"); var joint:IKJoint = bone.tailJoint; if (arm.springsEnabled) { joint.springStrength = 50; //medium spring strength joint.springDamping = 10; //light damping resistance if (joint.hasSpringAngle) { joint.setSpringAngle(30); //set angle for rotational spring } } Using IK Events Flash Player 10 and later, Adobe AIR 1.5 and later, requires Flash CS4 or later The IKEvent class lets you create an event object that contains information about IK Events. IKEvent information describes motion that has terminated because the specified time, distance, or iteration limit was exceeded. The following code shows an event listener and handler for tracking time limit events. This event handler reports on the time, distance, iteration count, and joint properties of an event that fires when the time limit of the IKMover is exceeded. Last updated 3/21/2011 328 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with inverse kinematics var ikmover:IKMover = new IKMover(endjoint, pos); ikMover.limitByTime = true; ikMover.timeLimit = 1000; ikmover.addEventListener(IKEvent.TIME_LIMIT, timeLimitFunction); function timeLimitFunction(evt:IKEvent):void { trace("timeLimit hit"); trace("time is " + evt.time); trace("distance is " + evt.distance); trace("iterationCount is " + evt.iterationCount); trace("IKJoint is " + evt.joint.name); } Last updated 3/21/2011 329 Chapter 17: Working in three dimensions (3D) Flash Player 10 and later, Adobe AIR 1.5 and later Basics of 3D Flash Player 10 and later, Adobe AIR 1.5 and later The main difference between a two-dimensional (2D) object and a three-dimensional (3D) object projected on a twodimensional screen is the addition of a third dimension to the object. The third dimension allows the object to move toward and away from viewpoint of the user. When you explicitly set the z property of a display object to a numeric value, the object automatically creates a 3D transformation matrix. You can alter this matrix to modify the 3D transformation settings of that object. In addition, 3D rotation differs from 2D rotation. In 2D the axis of rotation is always perpendicular to the x/y plane in other words, on the z-axis. In 3D the axis of rotation can be around any of the x, y, or z axes. Setting the rotation and scaling properties of a display object enable it to move in 3D space. Important concepts and terms The following reference list contains important terms that you will encounter when programming 3-dimensional graphics: Perspective In a 2D plane, representation of parallel lines as converging on a vanishing point to give the illusion of depth and distance. Projection The production of a 2D image of a higher-dimensional object; 3D projection maps 3D points to a 2D plane. Rotation Changing the orientation (and often the position) of an object by moving every point included in the object in a circular motion. Transformation Altering 3D points or sets of points by translation, rotation, scale, skew, or a combination of these actions. Translation Changing the position of an object by moving every point included in the object by the same amount in the same direction. Vanishing point Point at which receding parallel lines seem to meet when represented in linear perspective. Vector A 3D vector represents a point or a location in the three-dimensional space using the Cartesian coordinates x, y, and z. Vertex A corner point. Textured mesh Any point defining an object in 3D space. UV mapping A way to apply a texture or bitmap to a 3D surface. UV mapping assigns values to coordinates on an image as percentages of the horizontal (U) axis and vertical (V) axis. Last updated 3/21/2011 330 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) T value The scaling factor for determining the size of a 3D object as the object moves toward, or away from, the current point of view. Culling Rendering, or not, surfaces with specific winding. Using culling you can hide surfaces that are not visible to the current point of view. Understanding the 3D features of Flash Player and the AIR runtime Flash Player 10 and later, Adobe AIR 1.5 and later In Flash Player versions prior to Flash Player 10 and Adobe AIR versions prior to Adobe AIR 1.5, display objects have two properties, x and y, for positioning them on a 2D plane. Starting with Flash Player 10 and Adobe AIR 1.5, every ActionScript display object has a z property that lets you position it along the z-axis, which is generally used to indicate depth or distance. Flash Player 10 and Adobe AIR 1.5 introduced support for 3D effects. However, display objects are inherently flat. Each display object, such as a MovieClip object or a Sprite object, ultimately renders itself in two dimensions, on a single plane. The 3D features let you place, move, rotate, and otherwise transform these planar objects in all three dimensions. They also let you manage 3D points and convert them to 2D x, y coordinates, so you can project 3D objects onto a 2D view. You can simulate many kinds of 3D experiences using these features. The 3D coordinate system used by ActionScript differs from other systems. When you use 2D coordinates in ActionScript, the value of x increases as you move to the right along the x-axis, and the value of y increases as you move down along the y-axis. The 3D coordinate system retains those conventions and adds a z-axis whose value increases as you move away from the viewpoint. A (0,0,0) C B D The positive directions of the x, y, and z axes in the ActionScript 3D coordinate system. A. + Z axis B. Origin C. +X axis D. +Y axis Note: Be aware that Flash Player and AIR always represent 3D in layers. This means that if object A is in front of object B on the display list, Flash Player or AIR always renders A in front of B regardless of the z-axis values of the two objects. To resolve this conflict between the display list order and the z-axis order, use the transform.getRelativeMatrix3D() method to save and then re-order the layers of 3D display objects. For more information, see “Using Matrix3D objects for reordering display” on page 340. Last updated 3/21/2011 331 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) The following ActionScript classes support the new 3D-related features: 1 The flash.display.DisplayObject class contains the z property and new rotation and scaling properties for manipulating display objects in 3D space. The DisplayObject.local3DToGlobal() method offers a simple way to project 3D geometry onto a 2D plane. 2 The flash.geom.Vector3D class can be used as a data structure for managing 3D points. It also supports vector mathematics. 3 The flash.geom.Matrix3D class supports complex transformations of 3D geometry, such as rotation, scaling, and translation. 4 The flash.geom.PerspectiveProjection class controls the parameters for mapping 3D geometry onto a 2D view. There are two different approaches to simulating 3D images in ActionScript: 1 Arranging and animating planar objects in 3D space. This approach involves animating display objects using the x, y and z properties of display objects, or setting rotation and scaling properties using the DisplayObject class. More complex motion can be achieved using the DisplayObject.transform.matrix3D object. The DisplayObject.transform.perspectiveProjection object customizes how the display objects are drawn in 3D perspective. Use this approach when you want to animate 3D objects that consist primarily of planes. Examples of this approach include 3D image galleries or 2D animation objects arranged in 3D space. 2 Generating 2D triangles from 3D geometry, and rendering those triangles with textures. To use this approach you must first define and manage data about 3D objects and then convert that data into 2D triangles for rendering. Bitmap textures can be mapped to these triangles, and then the triangles are drawn to a graphics object using the Graphics.drawTriangles() method. Examples of this approach include loading 3D model data from a file and rendering the model on the screen, or generating and drawing 3D terrain as triangle meshes. Creating and moving 3D objects Flash Player 10 and later, Adobe AIR 1.5 and later To convert a 2D display object into a 3D display object, you can explicitly set its z property to a numeric value. When you assign a value to the z property, a new Transform object is created for the display object. Setting the DisplayObject.rotationX or DisplayObject.rotationY properties also creates a new Transform object. The Transform object contains a Matrix3D property that governs how the display object is represented in 3D space. The following code sets the coordinates for a display object called “leaf”: leaf.x = 100; leaf.y = 50; leaf.z = -30; You can see these values, as well as properties derived from these values, in the matrix3D property of the Transform object of the leaf: var leafMatrix:Matrix3D = leaf.transform.matrix3D; trace(leafMatrix.position.x); trace(leafMatrix.position.y); trace(leafMatrix.position.z); trace(leafMatrix.position.length); trace(leafMatrix.position.lengthSquared); For information about the properties of the Transform object, see the Transformclass. For information about the properties of the Matrix3D object, see the Matrix3D class. Last updated 3/21/2011 332 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) Moving an object in 3D space Flash Player 10 and later, Adobe AIR 1.5 and later You can move an object in 3D space by changing the values of its x, y, or z properties. When you change the value of the z property the object appears to move closer or farther away from the viewer. The following code moves two ellipses back and forth along their z-axes by changing the value of their z properties in response to an event. ellipse2 moves faster than ellipse1: its z property is increased by a multiple of 20 on each Frame event while the z property of ellipse1 is increased by a multiple of 10: var depth:int = 1000; function ellipse1FrameHandler(e:Event):void { ellipse1Back = setDepth(e, ellipse1Back); e.currentTarget.z += ellipse1Back * 10; } function ellipse2FrameHandler(e:Event):void { ellipse2Back = setDepth(e, ellipse1Back); e.currentTarget.z += ellipse1Back * 20; } function setDepth(e:Event, d:int):int { if(e.currentTarget.z > depth) { e.currentTarget.z = depth; d = -1; } else if (e.currentTarget.z < 0) { e.currentTarget.z = 0; d = 1; } } Rotating an object in 3D space Flash Player 10 and later, Adobe AIR 1.5 and later You can rotate an object in three different ways, depending on how you set the object’s rotation properties: rotationX, rotationY, and rotationZ. The figure below shows two squares that are not rotated: The next figure shows the two squares when you increment the rotationY property of the container of the squares to rotate them on the y- axis. Rotating the container, or parent display object, of the two squares rotates both squares: Last updated 3/21/2011 333 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) container.rotationY += 10; The next figure shows what happens when you set the rotationX property of the container for the squares. This rotates the squares on the x- axis. The next figure shows what happens when you increment the rotationZ property of the container of the squares.This rotates the squares on the z-axis. A display object can simultaneously move and rotate in 3D space. Projecting 3D objects onto a 2D view Flash Player 10 and later, Adobe AIR 1.5 and later The PerspectiveProjection class in the flash.geom package provides a simple way of applying rudimentary perspective when moving display objects through 3D space. If you do not explicitly create a perspective projection for your 3D space, the 3D engine uses a default PerspectiveProjection object that exists on the root and is propagated to all its children. The three properties that define how a PerspectiveProjection object displays 3D space are: • fieldOfView • projectionCenter • focalLength Modifying the value of the fieldOfView automatically modifies the value of the focalLength and vice-versa, since they are interdependent. The formula used to calculate the focalLength given the fieldOfView value is: Last updated 3/21/2011 334 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) focalLength = stageWidth/2 * (cos(fieldOfView/2) / sin(fieldOfView/2) Typically you would modify the fieldOfView property explicitly. Field of view Flash Player 10 and later, Adobe AIR 1.5 and later By manipulating the fieldOfView property of the PerspectiveProjection class, you can make a 3D display object approaching the viewer appear larger and an object receding from the viewer appear smaller. The fieldOfView property specifies an angle between 0 and 180 degrees that determines the strength of the perspective projection. The greater the value, the stronger the distortion applied to a display object moving along its z-axis. A low fieldOfView value results in very little scaling and causes objects to appear to move only slightly back in space. A high fieldOfView value causes more distortion and the appearance of greater movement. The maximum value of 179.9999... degrees results in an extreme fish-eye camera lens effect. The maximum value of fieldOfView is 179.9999... and the minimum is 0.00001... Exactly 0 and 180 are illegal values. Projection center Flash Player 10 and later, Adobe AIR 1.5 and later The projectionCenter property represents the vanishing point in the perspective projection. It is applied as an offset to the default registration point (0,0) in the upper-left corner of the stage. As an object appears to move further from the viewer, it skews towards the vanishing point and eventually vanishes. Imagine an infinitely long hall. As you look down the hall, the edges of the walls converge to a vanishing point far down the hall. If the vanishing point is at the center of the stage, the hall disappears towards a point in the center. The default value for the projectionCenter property is the center of the stage. If, for example, you want elements to appear on the left of the stage and a 3D area to appear on the right, set the projectionCenter to a point on the right of the stage to make that the vanishing point of your 3D viewing area. Focal length Flash Player 10 and later, Adobe AIR 1.5 and later The focalLength property represents the distance between the origin of the viewpoint (0,0,0) and the location of the display object on its z-axis. A long focal length is like a telephoto lens with a narrow view and compressed distances between objects. A short focal length is like a wide angle lens, with which you get a wide view with a lot of distortion. A medium focal length approximates what the human eye sees. Typically the focalLength is re-calculated dynamically during perspective transformation as the display object moves, but you can set it explicitly. Last updated 3/21/2011 335 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) Default perspective projection values Flash Player 10 and later, Adobe AIR 1.5 and later The default PerspectiveProjection object created on the root has the following values: • fieldOfView: 55 • perspectiveCenter: stagewidth/2, stageHeight/2 • focalLength: stageWidth/ 2 * ( cos(fieldOfView/2) / sin(fieldOfView/2) ) These are the values that are used if you do not create your own PerspectiveProjection object. You can instantiate your own PerspectiveProjection object with the intention of modifying the projectionCenter and fieldOfView yourself. In this case, the default values of the newly created object are the following, based on a default stage size of 500 by 500: • fieldOfView: 55 • perspectiveCenter: 250,250 • focalLength: 480.24554443359375 Example: Perspective projection Flash Player 10 and later, Adobe AIR 1.5 and later The following example demonstrates the use of perspective projection to create 3D space. It shows how you can modify the vanishing point and change the perspective projection of the space through the projectionCenter property. This modification forces the recalculation of the focalLength and fieldOfView with its concomitant distortion of the 3D space. This example: 1 Creates a sprite named center, as a circle with cross hairs 2 Assigns the coordinates of the center sprite to the projectionCenter property of the perspectiveProjection property of the transform property of the root 3 Adds event listeners for mouse events that call handlers that modify the projectionCenter so that it follows the location of the center object 4 Creates four accordion-style boxes that form the walls of the perspective space When you test this example, ProjectionDragger.swf, drag the circle around to different locations. The vanishing point follows the circle, landing wherever you drop it. Watch the boxes that enclose the space stretch and become distorted when you move the projection center far from the center of the stage. To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The ProjectionDragger application files are in the Samples/ProjectionDragger folder. Last updated 3/21/2011 336 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) package { import flash.display.Sprite; import flash.display.Shape; import flash.geom.Point; import flash.events.*; public class ProjectionDragger extends Sprite { private var center : Sprite; private var boxPanel:Shape; private var inDrag:Boolean = false; public function ProjectionDragger():void { createBoxes(); createCenter(); } public function createCenter():void { var centerRadius:int = 20; center = new Sprite(); // circle center.graphics.lineStyle(1, 0x000099); center.graphics.beginFill(0xCCCCCC, 0.5); center.graphics.drawCircle(0, 0, centerRadius); center.graphics.endFill(); // cross hairs center.graphics.moveTo(0, centerRadius); center.graphics.lineTo(0, -centerRadius); center.graphics.moveTo(centerRadius, 0); center.graphics.lineTo(-centerRadius, 0); center.x = 175; center.y = 175; center.z = 0; this.addChild(center); center.addEventListener(MouseEvent.MOUSE_DOWN, startDragProjectionCenter); center.addEventListener(MouseEvent.MOUSE_UP, stopDragProjectionCenter); center.addEventListener( MouseEvent.MOUSE_MOVE, doDragProjectionCenter); root.transform.perspectiveProjection.projectionCenter = new Point(center.x, center.y); } public function createBoxes():void { // createBoxPanel(); var boxWidth:int = 50; var boxHeight:int = 50; var numLayers:int = 12; var depthPerLayer:int = 50; // var boxVec:Vector. = new Vector.(numLayers); for (var i:int = 0; i < numLayers; i++) { this.addChild(createBox(150, 50, (numLayers - i) * depthPerLayer, boxWidth, boxHeight, 0xCCCCFF)); Last updated 3/21/2011 337 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) this.addChild(createBox(50, 150, (numLayers - i) * depthPerLayer, boxWidth, boxHeight, 0xFFCCCC)); this.addChild(createBox(250, 150, (numLayers - i) * depthPerLayer, boxWidth, boxHeight, 0xCCFFCC)); this.addChild(createBox(150, 250, (numLayers - i) * depthPerLayer, boxWidth, boxHeight, 0xDDDDDD)); } } public function createBox(xPos:int = 0, yPos:int = 0, zPos:int = 100, w:int = 50, h:int = 50, color:int = 0xDDDDDD):Shape { var box:Shape = new Shape(); box.graphics.lineStyle(2, 0x666666); box.graphics.beginFill(color, 1.0); box.graphics.drawRect(0, 0, w, h); box.graphics.endFill(); box.x = xPos; box.y = yPos; box.z = zPos; return box; } public function startDragProjectionCenter(e:Event) { center.startDrag(); inDrag = true; } public function doDragProjectionCenter(e:Event) { if (inDrag) { root.transform.perspectiveProjection.projectionCenter = new Point(center.x, center.y); } } public function stopDragProjectionCenter(e:Event) { center.stopDrag(); root.transform.perspectiveProjection.projectionCenter = new Point(center.x, center.y); inDrag = false; } } } For more complex perspective projection, use the Matrix3D class. Last updated 3/21/2011 338 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) Performing complex 3D transformations Flash Player 10 and later, Adobe AIR 1.5 and later The Matrix3D class lets you transform 3D points within a coordinate space or map 3D points from one coordinate space to another. You don't need to understand matrix mathematics to use the Matrix3D class. Most of the common transformation operations can be handled using the methods of the class. You don't have to worry about explicitly setting or calculating the values of each element in the matrix. After you set the z property of a display object to a numeric value, you can retrieve the object’s transformation matrix using the Matrix3D property of the display object’s Transform object: var leafMatrix:Matrix3D = this.transform.matrix3D; You can use the methods of the Matrix3D object to perform translation, rotation, scaling, and perspective projection on the display object. Use the Vector3D class with its x, y, and z properties for managing 3D points. It can also represent a spatial vector in physics, which has a direction and a magnitude. The methods of the Vector3D class let you perform common calculations with spatial vectors, such as addition, dot product, and cross product calculations. Note: The Vector3D class is not related to the ActionScript Vector class. The Vector3D class contains properties and methods for defining and manipulating 3D points, while the Vector class supports arrays of typed objects. Creating Matrix3D objects Flash Player 10 and later, Adobe AIR 1.5 and later There are three main ways of creating or retrieving Matrix3D objects: 1 Use the Matrix3D() constructor method to instantiate a new matrix. The Matrix3D() constructor takes a Vector object containing 16 numeric values and places each value into a cell of the matrix. For example: var rotateMatrix:Matrix3D = new Matrix3D(1,0,0,1, 0,1,0,1, 0,0,1,1, 0,0,0,1); 2 Set the value the z property of a display object. Then retrieve the transformation matrix from the transform.matrix3D property of that object. 3 Retrieve the Matrix3D object that controls the display of 3D objects on the stage by getting the value of the perspectiveProjection.matrix3D property of the root display object. Applying multiple 3D transformations Flash Player 10 and later, Adobe AIR 1.5 and later You can apply many 3D transformations at once using a Matrix3D object. For example if you wanted to rotate, scale, and then move a cube, you could apply three separate transformations to each point of the cube. However it is much more efficient to precalculate multiple transformations in one Matrix3D object and then perform one matrix transformation on each of the points. Note: The order in which matrix transformations are applied is important. Matrix calculations are not commutative. For example, applying a rotation followed by a translation gives a different result than applying the same translation followed by the same rotation. Last updated 3/21/2011 339 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) The following example shows two ways of performing multiple 3D transformations. package { import import import import flash.display.Sprite; flash.display.Shape; flash.display.Graphics; flash.geom.*; public class Matrix3DTransformsExample extends Sprite { private var rect1:Shape; private var rect2:Shape; public function Matrix3DTransformsExample():void { var pp:PerspectiveProjection = this.transform.perspectiveProjection; pp.projectionCenter = new Point(275,200); this.transform.perspectiveProjection = pp; rect1 = new Shape(); rect1.x = -70; rect1.y = -40; rect1.z = 0; rect1.graphics.beginFill(0xFF8800); rect1.graphics.drawRect(0,0,50,80); rect1.graphics.endFill(); addChild(rect1); rect2 = new Shape(); rect2.x = 20; rect2.y = -40; rect2.z = 0; rect2.graphics.beginFill(0xFF0088); rect2.graphics.drawRect(0,0,50,80); rect2.graphics.endFill(); addChild(rect2); doTransforms(); } private function doTransforms():void { rect1.rotationX = 15; rect1.scaleX = 1.2; rect1.x += 100; rect1.y += 50; rect1.rotationZ = 10; var matrix:Matrix3D = rect2.transform.matrix3D; matrix.appendRotation(15, Vector3D.X_AXIS); matrix.appendScale(1.2, 1, 1); matrix.appendTranslation(100, 50, 0); matrix.appendRotation(10, Vector3D.Z_AXIS); rect2.transform.matrix3D = matrix; } } } Last updated 3/21/2011 340 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) In the doTransforms() method the first block of code uses the DisplayObject properties to change the rotation, scaling, and position of a rectangle shape. The second block of code uses the methods of the Matrix3D class to do the same transformations. The main advantage of using the Matrix3D methods is that all of the calculations are performed in the matrix first,. Then they are applied to the display object only once, when its transform.matrix3D property is set. Setting DisplayObject properties make the source code a bit simpler to read. However each time a rotation or scaling property is set, it causes multiple calculations and changes multiple display object properties. If your code will apply the same complex transformations to display objects more than once, save the Matrix3D object as a variable and then reapply it over and over. Using Matrix3D objects for reordering display Flash Player 10 and later, Adobe AIR 1.5 and later As mentioned previously, the layering order of display objects in the display list determines the display layering order, regardless of their relative z-axes. If your animation transforms the properties of display objects into an order that differs from the display list order, the viewer might see display object layering that does not correspond to the z-axis layering. So, an object that should appear further away from the viewer might appear in front of an object that is closer to the viewer. To ensure that the layering of 3D display objects corresponds to the relative depths of the objects, use an approach like the following: 1 Use the getRelativeMatrix3D() method of the Transform object to get the relative z-axes of the child 3D display objects. 2 Use the removeChild() method to remove the objects from the display list. 3 Sort the display objects based on their relative z-axis values. 4 Use the addChild() method to add the children back to the display list in reverse order. This reordering ensures that your objects display in accordance with their relative z-axes. The following code enforces the correct display of the six faces of a 3D box. It reorders the faces of the box after rotations have been applied to the it: public var faces:Array; . . . public function ReorderChildren() { for(var ind:uint = 0; ind < 6; ind++) { faces[ind].z = faces[ind].child.transform.getRelativeMatrix3D(root).position.z; this.removeChild(faces[ind].child); } faces.sortOn("z", Array.NUMERIC | Array.DESCENDING); for (ind = 0; ind < 6; ind++) { this.addChild(faces[ind].child); } } To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The application files are in the Samples/ReorderByZ folder. Last updated 3/21/2011 341 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) Using triangles for 3D effects Flash Player 10 and later, Adobe AIR 1.5 and later In ActionScript, you perform bitmap transformations using the Graphics.drawTriangles() method, because 3D models are represented by a collection of triangles in space. (However, Flash Player and AIR do not support a depth buffer, so display objects are still inherently flat, or 2D. This is described in “Understanding the 3D features of Flash Player and the AIR runtime” on page 330.) The Graphics.drawTriangles() method is like the Graphics.drawPath() method, as it takes a set of coordinates to draw a triangle path. To familiarize yourself with using Graphics.drawPath(), see “Drawing Paths” on page 225. The Graphics.drawTriangles() method uses a Vector. to specify the point locations for the triangle path: drawTriangles(vertices:Vector., indices:Vector. = null, uvtData:Vector. = null, culling:String = "none"):void The first parameter of drawTriangles() is the only required parameter: the vertices parameter. This parameter is a vector of numbers defining the coordinates through which your triangles are drawn. Every three sets of coordinates (six numbers) represents a triangle path. Without the indices parameter, the length of the vector should always be a factor of six, since each triangle requires three coordinate pairs (three sets of two x/y values). For example: graphics.beginFill(0xFF8000); graphics.drawTriangles( Vector.([ 10,10, 100,10, 10,100, 110,10, 110,100, 20,100])); Neither of these triangles share any points, but if they did, the second drawTriangles() parameter, indices, could be used to reuse values in the vertices vector for more than one triangle. When using the indices parameter, be aware that the indices values are point indices, not indices that relate directly to the vertices array elements. In other words, an index in the vertices vector as defined by indices is actually the real index divided by 2. For the third point of a vertices vector, for example, use an indices value of 2, even though the first numeric value of that point starts at the vector index of 4. For example, merge two triangles to share the diagonal edge using the indices parameter: graphics.beginFill(0xFF8000); graphics.drawTriangles( Vector.([10,10, 100,10, 10,100, 100,100]), Vector.([0,1,2, 1,3,2])); Notice that though a square has now been drawn using two triangles, only four points were specified in the vertices vector. Using indices, the two points shared by the two triangles are reused for each triangle. This reduces the overall vertices count from 6 (12 numbers) to 4 (8 numbers): Last updated 3/21/2011 342 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) 0 1 2 3 A square drawn with two triangles using the vertices parameter This technique becomes useful with larger triangle meshes where most points are shared by multiple triangles. All fills can be applied to triangles. The fills are applied to the resulting triangle mesh as they would to any other shape. Transforming bitmaps Flash Player 10 and later, Adobe AIR 1.5 and later Bitmap transformations provide the illusion of perspective or "texture" on a three-dimensional object. Specifically, you can distort a bitmap toward a vanishing point so the image appears to shrink as it moves toward the vanishing point. Or, you can use a two-dimensional bitmap to create a surface for a three-dimensional object, providing the illusion of texture or “wrapping” on that three-dimensional object. A two-dimensional surface using a vanishing point and a three-dimensional object wrapped with a bitmap. UV mapping Flash Player 10 and later, Adobe AIR 1.5 and later Once you start working with textures, you'll want to make use of the uvtData parameter of drawTriangles(). This parameter allows you to set up UV mapping for bitmap fills. UV mapping is a method for texturing objects. It relies on two values, a U horizontal (x) value and a V vertical (y) value. Rather than being based on pixel values, they are based on percentages. 0 U and 0 V is the upper-left of an image and 1 U and 1 V is the lower-right: Last updated 3/21/2011 343 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) The UV 0 and 1 locations on a bitmap image Vectors of a triangle can be given UV coordinates to associate themselves with the respective locations on an image: The UV coordinates of a triangular area of a bitmap image The UV values stay consistent with the points of the triangle: The vertices of the triangle move and the bitmap distorts to keep the UV values for an individual point the same As ActionScript 3D transformations are applied to the triangle associated with the bitmap, the bitmap image is applied to the triangle based on the UV values. So, instead of using matrix calculations, set or adjust the UV values to create a three-dimensional effect. The Graphics.drawTriangles() method also accepts an optional piece of information for three-dimensional transformations: the T value. The T value in uvtData represents the 3D perspective, or more specifically, the scale factor of the associated vertex. UVT mapping adds perspective correction to UV mapping. For example, if an object is positioned in 3D space away from the viewpoint so that it appears to be 50% its “original” size, the T value of that object would be 0.5. Since triangles are drawn to represent objects in 3D space, their locations along the z-axis determine their T values. The equation that determines the T value is: T = focalLength/(focalLength + z); In this equation, focalLength represents a focal length or calculated "screen" location which dictates the amount of perspective provided in the view. Last updated 3/21/2011 344 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) A B D C E The focal length and z value A. viewpoint B. screen C. 3D object D. focalLength value E. z value The value of T is used to scale basic shapes to make them seem further in the distance. It is usually the value used to convert 3D points to 2D points. In the case of UVT data, it is also used to scale a bitmap between the points within a triangle with perspective. When you define UVT values, the T value directly follows the UV values defined for a vertex. With the inclusion of T, every three values in the uvtData parameter (U, V, and T) match up with every two values in the vertices parameter (x, and y). With UV values alone, uvtData.length == vertices.length. With the inclusion of a T value, uvtData.length = 1.5*vertices.length. The following example shows a plane being rotated in 3D space using UVT data. This example uses an image called ocean.jpg and a “helper” class, ImageLoader, to load the ocean.jpg image so it can be assigned to the BitmapData object. Here is the ImageLoader class source (save this code into a file named ImageLoader.as): Last updated 3/21/2011 345 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) package { import flash.display.* import flash.events.*; import flash.net.URLRequest; public class ImageLoader extends Sprite { public var url:String; public var bitmap:Bitmap; public function ImageLoader(loc:String = null) { if (loc != null){ url = loc; loadImage(); } } public function loadImage():void{ if (url != null){ var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError); var req:URLRequest = new URLRequest(url); loader.load(req); } } private function onComplete(event:Event):void { var loader:Loader = Loader(event.target.loader); var info:LoaderInfo = LoaderInfo(loader.contentLoaderInfo); this.bitmap = info.content as Bitmap; this.dispatchEvent(new Event(Event.COMPLETE)); } private function onIoError(event:IOErrorEvent):void { trace("onIoError: " + event); } } } And here is the ActionScript that uses triangles, UV mapping, and T values to make the image appear as if it is shrinking toward a vanishing point and rotating. Save this code in a file named Spinning3dOcean.as: Last updated 3/21/2011 346 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) package { import flash.display.* import flash.events.*; import flash.utils.getTimer; public class Spinning3dOcean extends Sprite { // plane vertex coordinates (and t values) var x1:Number = -100,y1:Number = -100,z1:Number = 0,t1:Number = 0; var x2:Number = 100,y2:Number = -100,z2:Number = 0,t2:Number = 0; var x3:Number = 100,y3:Number = 100,z3:Number = 0,t3:Number = 0; var x4:Number = -100,y4:Number = 100,z4:Number = 0,t4:Number = 0; var focalLength:Number = 200; // 2 triangles for 1 plane, indices will always be the same var indices:Vector.; var container:Sprite; var bitmapData:BitmapData; // texture var imageLoader:ImageLoader; public function Spinning3dOcean():void { indices = new Vector.(); indices.push(0,1,3, 1,2,3); container = new Sprite(); // container to draw triangles in container.x = 200; container.y = 200; addChild(container); imageLoader = new ImageLoader("ocean.jpg"); imageLoader.addEventListener(Event.COMPLETE, onImageLoaded); } function onImageLoaded(event:Event):void { bitmapData = imageLoader.bitmap.bitmapData; // animate every frame addEventListener(Event.ENTER_FRAME, rotatePlane); } function rotatePlane(event:Event):void { // rotate vertices over time var ticker = getTimer()/400; z2 = z3 = -(z1 = z4 = 100*Math.sin(ticker)); x2 = x3 = -(x1 = x4 = 100*Math.cos(ticker)); // calculate t values Last updated 3/21/2011 347 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) t1 t2 t3 t4 = = = = focalLength/(focalLength focalLength/(focalLength focalLength/(focalLength focalLength/(focalLength + + + + z1); z2); z3); z4); // determine triangle vertices based on t values var vertices:Vector. = new Vector.(); vertices.push(x1*t1,y1*t1, x2*t2,y2*t2, x3*t3,y3*t3, x4*t4,y4*t4); // set T values allowing perspective to change // as each vertex moves around in z space var uvtData:Vector. = new Vector.(); uvtData.push(0,0,t1, 1,0,t2, 1,1,t3, 0,1,t4); // draw container.graphics.clear(); container.graphics.beginBitmapFill(bitmapData); container.graphics.drawTriangles(vertices, indices, uvtData); } } } To test this example, save these two class files in the same directory as an image named “ocean.jpg”. You can see how the original bitmap is transformed to appear as if it is vanishing in the distance and rotating in 3D space. Culling Flash Player 10 and later, Adobe AIR 1.5 and later Culling is the process that determines which surfaces of a three-dimensional object the renderer should not render because they are hidden from the current viewpoint. In 3D space, the surface on the “back” of a three-dimensional object is hidden from the viewpoint: A B C The back of a 3D object is hidden from the viewpoint. A. viewpoint B. 3D object C. the back of a three dimensional object Inherently all triangles are always rendered no matter their size, shape, or position. Culling insures that Flash Player or AIR renders your 3D object correctly. In addition, to save on rendering cycles, sometimes you want some triangles to be skipped by the render. Consider a cube rotating in space. At any given time, you'll never see more than three sides of that cube since the sides not in view would be facing the other direction on the other side of the cube. Since those sides are not going to be seen, the renderer shouldn't draw them. Without culling, Flash Player or AIR renders both the front and back sides. Last updated 3/21/2011 348 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working in three dimensions (3D) A cube has sides not visible from the current viewpoint So, the Graphics.drawTriangles() method has a fourth parameter to establish a culling value: public function drawTriangles(vertices:Vector., indices:Vector. = null, uvtData:Vector. = null, culling:String = "none"):void The culling parameter is a value from the TriangleCulling enumeration class: TriangleCulling.NONE, TriangleCulling.POSITIVE, and TriangleCulling.NEGATIVE. These values are dependent upon the direction of the triangle path defining the surface of the object. The ActionScript API for determining the culling assumes that all out-facing triangles of a 3D shape are drawn with the same path direction. Once a triangle face is turned around, the path direction also changes. At that point, the triangle can be culled (removed from rendering). So, a TriangleCulling value of POSITIVE removes triangles with positive path direction (clockwise). A TriangleCulling value of NEGATIVE removes triangles with a negative (counterclockwise) path direction. In the case of a cube, while the front facing surfaces have a positive path direction, the back facing surfaces have a negative path direction: A cube “unwrapped” to show the path direction. When “wrapped”, the back side path direction is reversed. To see how culling works, start with the earlier example from “UV mapping” on page 342, set the culling parameter of the drawTriangles() method to TriangleCulling.NEGATIVE: container.graphics.drawTriangles(vertices, indices, uvtData, TriangleCulling.NEGATIVE); Notice the “back” side of the image is not rendered as the object rotates. Last updated 3/21/2011 349 Chapter 18: Basics of Working with text Flash Player 9 and later, Adobe AIR 1.0 and later To display text on the screen in Adobe® Flash® Player or Adobe® AIR™, use an instance of the TextField class or use the Flash Text Engine classes. These classes allow you to create, display, and format text. Alternatively, you can use the Text Layout Framework (TLF) — a component library based on the Flash Text Engine classes, but designed for ease of use. You can establish specific content for text fields, or designate the source for the text, and then set the appearance of that text. You can also respond to user events as the user inputs text or clicks a hypertext link. Both the TextField class and the Flash Text Engine classes allow you to display and manage text in Flash Player and AIR.You can use the TextField class to create text objects for display and input. The TextField class provides the basis for other text-based components, such as TextArea and TextInput. You can use the TextFormat class to set character and paragraph formatting for TextField objects and you can apply Cascading Style Sheets (CSS) using the Textfield.styleSheet property and the StyleSheet class. You can assign HTML-formatted text, which can contain embedded media (movie clips, SWF files, GIF files, PNG files, and JPEG files), directly to a text field. The Flash Text Engine, available starting with Flash Player 10 and Adobe AIR 1.5, provides low-level support for sophisticated control of text metrics, formatting, and bi-directional text. It also offers improved text flow and enhanced language support. While you can use the Flash Text Engine to create and manage text elements, it is primarily designed as the foundation for creating text-handling components and requires greater programming expertise.The Text Layout Framework, which includes a text-handling component based on the Flash Text Engine, provides an easier way to use the advanced features of the new text engine. The Text Layout Framework is an extensible library built entirely in ActionScript 3.0. You can use the existing TLF component, or use the framework to build your own text component. For more information on these topics, see: • “Using the TextField class” on page 351 • “Using the Flash Text Engine” on page 375 • “Using the Text Layout Framework” on page 404 Important concepts and terms The following reference list contains important terms involved with text handling: Cascading style sheets A standard syntax for specifying styles and formatting for content that’s structured in XML (or HTML) format. Device font A font that is installed on the user’s machine. Dynamic text field A text field whose contents can be changed by ActionScript but not by user input. Embedded font A font that has its character outline data stored in the application SWF file. HTML text Text content entered into a text field using ActionScript that includes HTML formatting tags along with actual text content. Input text field A text field whose contents can be changed either by user input or by ActionScript. Kerning An adjustment of the spacing between pairs of characters to make the spacing in words more proportional and the text easier to read. Static text field A text field created in the authoring tool, whose content cannot change when the SWF file is running. Last updated 3/21/2011 350 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Basics of Working with text Text line metrics Measurements of the size of various parts of the text content in a text field, such as the baseline of the text, the height of the top of the characters, size of descenders (the part of some lowercase letters that extends below the baseline), and so on. Tracking An adjustment of spacing between groups of letters or blocks of text to increase or decrease the density and make the text more readable. Last updated 3/21/2011 351 Chapter 19: Using the TextField class Flash Player 9 and later, Adobe AIR 1.0 and later You can use an instance of the TextField class to display text or create a text input field on the screen in Adobe® Flash® Player or Adobe® AIR™. The TextField class is the basis for other text-based components, such as the TextArea components or the TextInput components. Text field content can be pre-specified in the SWF file, loaded from a text file or database, or entered by a user interacting with your application. Within a text field, the text can appear as rendered HTML content, with images embedded in the rendered HTML. After you create an instance of a text field, you can use flash.text classes, such as TextFormat and StyleSheet, to control the appearance of the text. The flash.text package contains nearly all the classes related to creating, managing, and formatting text in ActionScript. You can format text by defining the formatting with a TextFormat object and assigning that object to the text field. If your text field contains HTML text, you can apply a StyleSheet object to the text field to assign styles to specific pieces of the text field content. The TextFormat object or StyleSheet object contains properties defining the appearance of the text, such as color, size, and weight. The TextFormat object assigns the properties to all the content within a text field or to a range of text. For example, within the same text field, one sentence can be bold red text and the next sentence can be blue italic text. In addition to the classes in the flash.text package, you can use the flash.events.TextEvent class to respond to user actions related to text. More Help topics “Assigning text formats” on page 358 “Displaying HTML text” on page 353 “Applying cascading style sheets” on page 358 Displaying text Flash Player 9 and later, Adobe AIR 1.0 and later Although authoring tools like Adobe Flash Builder and Flash Professional provide several options for displaying text, including text-related components or text tools, the simplest way to display text programmatically is through a text field. Types of text Flash Player 9 and later, Adobe AIR 1.0 and later The type of text within a text field is characterized by its source: • Dynamic text Dynamic text includes content that is loaded from an external source, such as a text file, an XML file, or even a remote web service. Last updated 3/21/2011 352 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class • Input text Input text is any text entered by a user or dynamic text that a user can edit. You can set up a style sheet to format input text, or use the flash.text.TextFormat class to assign properties to the text field for the input content. For more information, see “Capturing text input” on page 356. • Static text Static text is created through Flash Professional only. You cannot create a static text instance using ActionScript 3.0. However, you can use ActionScript classes like StaticText and TextSnapshot to manipulate an existing static text instance. For more information, see “Working with static text” on page 364. Modifying the text field contents Flash Player 9 and later, Adobe AIR 1.0 and later You can define dynamic text by assigning a string to the flash.text.TextField.text property. You assign a string directly to the property, as follows: myTextField.text = "Hello World"; You can also assign the text property a value from a variable defined in your script, as in the following example: package { import flash.display.Sprite; import flash.text.*; public class TextWithImage extends Sprite { private var myTextBox:TextField = new TextField(); private var myText:String = "Hello World"; public function TextWithImage() { addChild(myTextBox); myTextBox.text = myText; } } } Alternatively, you can assign the text property a value from a remote variable. You have three options for loading text values from remote sources: • The flash.net.URLLoader and flash.net.URLRequest classes load variables for the text from a local or remote location. • The FlashVars attribute is embedded in the HTML page hosting the SWF file and can contain values for text variables. • The flash.net.SharedObject class manages persistent storage of values. For more information, see “Storing local data” on page 668. Last updated 3/21/2011 353 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class Displaying HTML text Flash Player 9 and later, Adobe AIR 1.0 and later The flash.text.TextField class has an htmlText property that you can use to identify your text string as one containing HTML tags for formatting the content. As in the following example, you must assign your string value to the htmlText property (not the text property) for Flash Player or AIR to render the text as HTML: var myText:String = "

    This is some content to render as HTML text.

    "; myTextBox.htmlText = myText; Flash Player and AIR support a subset of HTML tags and entities for the htmlText property. The flash.text.TextField.htmlText property description in the ActionScript 3.0 Reference provides detailed information about the supported HTML tags and entities. Once you designate your content using the htmlText property, you can use style sheets or the textformat tag to manage the formatting of your content. For more information, see “Formatting text” on page 358. Using images in text fields Flash Player 9 and later, Adobe AIR 1.0 and later Another advantage to displaying your content as HTML text is that you can include images in the text field. You can reference an image, local or remote, using the img tag and have it appear within the associated text field. The following example creates a text field named myTextBox and includes a JPG image of an eye, stored in the same directory as the SWF file, within the displayed text: package { import flash.display.Sprite; import flash.text.*; public class TextWithImage extends Sprite { private var myTextBox:TextField; private var myText:String = "

    This is some content to test and see

    what can be rendered.

    You should see an eye image and some HTML text.

    "; public function TextWithImage() { myTextBox.width = 200; myTextBox.height = 200; myTextBox.multiline = true; myTextBox.wordWrap = true; myTextBox.border = true; addChild(myTextBox); myTextBox.htmlText = myText; } } } The img tag supports JPEG, GIF, PNG, and SWF files. Last updated 3/21/2011 354 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class Scrolling text in a text field Flash Player 9 and later, Adobe AIR 1.0 and later In many cases, your text can be longer than the text field displaying the text. Or you may have an input field that allows a user to input more text than can be displayed at one time. You can use the scroll-related properties of the flash.text.TextField class to manage lengthy content, either vertically or horizontally. The scroll-related properties include TextField.scrollV, TextField.scrollH and maxScrollV and maxScrollH. Use these properties to respond to events, like a mouse click or a keypress. The following example creates a text field that is a set size and contains more text than the field can display at one time. As the user clicks the text field, the text scrolls vertically. package { import flash.display.Sprite; import flash.text.*; import flash.events.MouseEvent; public class TextScrollExample extends Sprite { private var myTextBox:TextField = new TextField(); private var myText:String = "Hello world and welcome to the show. It's really nice to meet you. Take your coat off and stay a while. OK, show is over. Hope you had fun. You can go home now. Don't forget to tip your waiter. There are mints in the bowl by the door. Thank you. Please come again."; public function TextScrollExample() { myTextBox.text = myText; myTextBox.width = 200; myTextBox.height = 50; myTextBox.multiline = true; myTextBox.wordWrap = true; myTextBox.background = true; myTextBox.border = true; var format:TextFormat = new TextFormat(); format.font = "Verdana"; format.color = 0xFF0000; format.size = 10; myTextBox.defaultTextFormat = format; addChild(myTextBox); myTextBox.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownScroll); } public function mouseDownScroll(event:MouseEvent):void { myTextBox.scrollV++; } } } Last updated 3/21/2011 355 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class Selecting and manipulating text Flash Player 9 and later, Adobe AIR 1.0 and later You can select dynamic or input text. Since the text selection properties and methods of the TextField class use index positions to set the range of text to manipulate, you can programmatically select dynamic or input text even if you don’t know the content. Note: In Flash Professional, if you choose the selectable option on a static text field, the text field that is exported and placed on the display list is a regular, dynamic text field. Selecting text Flash Player 9 and later, Adobe AIR 1.0 and later The flash.text.TextField.selectable property is true by default, and you can programmatically select text using the setSelection() method. For example, you can set specific text within a text field to be selected when the user clicks the text field: var myTextField:TextField = new TextField(); myTextField.text = "No matter where you click on this text field the TEXT IN ALL CAPS is selected."; myTextField.autoSize = TextFieldAutoSize.LEFT; addChild(myTextField); addEventListener(MouseEvent.CLICK, selectText); function selectText(event:MouseEvent):void { myTextField.setSelection(49, 65); } Similarly, if you want text within a text field to be selected as the text is initially displayed, create an event handler function that is called as the text field is added to the display list. Capturing user-selected text Flash Player 9 and later, Adobe AIR 1.0 and later The TextField selectionBeginIndex and selectionEndIndex properties, which are “read-only” so they can’t be set to programmatically select text, can be used to capture whatever the user has currently selected. Additionally, input text fields can use the caretIndex property. For example, the following code traces the index values of user-selected text: Last updated 3/21/2011 356 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class var myTextField:TextField = new TextField(); myTextField.text = "Please select the TEXT IN ALL CAPS to see the index values for the first and last letters."; myTextField.autoSize = TextFieldAutoSize.LEFT; addChild(myTextField); addEventListener(MouseEvent.MOUSE_UP, selectText); function selectText(event:MouseEvent):void { trace("First letter index position: " + myTextField.selectionBeginIndex); trace("Last letter index position: " + myTextField.selectionEndIndex); } You can apply a collection of TextFormat object properties to the selection to change the text appearance. For more information about applying a collection of TextFormat properties to selected text, see “Formatting ranges of text within a text field” on page 361. Capturing text input Flash Player 9 and later, Adobe AIR 1.0 and later By default, the type property of a text field is set to dynamic. If you set the type property to input using the TextFieldType class, you can collect user input and save the value for use in other parts of your application. Input text fields are useful for forms and any application that wants the user to define a text value for use elsewhere in the program. For example, the following code creates an input text field called myTextBox. As the user enters text in the field, the textInput event is triggered. An event handler called textInputCapture captures the string of text entered and assigns it a variable. Flash Player or AIR displays the new text in another text field, called myOutputBox. package { import import import import flash.display.Sprite; flash.display.Stage; flash.text.*; flash.events.*; public class CaptureUserInput extends { private var myTextBox:TextField = private var myOutputBox:TextField private var myText:String = "Type Sprite new TextField(); = new TextField(); your text here."; public function CaptureUserInput() { captureText(); } public function captureText():void { myTextBox.type = TextFieldType.INPUT; myTextBox.background = true; addChild(myTextBox); Last updated 3/21/2011 357 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class myTextBox.text = myText; myTextBox.addEventListener(TextEvent.TEXT_INPUT, textInputCapture); } public function textInputCapture(event:TextEvent):void { var str:String = myTextBox.text; createOutputBox(str); } public function createOutputBox(str:String):void { myOutputBox.background = true; myOutputBox.x = 200; addChild(myOutputBox); myOutputBox.text = str; } } } Restricting text input Flash Player 9 and later, Adobe AIR 1.0 and later Since input text fields are often used for forms or dialog boxes in applications, you may want to limit the types of characters a user can enter in a text field, or even keep the text hidden —for example, for a password. The flash.text.TextField class has a displayAsPassword property and a restrict property that you can set to control user input. The displayAsPassword property simply hides the text (displaying it as a series of asterisks) as the user types it. When displayAsPassword is set to true, the Cut and Copy commands and their corresponding keyboard shortcuts do not function. As the following example shows, you assign the displayAsPassword property just as you would other properties, such as background and color: myTextBox.type = TextFieldType.INPUT; myTextBox.background = true; myTextBox.displayAsPassword = true; addChild(myTextBox); The restrict property is a little more complicated since you must specify which characters the user is allowed to type in an input text field. You can allow specific letters, numbers, or ranges of letters, numbers, and characters. The following code allows the user to enter only uppercase letters (and not numbers or special characters) in the text field: myTextBox.restrict = "A-Z"; ActionScript 3.0 uses hyphens to define ranges, and carets to define excluded characters. For more information about defining what is restricted in an input text field, see the flash.text.TextField.restrict property entry in the ActionScript 3.0 Reference. Last updated 3/21/2011 358 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class Formatting text Flash Player 9 and later, Adobe AIR 1.0 and later You have several options for programmatically formatting the display of text. You can set properties directly on the TextField instance—for example, the TextFIeld.thickness, TextField.textColor, and TextField.textHeight properties.Or you can designate the content of the text field using the htmlText property and use the supported HTML tags, such as b, i, and u. But you can also apply TextFormat objects to text fields containing plain text, or StyleSheet objects to text fields containing the htmlText property. Using TextFormat and StyleSheet objects provides the most control and consistency over the appearance of text throughout your application. You can define a TextFormat or StyleSheet object and apply it to many or all text fields in your application. Assigning text formats Flash Player 9 and later, Adobe AIR 1.0 and later You can use the TextFormat class to set a number of different text display properties and to apply them to the entire contents of a TextField object, or to a range of text. The following example applies one TextFormat object to an entire TextField object and applies a second TextFormat object to a range of text within that TextField object: var tf:TextField = new TextField(); tf.text = "Hello Hello"; var format1:TextFormat = new TextFormat(); format1.color = 0xFF0000; var format2:TextFormat = new TextFormat(); format2.font = "Courier"; tf.setTextFormat(format1); var startRange:uint = 6; tf.setTextFormat(format2, startRange); addChild(tf); The TextField.setTextFormat() method only affects text that is already displayed in the text field. If the content in the TextField changes, your application might need to call the TextField.setTextFormat() method again to reapply the formatting. You can also set the TextField defaultTextFormat property to specify the format to be used for user-entered text. Applying cascading style sheets Flash Player 9 and later, Adobe AIR 1.0 and later Text fields can contain either plain text or HTML-formatted text. Plain text is stored in the text property of the instance, and HTML text is stored in the htmlText property. You can use CSS style declarations to define text styles that you can apply to many different text fields. CSS style declarations can be created in your application code or loaded in at run time from an external CSS file. Last updated 3/21/2011 359 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class The flash.text.StyleSheet class handles CSS styles. The StyleSheet class recognizes a limited set of CSS properties. For a detailed list of the style properties that the StyleSheet class supports, see the flash.textStylesheet entry in the ActionScript 3.0 Reference. As the following example shows, you can create CSS in your code and apply those styles to HTML text by using a StyleSheet object: var style:StyleSheet = new StyleSheet(); var styleObj:Object = new Object(); styleObj.fontSize = "bold"; styleObj.color = "#FF0000"; style.setStyle(".darkRed", styleObj); var tf:TextField = new TextField(); tf.styleSheet = style; tf.htmlText = "Red apple"; addChild(tf); After creating a StyleSheet object, the example code creates a simple object to hold a set of style declaration properties. Then it calls the StyleSheet.setStyle() method, which adds the new style to the style sheet with the name “.darkred”. Next, it applies the style sheet formatting by assigning the StyleSheet object to the TextField styleSheet property. For CSS styles to take effect, the style sheet should be applied to the TextField object before the htmlText property is set. By design, a text field with a style sheet is not editable. If you have an input text field and assign a style sheet to it, the text field shows the properties of the style sheet, but the text field does not allow users to enter new text into it. Also, you cannot use the following ActionScript APIs on a text field with an assigned style sheet: • The TextField.replaceText() method • The TextField.replaceSelectedText() method • The TextField.defaultTextFormat property • The TextField.setTextFormat() method If a text field has a style sheet assigned to it, but later the TextField.styleSheet property is set to null, the contents of both TextField.text and TextField.htmlText properties add tags and attributes to their content to incorporate the formatting from the previously assigned style sheet. To preserve the original htmlText property, save it in a variable before setting the style sheet to null. Loading an external CSS file Flash Player 9 and later, Adobe AIR 1.0 and later The CSS approach to formatting is more powerful when you can load CSS information from an external file at run time. When the CSS data is external to the application itself, you can change the visual style of text in your application without having to change your ActionScript 3.0 source code. After your application has been deployed, you can change an external CSS file to change the look of the application, without having to redeploy the application SWF file. The StyleSheet.parseCSS() method converts a string that contains CSS data into style declarations in the StyleSheet object. The following example shows how to read an external CSS file and apply its style declarations to a TextField object. First, here is the content of the CSS file to be loaded, which is named example.css: Last updated 3/21/2011 360 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class p { font-family: Times New Roman, Times, _serif; font-size: 14; } h1 { font-family: Arial, Helvetica, _sans; font-size: 20; font-weight: bold; } .bluetext { color: #0000CC; } Next is the ActionScript code for a class that loads the example.css file and applies the styles to TextField content: package { import import import import import import import flash.display.Sprite; flash.events.Event; flash.net.URLLoader; flash.net.URLRequest; flash.text.StyleSheet; flash.text.TextField; flash.text.TextFieldAutoSize; public class CSSFormattingExample extends Sprite { var loader:URLLoader; var field:TextField; var exampleText:String = "

    This is a headline

    " + "

    This is a line of text. " + "This line of text is colored blue.

    "; public function CSSFormattingExample():void { field = new TextField(); field.width = 300; Last updated 3/21/2011 361 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class field.autoSize = TextFieldAutoSize.LEFT; field.wordWrap = true; addChild(field); var req:URLRequest = new URLRequest("example.css"); loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onCSSFileLoaded); loader.load(req); } public function onCSSFileLoaded(event:Event):void { var sheet:StyleSheet = new StyleSheet(); sheet.parseCSS(loader.data); field.styleSheet = sheet; field.htmlText = exampleText; } } } When the CSS data is loaded, the onCSSFileLoaded() method executes and calls the StyleSheet.parseCSS() method to transfer the style declarations to the StyleSheet object. Formatting ranges of text within a text field Flash Player 9 and later, Adobe AIR 1.0 and later A useful method of the flash.text.TextField class is the setTextFormat() method. Using setTextFormat(), you can assign specific properties to the contents of a part of a text field to respond to user input, such as forms that need to remind users that certain entries are required or to change the emphasis of a subsection of a passage of text within a text field as a user selects parts of the text. The following example uses TextField.setTextFormat() on a range of characters to change the appearance of part of the content of myTextField when the user clicks the text field: var myTextField:TextField = new TextField(); myTextField.text = "No matter where you click on this text field the TEXT IN ALL CAPS changes format."; myTextField.autoSize = TextFieldAutoSize.LEFT; addChild(myTextField); addEventListener(MouseEvent.CLICK, changeText); var myformat:TextFormat = new TextFormat(); myformat.color = 0xFF0000; myformat.size = 18; myformat.underline = true; function changeText(event:MouseEvent):void { myTextField.setTextFormat(myformat, 49, 65); } Last updated 3/21/2011 362 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class Advanced text rendering Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript 3.0 provides a variety of classes in the flash.text package to control the properties of displayed text, including embedded fonts, anti-aliasing settings, alpha channel control, and other specific settings. The ActionScript 3.0 Reference provides detailed descriptions of these classes and properties, including the CSMSettings, Font, and TextRenderer classes. Using embedded fonts Flash Player 9 and later, Adobe AIR 1.0 and later When you specify a specific font for a TextField in your application, Flash Player or AIR look for a device font (a font that resides on the user’s computer) with the same name. If it doesn’t find that font on the system, or if the user has a slightly different version of a font with that name, the text display could look very different from what you intend. To make sure the user sees exactly the right font, you can embed that font in your application SWF file. Embedded fonts have a number of benefits: • Embedded font characters are anti-aliased, making their edges appear smoother, especially for larger text. • You can rotate text that uses embedded fonts. • Embedded font text can be made transparent or semitransparent. • You can use the kerning CSS style with embedded fonts. The biggest limitation to using embedded fonts is that they increase the file size or download size of your application. The exact method of embedding a font file into your application SWF file varies according to your development environment. Once you have embedded a font you can make sure a TextField uses the correct embedded font: • Set the embedFonts property of the TextField to true. • Create a TextFormat object, set its fontFamily property to the name of the embedded font, and apply the TextFormat object to the TextField. When specifying an embedded font, the fontFamily property should only contain a single name; it cannot use a comma-delimited list of multiple font names. • If using CSS styles to set fonts for TextFields or components, set the font-family CSS property to the name of the embedded font. The font-family property must contain a single name and not a list of names if you want to specify an embedded font. Embedding a font in Flash Flash Professional lets you embed almost any font you have installed on your system, including TrueType fonts and Type 1 Postscript fonts. You can embed fonts in an application in many ways, including: • Setting the font and style properties of a TextField on the Stage and clicking the Embed Fonts checkbox • Creating and referencing a font symbol • Creating and using a run-time shared library containing embedded font symbols Last updated 3/21/2011 363 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class For more details about how to embed fonts in applications, see “Embedded fonts for dynamic or input text fields” in Using Flash. Embedding a font in Flex You can embed fonts in a Flex application in many ways, including: • Using the [Embed] metadata tag in a script • Using the @font-face style declaration • Establish a class for the font and use the [Embed] tag to embed it. You can only embed TrueType fonts directly in a Flex application. Fonts in other formats, such as Type 1 Postscript fonts, can first be embedded in a SWF file using Flash Professional and then that SWF file can be used in your Flex application. For more details about using embedded fonts from SWF files in Flex, see “Embedding fonts from SWF files” in Using Flex 4. More Help topics Peter deHaan: Embedding fonts Divillysausages.com: AS3 Font embedding masterclass Controlling sharpness, thickness, and anti-aliasing Flash Player 9 and later, Adobe AIR 1.0 and later By default, Flash Player or AIR determines the settings for text display controls like sharpness, thickness, and antialiasing as text resizes, changes color, or is displayed on various backgrounds. In some cases, like when you have very small or very large text, or text on a variety of unique backgrounds, you might want to maintain control over these settings. You can override Flash Player or AIR settings using the flash.text.TextRenderer class and its associated classes, like the CSMSettings class. These classes give you precise control over the rendering quality of embedded text. For more information about embedded fonts, see “Using embedded fonts” on page 362. Note: The flash.text.TextField.antiAliasType property must have the value AntiAliasType.ADVANCED in order for you to set the sharpness, thickness, or the gridFitType property, or to use the TextRenderer.setAdvancedAntiAliasingTable() method. The following example applies custom continuous stroke modulation (CSM) properties and formatting to displayed text using an embedded font called myFont. When the user clicks the displayed text, Flash Player or Adobe AIR applies the custom settings: Last updated 3/21/2011 364 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class var format:TextFormat = new TextFormat(); format.color = 0x336699; format.size = 48; format.font = "myFont"; var myText:TextField = new TextField(); myText.embedFonts = true; myText.autoSize = TextFieldAutoSize.LEFT; myText.antiAliasType = AntiAliasType.ADVANCED; myText.defaultTextFormat = format; myText.selectable = false; myText.mouseEnabled = true; myText.text = "Hello World"; addChild(myText); myText.addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(event:Event):void { var myAntiAliasSettings = new CSMSettings(48, 0.8, -0.8); var myAliasTable:Array = new Array(myAntiAliasSettings); TextRenderer.setAdvancedAntiAliasingTable("myFont", FontStyle.ITALIC, TextColorType.DARK_COLOR, myAliasTable); } Working with static text Flash Player 9 and later, Adobe AIR 1.0 and later Static text is created only within Flash Professional. You cannot programmatically instantiate static text using ActionScript. Static text is useful if the text is short and is not intended to change (as dynamic text can). Think of static text as similar to a graphic element like a circle or square drawn on the Stage in Flash Professional. While static text is more limited than dynamic text, ActionScript 3.0 does allow you to read the property values of static text using the StaticText class. You can also use the TextSnapshot class to read values out of the static text. Accessing static text fields with the StaticText class Flash Player 9 and later, Adobe AIR 1.0 and later Typically, you use the flash.text.StaticText class in the Actions panel of Flash Professional to interact with a static text instance placed on the Stage. You may also work in ActionScript files that interact with a SWF file containing static text. In either case, you can’t instantiate a static text instance programmatically. Static text is created in Flash Professional. To create a reference to an existing static text field, iterate over the items in the display list and assign a variable. For example: Last updated 3/21/2011 365 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class for (var i = 0; i < this.numChildren; i++) { var displayitem:DisplayObject = this.getChildAt(i); if (displayitem instanceof StaticText) { trace("a static text field is item " + i + " on the display list"); var myFieldLabel:StaticText = StaticText(displayitem); trace("and contains the text: " + myFieldLabel.text); } } Once you have a reference to a static text field, you can use the properties of that field in ActionScript 3.0. The following code is attached to a frame in the Timeline, and assumes that a variable named myFieldLabel is assigned to a static text reference. A dynamic text field named myField is positioned relative to the x and y values of myFieldLabel and displays the value of myFieldLabel again. var myField:TextField = new TextField(); addChild(myField); myField.x = myFieldLabel.x; myField.y = myFieldLabel.y + 20; myField.autoSize = TextFieldAutoSize.LEFT; myField.text = "and " + myFieldLabel.text Using the TextSnapshot class Flash Player 9 and later, Adobe AIR 1.0 and later If you want to programmatically work with an existing static text instance, you can use the flash.text.TextSnapshot class to work with the textSnapshot property of a flash.display.DisplayObjectContainer. In other words, you create a TextSnapshot instance from the DisplayObjectContainer.textSnapshot property. You can then apply methods to that instance to retrieve values or select parts of the static text. For example, place a static text field that contains the text "TextSnapshot Example" on the Stage. Add the following ActionScript to Frame 1 of the Timeline: var mySnap:TextSnapshot = this.textSnapshot; var count:Number = mySnap.charCount; mySnap.setSelected(0, 4, true); mySnap.setSelected(1, 2, false); var myText:String = mySnap.getSelectedText(false); trace(myText); The TextSnapshot class is useful for getting the text out of static text fields in a loaded SWF file, if you want to use the text as a value in another part of an application. TextField Example: Newspaper-style text formatting Flash Player 9 and later, Adobe AIR 1.0 and later The News Layout example formats text to look something like a story in a printed newspaper. The input text can contain a headline, a subtitle, and the body of the story. Given a display width and height, this News Layout example formats the headline and the subtitle to take the full width of the display area. The story text is distributed across two or more columns. Last updated 3/21/2011 366 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class This example illustrates the following ActionScript programming techniques: • Extending the TextField class • Loading and applying an external CSS file • Converting CSS styles into TextFormat objects • Using the TextLineMetrics class to get information about text display size To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The News Layout application files can be found in the folder Samples/NewsLayout. The application consists of the following files: File Description NewsLayout.mxml The user interface for the application for Flex (MXML) or Flash (FLA). or NewsLayout.fla com/example/programmingas3/ne wslayout/StoryLayoutComponent.a s A Flex UIComponent class that places the StoryLayout instance. com/example/programmingas3/ne wslayout/StoryLayout.as The main ActionScript class that arranges all the components of a news story for display. com/example/programmingas3/ne wslayout/FormattedTextField.as A subclass of the TextField class that manages its own TextFormat object. com/example/programmingas3/ne wslayout/HeadlineTextField.as A subclass of the FormattedTextField class that adjusts font sizes to fit a desired width. com/example/programmingas3/ne wslayout/MultiColumnTextField.as An ActionScript class that splits text across two or more columns. story.css A CSS file that defines text styles for the layout. Reading the external CSS file Flash Player 9 and later, Adobe AIR 1.0 and later The News Layout application starts by reading story text from a local XML file. Then it reads an external CSS file that provides the formatting information for the headline, subtitle, and main text. The CSS file defines three styles, a standard paragraph style for the story, and the h1 and h2 styles for the headline and subtitle respectively. Last updated 3/21/2011 367 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class p { font-family: Georgia, "Times New Roman", Times, _serif; font-size: 12; leading: 2; text-align: justify; indent: 24; } h1 { font-family: Verdana, Arial, Helvetica, _sans; font-size: 20; font-weight: bold; color: #000099; text-align: left; } h2 { font-family: Verdana, Arial, Helvetica, _sans; font-size: 16; font-weight: normal; text-align: left; } The technique used to read the external CSS file is the same as the technique described in “Loading an external CSS file” on page 359. When the CSS file has been loaded the application executes the onCSSFileLoaded() method, shown below. public function onCSSFileLoaded(event:Event):void { this.sheet = new StyleSheet(); this.sheet.parseCSS(loader.data); h1Format = getTextStyle("h1", this.sheet); if (h1Format == null) { h1Format = getDefaultHeadFormat(); } h2Format = getTextStyle("h2", this.sheet); if (h2Format == null) { h2Format = getDefaultHeadFormat(); h2Format.size = 16; } pFormat = getTextStyle("p", this.sheet); if (pFormat == null) { pFormat = getDefaultTextFormat(); pFormat.size = 12; } displayText(); } The onCSSFileLoaded() method creates a StyleSheet object and has it parse the input CSS data. The main text for the story is displayed in a MultiColumnTextField object, which can use a StyleSheet object directly. However, the headline fields use the HeadlineTextField class, which uses a TextFormat object for its formatting. Last updated 3/21/2011 368 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class The onCSSFileLoaded() method calls the getTextStyle() method twice to convert a CSS style declaration into a TextFormat object for use with each of the two HeadlineTextField objects. public function getTextStyle(styleName:String, ss:StyleSheet):TextFormat { var format:TextFormat = null; var style:Object = ss.getStyle(styleName); if (style != null) { var colorStr:String = style.color; if (colorStr != null && colorStr.indexOf("#") == 0) { style.color = colorStr.substr(1); } format = new TextFormat(style.fontFamily, style.fontSize, style.color, (style.fontWeight == "bold"), (style.fontStyle == "italic"), (style.textDecoration == "underline"), style.url, style.target, style.textAlign, style.marginLeft, style.marginRight, style.indent, style.leading); if (style.hasOwnProperty("letterSpacing")) { format.letterSpacing = style.letterSpacing; } } return format; } The property names and the meaning of the property values differ between CSS style declarations and TextFormat objects. The getTextStyle() method translates CSS property values into the values expected by the TextFormat object. Arranging story elements on the page Flash Player 9 and later, Adobe AIR 1.0 and later The StoryLayout class formats and lays out the headline, subtitle, and main text fields into a newspaper-style arrangement. The displayText() method initially creates and places the various fields. Last updated 3/21/2011 369 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class public function displayText():void { headlineTxt = new HeadlineTextField(h1Format); headlineTxt.wordWrap = true; headlineTxt.x = this.paddingLeft; headlineTxt.y = this.paddingTop; headlineTxt.width = this.preferredWidth; this.addChild(headlineTxt); headlineTxt.fitText(this.headline, 1, true); subtitleTxt = new HeadlineTextField(h2Format); subtitleTxt.wordWrap = true; subtitleTxt.x = this.paddingLeft; subtitleTxt.y = headlineTxt.y + headlineTxt.height; subtitleTxt.width = this.preferredWidth; this.addChild(subtitleTxt); subtitleTxt.fitText(this.subtitle, 2, false); storyTxt = new MultiColumnText(this.numColumns, 20, this.preferredWidth, 400, true, this.pFormat); storyTxt.x = this.paddingLeft; storyTxt.y = subtitleTxt.y + subtitleTxt.height + 10; this.addChild(storyTxt); storyTxt.text = this.content; ... Each field is placed below the previous field by setting its y property to equal the y property of the previous field plus its height. This dynamic placement calculation is needed because HeadlineTextField objects and MultiColumnTextField objects can change their height to fit their contents. Altering font size to fit the field size Flash Player 9 and later, Adobe AIR 1.0 and later Given a width in pixels and a maximum number of lines to display, the HeadlineTextField alters the font size to make the text fit the field. If the text is short, the font size is large, creating a tabloid-style headline. If the text is long, the font size is smaller. The HeadlineTextField.fitText() method shown below does the font sizing work: Last updated 3/21/2011 370 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class public function fitText(msg:String, maxLines:uint = 1, toUpper:Boolean = false, targetWidth:Number = -1):uint { this.text = toUpper ? msg.toUpperCase() : msg; if (targetWidth == -1) { targetWidth = this.width; } var pixelsPerChar:Number = targetWidth / msg.length; var pointSize:Number = Math.min(MAX_POINT_SIZE, Math.round(pixelsPerChar * 1.8 * maxLines)); if (pointSize < 6) { // the point size is too small return pointSize; } this.changeSize(pointSize); if (this.numLines > maxLines) { return shrinkText(--pointSize, maxLines); } else { return growText(pointSize, maxLines); } } public function growText(pointSize:Number, maxLines:uint = 1):Number { if (pointSize >= MAX_POINT_SIZE) { return pointSize; } this.changeSize(pointSize + 1); if (this.numLines > maxLines) { // set it back to the last size this.changeSize(pointSize); return pointSize; } else { return growText(pointSize + 1, maxLines); } Last updated 3/21/2011 371 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class } public function shrinkText(pointSize:Number, maxLines:uint=1):Number { if (pointSize <= MIN_POINT_SIZE) { return pointSize; } this.changeSize(pointSize); if (this.numLines > maxLines) { return shrinkText(pointSize - 1, maxLines); } else { return pointSize; } } The HeadlineTextField.fitText() method uses a simple recursive technique to size the font. First it guesses an average number of pixels per character in the text and from there calculates a starting point size. Then it changes the font size and checks whether the text has word wrapped to create more than the maximum number of text lines. If there are too many lines it calls the shrinkText() method to decrease the font size and try again. If there are not too many lines it calls the growText() method to increase the font size and try again. The process stops at the point where incrementing the font size by one more point would create too many lines. Splitting text across multiple columns Flash Player 9 and later, Adobe AIR 1.0 and later The MultiColumnTextField class spreads text among multiple TextField objects which are then arranged like newspaper columns. The MultiColumnTextField() constructor first creates an array of TextField objects, one for each column, as shown here: for (var i:int = 0; i < cols; i++) { var field:TextField = new TextField(); field.multiline = true; field.autoSize = TextFieldAutoSize.NONE; field.wordWrap = true; field.width = this.colWidth; field.setTextFormat(this.format); this.fieldArray.push(field); this.addChild(field); } Each TextField object is added to the array and added to the display list with the addChild() method. Whenever the StoryLayout text property or styleSheet property changes, it calls the layoutColumns() method to redisplay the text. The layoutColumns() method calls the getOptimalHeight() method, to figure out the correct pixel height that is needed to fit all of the text within the given layout width. Last updated 3/21/2011 372 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class public function getOptimalHeight(str:String):int { if (field.text == "" || field.text == null) { return this.preferredHeight; } else { this.linesPerCol = Math.ceil(field.numLines / this.numColumns); var metrics:TextLineMetrics = field.getLineMetrics(0); this.lineHeight = metrics.height; var prefHeight:int = linesPerCol * this.lineHeight; return prefHeight + 4; } } First the getOptimalHeight() method calculates the width of each column. Then it sets the width and htmlText property of the first TextField object in the array. The getOptimalHeight() method uses that first TextField object to discover the total number of word-wrapped lines in the text, and from that it identifies how many lines should be in each column. Next it calls the TextField.getLineMetrics() method to retrieve a TextLineMetrics object that contains details about size of the text in the first line. The TextLineMetrics.height property represents the full height of a line of text, in pixels, including the ascent, descent, and leading. The optimal height for the MultiColumnTextField object is then the line height multiplied by the number of lines per column, plus 4 to account for the two-pixel border at the top and the bottom of a TextField object. Here is the code for the full layoutColumns() method: public function layoutColumns():void { if (this._text == "" || this._text == null) { return; } var field:TextField = fieldArray[0] as TextField; field.text = this._text; field.setTextFormat(this.format); this.preferredHeight = this.getOptimalHeight(field); var remainder:String = this._text; var fieldText:String = ""; var lastLineEndedPara:Boolean = true; var indent:Number = this.format.indent as Number; for (var i:int = 0; i < fieldArray.length; i++) { field = this.fieldArray[i] as TextField; field.height = this.preferredHeight; field.text = remainder; field.setTextFormat(this.format); Last updated 3/21/2011 373 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class var lineLen:int; if (indent > 0 && !lastLineEndedPara && field.numLines > 0) { lineLen = field.getLineLength(0); if (lineLen > 0) { field.setTextFormat(this.firstLineFormat, 0, lineLen); } } field.x = i * (colWidth + gutter); field.y = 0; remainder = ""; fieldText = ""; var linesRemaining:int = field.numLines; var linesVisible:int = Math.min(this.linesPerCol, linesRemaining); for (var j:int = 0; j < linesRemaining; j++) { if (j < linesVisible) { fieldText += field.getLineText(j); } else { remainder +=field.getLineText(j); } } field.text = fieldText; field.setTextFormat(this.format); if (indent > 0 && !lastLineEndedPara) { lineLen = field.getLineLength(0); if (lineLen > 0) { field.setTextFormat(this.firstLineFormat, 0, lineLen); } } var lastLine:String = field.getLineText(field.numLines - 1); var lastCharCode:Number = lastLine.charCodeAt(lastLine.length - 1); Last updated 3/21/2011 374 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the TextField class if (lastCharCode == 10 || lastCharCode == 13) { lastLineEndedPara = true; } else { lastLineEndedPara = false; } if ((this.format.align == TextFormatAlign.JUSTIFY) && (i < fieldArray.length - 1)) { if (!lastLineEndedPara) { justifyLastLine(field, lastLine); } } } } After the preferredHeight property has been set by calling the getOptimalHeight() method, the layoutColumns() method iterates through the TextField objects, setting the height of each to the preferredHeight value. The layoutColumns() method then distributes just enough lines of text to each field so that no scrolling occurs in any individual field, and the text in each successive field begins where the text in the previous field ended. If the text alignment style has been set to “justify” then the justifyLastLine() method is called to justify the final line of text in a field. Otherwise that last line would be treated as an end-of-paragraph line and not justified. Last updated 3/21/2011 375 Chapter 20: Using the Flash Text Engine Flash Player 10 and later, Adobe AIR 1.5 and later The Adobe® Flash® Text Engine (FTE), available starting with Flash Player 10 and Adobe® AIR™1.5, provides low-level support for sophisticated control of text metrics, formatting, and bi-directional text. It offers improved text flow and enhanced language support. While it can be used to create and manage simple text elements, the FTE is primarily designed as a foundation for developers to create text-handling components. As such, Flash Text Engine assumes a more advanced level of programming expertise. To display simple text elements, see “Using the TextField class” on page 351. The Text Layout Framework, which includes a text-handling component based on the FTE, provides an easier way to use its advanced features. The Text Layout Framework is an extensible library built entirely in ActionScript 3.0. You can use the existing TLF component, or use the framework to build your own text component. For more information, see “Using the Text Layout Framework” on page 404. More Help topics flash.text.engine package Creating and displaying text Flash Player 10 and later, Adobe AIR 1.5 and later The classes that make up the Flash Text Engine enable you to create, format, and control text. The following classes are the basic building blocks for creating and displaying text with the Flash Text Engine: • TextElement/GraphicElement/GroupElement - contain the content of a TextBlock instance • ElementFormat - specifies formatting attributes for the content of a TextBlock instance • TextBlock - the factory for building a paragraph of text • TextLine - a line of text created from the TextBlock To display text, create a TextElement object from a String, using an ElementFormat object to specify the formatting characteristics. Assign the TextElement to the content property of a TextBlock object. Create the lines of text for display by calling the TextBlock.createTextLine() method. The createTextLine() method returns a TextLine object containing as much of the string as will fit in the specified width. Call the method repeatedly until the entire string has been formatted into lines. When there are no more lines to be created, the textLineCreationResult property of the TextBlock object is assigned the value: TextLineCreationResult.COMPLETE. To show the lines, add them to the display list (with appropriate x and y position values). The following code, for example, uses these FTE classes to display, "Hello World! This is Flash Text Engine!", using default format and font values. In this simple example, only a single line of text is created. Last updated 3/21/2011 376 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import flash.text.engine.*; import flash.display.Sprite; public class HelloWorldExample extends Sprite { public function HelloWorldExample() { var str = "Hello World! This is Flash Text Engine!"; var format:ElementFormat = new ElementFormat(); var textElement:TextElement = new TextElement(str, format); var textBlock:TextBlock = new TextBlock(); textBlock.content = textElement; var textLine1:TextLine = textBlock.createTextLine(null, 300); addChild(textLine1); textLine1.x = 30; textLine1.y = 30; } } } The parameters for createTextLine() specify the line from which to begin the new line and the width of the line in pixels. The line from which to begin the new line is usually the previous line but, in the case of the first line, it is null. Adding GraphicElement and GroupElement objects Flash Player 10 and later, Adobe AIR 1.5 and later You can assign a GraphicElement object to a TextBlock object to display an image or a graphic element. Simply create an instance of the GraphicElement class from a graphic or an image and assign the instance to the TextBlock.content property. Create the text line by calling TextBlock.createTextline() as you normally would. The following example creates two text lines, one with a GraphicElement object and one with a TextElement object. package { import import import import flash.text.engine.*; flash.display.Sprite; flash.display.Shape; flash.display.Graphics; public class GraphicElementExample extends Sprite { public function GraphicElementExample() { var str:String = "Beware of Dog!"; var triangle:Shape = new Shape(); triangle.graphics.beginFill(0xFF0000, 1); triangle.graphics.lineStyle(3); triangle.graphics.moveTo(30, 0); triangle.graphics.lineTo(60, 50); triangle.graphics.lineTo(0, 50); triangle.graphics.lineTo(30, 0); triangle.graphics.endFill(); Last updated 3/21/2011 377 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine var format:ElementFormat = new ElementFormat(); format.fontSize = 20; var graphicElement:GraphicElement = new GraphicElement(triangle, triangle.width, triangle.height, format); var textBlock:TextBlock = new TextBlock(); textBlock.content = graphicElement; var textLine1:TextLine = textBlock.createTextLine(null, triangle.width); textLine1.x = 50; textLine1.y = 110; addChild(textLine1); var textElement:TextElement = new TextElement(str, format); textBlock.content = textElement; var textLine2 = textBlock.createTextLine(null, 300); addChild(textLine2); textLine2.x = textLine1.x - 30; textLine2.y = textLine1.y + 15; } } } You can create a GroupElement object to create a group of TextElement, GraphicElement, and other GroupElement objects. A GroupElement can be assigned to the content property of a TextBlock object. The parameter to the GroupElement() constructor is a Vector, which points to the text, graphic, and group elements that make up the group. The following example groups two graphic elements and a text element and assigns them as a unit to a text block. package { import import import import flash.text.engine.*; flash.display.Sprite; flash.display.Shape; flash.display.Graphics; public class GroupElementExample extends Sprite { public function GroupElementExample() { var str:String = "Beware of Alligators!"; var triangle1:Shape = new Shape(); triangle1.graphics.beginFill(0xFF0000, 1); triangle1.graphics.lineStyle(3); triangle1.graphics.moveTo(30, 0); triangle1.graphics.lineTo(60, 50); triangle1.graphics.lineTo(0, 50); triangle1.graphics.lineTo(30, 0); triangle1.graphics.endFill(); var triangle2:Shape = new Shape(); triangle2.graphics.beginFill(0xFF0000, 1); triangle2.graphics.lineStyle(3); triangle2.graphics.moveTo(30, 0); triangle2.graphics.lineTo(60, 50); triangle2.graphics.lineTo(0, 50); Last updated 3/21/2011 378 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine triangle2.graphics.lineTo(30, 0); triangle2.graphics.endFill(); var format:ElementFormat = new ElementFormat(); format.fontSize = 20; var graphicElement1:GraphicElement = new GraphicElement(triangle1, triangle1.width, triangle1.height, format); var textElement:TextElement = new TextElement(str, format); var graphicElement2:GraphicElement = new GraphicElement(triangle2, triangle2.width, triangle2.height, format); var groupVector:Vector. = new Vector.(); groupVector.push(graphicElement1, textElement, graphicElement2); var groupElement = new GroupElement(groupVector); var textBlock:TextBlock = new TextBlock(); textBlock.content = groupElement; var textLine:TextLine = textBlock.createTextLine(null, 800); addChild(textLine); textLine.x = 100; textLine.y = 200; } } } Replacing text Flash Player 10 and later, Adobe AIR 1.5 and later You can replace text in a TextBlock instance by calling TextElement.replaceText() to replace text in the TextElement that you assigned to the TextBlock.content property. The following example uses replaceText() to first, insert text at the beginning of the line, then, to append text to the end of the line, and, finally, to replace text in the middle of the line. Last updated 3/21/2011 379 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import flash.text.engine.*; import flash.display.Sprite; public class ReplaceTextExample extends Sprite { public function ReplaceTextExample() { var str:String = "Lorem ipsum dolor sit amet"; var fontDescription:FontDescription = new FontDescription("Arial"); var format:ElementFormat = new ElementFormat(fontDescription); format.fontSize = 14; var textElement:TextElement = new TextElement(str, format); var textBlock:TextBlock = new TextBlock(); textBlock.content = textElement; createLine(textBlock, 10); textElement.replaceText(0, 0, "A text fragment: "); createLine(textBlock, 30); textElement.replaceText(43, 43, "..."); createLine(textBlock, 50); textElement.replaceText(23, 28, "(ipsum)"); createLine(textBlock, 70); } function createLine(textBlock:TextBlock, y:Number):void { var textLine:TextLine = textBlock.createTextLine(null, 300); textLine.x = 10; textLine.y = y; addChild(textLine); } } } The replaceText() method replaces the text specified by the beginIndex and endIndex parameters with the text specified by the newText parameter. If the values of the beginIndex and endIndex parameters are the same, replaceText() inserts the specified text at that location. Otherwise it replaces the characters specified by beginIndex and endIndex with the new text. Handling Events in FTE Flash Player 10 and later, Adobe AIR 1.5 and later You can add event listeners to a TextLine instance just as you can to other display objects. For example, you can detect when a user rolls the mouse over a text line or a user clicks the line. The following example detects both of these events. When you roll the mouse over the line, the cursor changes to a button cursor and when you click the line, it changes color. Last updated 3/21/2011 380 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import import import import import flash.text.engine.*; flash.ui.Mouse; flash.display.Sprite flash.events.MouseEvent; flash.events.EventDispatcher; public class EventHandlerExample extends Sprite { var textBlock:TextBlock = new TextBlock(); public function EventHandlerExample():void { var str:String = "I'll change color if you click me."; var fontDescription:FontDescription = new FontDescription("Arial"); var format:ElementFormat = new ElementFormat(fontDescription, 18); var textElement = new TextElement(str, format); textBlock.content = textElement; createLine(textBlock); } private function createLine(textBlock:TextBlock):void { var textLine:TextLine = textBlock.createTextLine(null, 500); textLine.x = 30; textLine.y = 30; addChild(textLine); textLine.addEventListener("mouseOut", mouseOutHandler); textLine.addEventListener("mouseOver", mouseOverHandler); textLine.addEventListener("click", clickHandler); } private function mouseOverHandler(event:MouseEvent):void { Mouse.cursor = "button"; } private function mouseOutHandler(event:MouseEvent):void { Mouse.cursor = "arrow"; } function clickHandler(event:MouseEvent):void { if(textBlock.firstLine) removeChild(textBlock.firstLine); var newFormat:ElementFormat = textBlock.content.elementFormat.clone(); Last updated 3/21/2011 381 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine switch(newFormat.color) { case 0x000000: newFormat.color = 0xFF0000; break; case 0xFF0000: newFormat.color = 0x00FF00; break; case 0x00FF00: newFormat.color = 0x0000FF; break; case 0x0000FF: newFormat.color = 0x000000; break; } textBlock.content.elementFormat = newFormat; createLine(textBlock); } } } Mirroring events Flash Player 10 and later, Adobe AIR 1.5 and later You can also mirror events on a text block, or on a portion of a text block, to an event dispatcher. First, create an EventDispatcher instance and then assign it to the eventMirror property of a TextElement instance. If the text block consists of a single text element, the text engine mirrors events for the entire text block. If the text block consists of multiple text elements, the text engine mirrors events only for the TextElement instances that have the eventMirror property set. The text in the following example consists of three elements: the word "Click", the word "here", and the string "to see me in italic". The example assigns an event dispatcher to the second text element, the word "here", and adds an event listener, the clickHandler() method. The clickHandler() method changes the text to italic. It also replaces the content of the third text element to read, "Click here to see me in normal font!". package { import import import import import flash.text.engine.*; flash.ui.Mouse; flash.display.Sprite; flash.events.MouseEvent; flash.events.EventDispatcher; public class EventMirrorExample extends Sprite { var fontDescription:FontDescription = new FontDescription("Helvetica", "bold"); var format:ElementFormat = new ElementFormat(fontDescription, 18); var textElement1 = new TextElement("Click ", format); var textElement2 = new TextElement("here ", format); var textElement3 = new TextElement("to see me in italic! ", format); var textBlock:TextBlock = new TextBlock(); public function EventMirrorExample() { var myEvent:EventDispatcher = new EventDispatcher(); Last updated 3/21/2011 382 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine myEvent.addEventListener("click", clickHandler); myEvent.addEventListener("mouseOut", mouseOutHandler); myEvent.addEventListener("mouseOver", mouseOverHandler); textElement2.eventMirror=myEvent; var groupVector:Vector. = new Vector.; groupVector.push(textElement1, textElement2, textElement3); var groupElement:GroupElement = new GroupElement(groupVector); textBlock.content = groupElement; createLines(textBlock); } private function clickHandler(event:MouseEvent):void { var newFont:FontDescription = new FontDescription(); newFont.fontWeight = "bold"; var newFormat:ElementFormat = new ElementFormat(); newFormat.fontSize = 18; if(textElement3.text == "to see me in italic! ") { newFont.fontPosture = FontPosture.ITALIC; textElement3.replaceText(0,21, "to see me in normal font! "); } else { newFont.fontPosture = FontPosture.NORMAL; textElement3.replaceText(0, 26, "to see me in italic! "); } newFormat.fontDescription = newFont; textElement1.elementFormat = newFormat; textElement2.elementFormat = newFormat; textElement3.elementFormat = newFormat; createLines(textBlock); } private function mouseOverHandler(event:MouseEvent):void { Mouse.cursor = "button"; } private function mouseOutHandler(event:MouseEvent):void { Mouse.cursor = "arrow"; } private function createLines(textBlock:TextBlock):void { if(textBlock.firstLine) removeChild (textBlock.firstLine); var textLine:TextLine = textBlock.createTextLine (null, 300); textLine.x = 15; textLine.y = 20; addChild (textLine); } } } Last updated 3/21/2011 383 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine The mouseOverHandler() and mouseOutHandler() functions set the cursor to a button cursor when it's over the word "here" and back to an arrow when it's not. Formatting text Flash Player 10 and later, Adobe AIR 1.5 and later A TextBlock object is a factory for creating lines of text. The content of a TextBlock is assigned via the TextElement object. An ElementFormat object handles the formatting for the text. The ElementFormat class defines such properties as baseline alignment, kerning, tracking, text rotation, and font size, color, and case. It also includes a FontDescription, which is covered in detail in “Working with fonts” on page 387. Using the ElementFormat object Flash Player 10 and later, Adobe AIR 1.5 and later The constructor for the ElementFormat object takes any of a long list of optional parameters, including a FontDescription. You can also set these properties outside the constructor. The following example shows the relationship of the various objects in defining and displaying a simple text line: package { import flash.display.Sprite; import flash.text.*; public class ElementFormatExample extends Sprite { private var tb:TextBlock = new TextBlock(); private var te:TextElement; private var ef:ElementFormat; private var fd:FontDescription = new FontDescription(); private var str:String; private var tl:TextLine; public function ElementFormatExample() { fd.fontName = "Garamond"; ef = new ElementFormat(fd); ef.fontSize = 30; ef.color = 0xFF0000; str = "This is flash text"; te = new TextElement(str, ef); tb.content = te; tl = tb.createTextLine(null,600); addChild(tl); } } } Last updated 3/21/2011 384 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine Font color and transparency (alpha) Flash Player 10 and later, Adobe AIR 1.5 and later The color property of the ElementFormat object sets the font color. The value is an integer representing the RGB components of the color; for example, 0xFF0000 for red and 0x00FF00 for green. The default is black (0x000000). The alpha property sets the alpha transparency value for an element (both TextElement and GraphicElement). Values can range from 0 (fully transparent) to 1 (fully opaque, which is the default). Elements with an alpha of 0 are invisible, but still active. This value is multiplied by any inherited alpha values, thus making the element more transparent. var ef:ElementFormat = new ElementFormat(); ef.alpha = 0.8; ef.color = 0x999999; Baseline alignment and shift Flash Player 10 and later, Adobe AIR 1.5 and later The font and size of the largest text in a line determine its dominant baseline. You can override these values by setting TextBlock.baselineFontDescription and TextBlock.baselineFontSize. You can align the dominant baseline with one of several baselines within the text. These baselines include the ascent line and the descent line or the ideographic top, center, or bottom. A D B C A. Ascent B. Baseline C. Descent D. x-height In the ElementFormat object, three properties determine baseline and alignment characteristics. The alignmentBaseline property sets the main baseline of a TextElement or GraphicElement. This baseline is the “snap-to” line for the element, and it’s to this position that the dominant baseline of all text aligns. The dominantBaseline property specifies which of the various baselines of the element to use, which determines the vertical position of the element on the line. The default value is TextBaseline.ROMAN, but it can also be set to have the IDEOGRAPHIC_TOP or IDEOGRAPHIC_BOTTOM baselines be dominant. The baselineShift property moves the baseline by a set number of pixels on the y-axis. In normal (non-rotated) text, a positive value moves the baseline down and a negative value moves it up. Typographic Case Flash Player 10 and later, Adobe AIR 1.5 and later The TypographicCase property of ElementFormat specifies text case, such as uppercase, lowercase, or small caps. Last updated 3/21/2011 385 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine var ef_Upper:ElementFormat = new ElementFormat(); ef_Upper.typographicCase = TypographicCase.UPPERCASE; var ef_SmallCaps:ElementFormat = new ElementFormat(); ef_SmallCaps.typographicCase = TypographicCase.SMALL_CAPS; Rotating text Flash Player 10 and later, Adobe AIR 1.5 and later You can rotate a block of text or the glyphs within a segment of text in increments of 90°. The TextRotation class defines the following constants for setting both text block and glyph rotation: Constant Value Description AUTO “auto” Specifies 90 degree counter-clockwise rotation. Typically used with vertical Asian text to rotate only glyphs that require rotation. ROTATE_0 “rotate_0” Specifies no rotation. ROTATE_180 “rotate_180” Specifies 180 degree rotation. ROTATE_270 “rotate_270” Specifies 270 degree rotation. ROTATE_90 “rotate_90” Specifies 90 degree clockwise rotation. To rotate the lines of text in a text block, set the TextBlock.lineRotation property before calling the TextBlock.createTextLine() method to create the text line. To rotate the glyphs within a block of text or a segment, set the ElementFormat.textRotation property to the number of degrees that you want the glyphs to rotate. A glyph is the shape that makes up a character, or a part of a character that consists of multiple glyphs. The letter “a” and the dot on an “i”, for example, are glyphs. Rotating glyphs is relevant in some Asian languages in which you want to rotate the lines to vertical but not rotate the characters within the lines. For more information on rotating Asian text, see “Justifying East Asian text” on page 391. Here is an example of rotating both a block of text and the glyphs within, as you would with Asian text. The example also uses a Japanese font: Last updated 3/21/2011 386 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import flash.display.Sprite; import flash.text.*; public class RotationExample extends Sprite { private var tb:TextBlock = new TextBlock(); private var te:TextElement; private var ef:ElementFormat; private var fd:FontDescription = new FontDescription(); private var str:String; private var tl:TextLine; public function RotationExample() { fd.fontName = "MS Mincho"; ef = new ElementFormat(fd); ef.textRotation = TextRotation.AUTO; str = "This is rotated Japanese text"; te = new TextElement(str, ef); tb.lineRotation = TextRotation.ROTATE_90; tb.content = te; tl = tb.createTextLine(null,600); addChild(tl); } } } Locking and cloning ElementFormat Flash Player 10 and later, Adobe AIR 1.5 and later When an ElementFormat object is assigned to any type of ContentElement, its locked property is automatically set to true. Attempting to modify a locked ElementFormat object throws an IllegalOperationError. The best practice is to fully define such an object before assigning it to a TextElement instance. If you want to modify an existing ElementFormat instance, first check its locked property. If it’s true, use the clone()method to create an unlocked copy of the object. The properties of this unlocked object can be changed, and it can then be assigned to the TextElement instance. Any new lines created from it have the new formatting. Previous lines created from this same object and using the old format are unchanged. Last updated 3/21/2011 387 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import flash.display.Sprite; import flash.text.*; public class ElementFormatCloneExample extends Sprite { private var tb:TextBlock = new TextBlock(); private var te:TextElement; private var ef1:ElementFormat; private var ef2:ElementFormat; private var fd:FontDescription = new FontDescription(); public function ElementFormatCloneExample() { fd.fontName = "Garamond"; ef1 = new ElementFormat(fd); ef1.fontSize = 24; var str:String = "This is flash text"; te = new TextElement(str, ef); tb.content = te; var tx1:TextLine = tb.createTextLine(null,600); addChild(tx1); ef2 = (ef1.locked) ? ef1.clone() : ef1; ef2.fontSize = 32; tb.content.elementFormat = ef2; var tx2:TextLine = tb.createTextLine(null,600); addChild(tx2); } } } Working with fonts Flash Player 10 and later, Adobe AIR 1.5 and later The FontDescription object is used in conjunction with ElementFormat to identify a font face and define some of its characteristics. These characteristics include the font name, weight, posture, rendering, and how to find the font (device versus embedded). Note: FTE does not support Type 1 fonts or bitmap fonts such as Type 3, ATC, sfnt-wrapped CID, or Naked CID. Last updated 3/21/2011 388 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine Defining font characteristics (FontDescription object) Flash Player 10 and later, Adobe AIR 1.5 and later The fontName property of the FontDescription object can be a single name or a comma-separated list of names. For example, in a list such as “Arial, Helvetica, _sans”, the text engine looks for “Arial” first, then “Helvetica”, and finally “_sans”, if it can’t find either of the first two fonts. The set of font names include three generic device font names: “_sans”, “_serif”, and “_typewriter”. They map to specific device fonts, depending on the playback system. It is good practice to specify default names such as these in all font descriptions that use device fonts. If no fontName is specified, “_serif” is used as the default. The fontPosture property can either be set to the default (FontPosture.NORMAL) or to italics (FontPosture.ITALIC). The fontWeight property can be set to the default (FontWeight.NORMAL) or to bold (FontWeight.BOLD). var fd1:FontDescription = new FontDescription(); fd1.fontName = "Arial, Helvetica, _sans"; fd1.fontPosture = FontPosture.NORMAL; fd1.fontWeight = FontWeight.BOLD; Embedded versus device fonts Flash Player 10 and later, Adobe AIR 1.5 and later The fontLookup property of the FontDescription object specifies whether the text engine looks for a device font or embedded font to render text. If a device font (FontLookup.DEVICE) is specified, the runtime looks for the font on the playback system. Specifying an embedded font (FontLookup.EMBEDDED_CFF) causes the runtime to look for an embedded font with the specified name in the SWF file. Only embedded CFF (Compact Font Format) fonts work with this setting. If the specified font is not found, a fallback device font is used. Device fonts result in a smaller SWF file size. Embedded fonts give you greater fidelity across platforms. var fd1:FontDescription = new FontDescription(); fd1.fontLookup = FontLookup.EMBEDDED_CFF; fd1.fontName = "Garamond, _serif"; Rendering mode and hinting Flash Player 10 and later, Adobe AIR 1.5 and later CFF (Compact Font Format) rendering is available starting with Flash Player 10 and Adobe AIR 1.5. This type of font rendering makes text more legible, and permits higher-quality display of fonts at small sizes. This setting only applies to embedded fonts. FontDescription defaults to this setting (RenderingMode.CFF) for the renderingMode property. You can set this property to RenderingMode.NORMAL to match the type of rendering used by Flash Player 7 or earlier versions. When CFF rendering is selected, a second property, cffHinting, controls how a font’s horizontal stems are fit to the subpixel grid. The default value, CFFHinting.HORIZONTAL_STEM, uses CFF hinting. Setting this property to CFFHinting.NONE removes hinting, which is appropriate for animation or for large font sizes. var fd1:FontDescription = new FontDescription(); fd1.renderingMode = RenderingMode.CFF; fd1.cffHinting = CFFHinting.HORIZONTAL_STEM; Last updated 3/21/2011 389 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine Locking and cloning FontDescription Flash Player 10 and later, Adobe AIR 1.5 and later When a FontDescription object is assigned to an ElementFormat, its locked property is automatically set to true. Attempting to modify a locked FontDescription object throws an IllegalOperationError. The best practice is to fully define such an object before assigning it to a ElementFormat. If you want to modify an existing FontDescription, first check its locked property. If it’s true, use the clone()method to create an unlocked copy of the object. The properties of this unlocked object can be changed, and it can then be assigned to the ElementFormat. Any new lines created from this TextElement have the new formatting. Previous lines created from this same object are unchanged. package { import flash.display.Sprite; import flash.text.*; public class FontDescriptionCloneExample extends Sprite { private var tb:TextBlock = new TextBlock(); private var te:TextElement; private var ef1:ElementFormat; private var ef2:ElementFormat; private var fd1:FontDescription = new FontDescription(); private var fd2:FontDescription; public function FontDescriptionCloneExample() { fd1.fontName = "Garamond"; ef1 = new ElementFormat(fd); var str:String = "This is flash text"; te = new TextElement(str, ef); tb.content = te; var tx1:TextLine = tb.createTextLine(null,600); addChild(tx1); fd2 = (fd1.locked) ? fd1.clone() : fd1; fd2.fontName = "Arial"; ef2 = (ef1.locked) ? ef1.clone() : ef1; ef2.fontDescription = fd2; tb.content.elementFormat = ef2; var tx2:TextLine = tb.createTextLine(null,600); addChild(tx2); } } } Controlling text Flash Player 10 and later, Adobe AIR 1.5 and later FTE gives you a new set of text formatting controls to handle justification and character spacing (kerning and tracking). There are also properties for controlling that way lines are broken and for setting tab stops within lines. Last updated 3/21/2011 390 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine Justifying text Flash Player 10 and later, Adobe AIR 1.5 and later Justifying text makes all lines in a paragraph the same length by adjusting the spacing between words and sometimes between letters. The effect is to align the text on both sides, while the spacing between words and letters varies. Columns of text in newspapers and magazines are frequently justified. The lineJustfication property in the SpaceJustifier class allows you to control the justification of lines in a block of text. The LineJustification class defines constants that you can use to specify a justification option: ALL_BUT_LAST justifies all but the last line of text; ALL_INCLUDING_LAST justifies all text, including the last line; UNJUSTIFIED, which is the default, leaves the text unjustified. To justify text, set the lineJustification property to an instance of the SpaceJustifier class and assign that instance to the textJustifier property of a TextBlock instance. The following example creates a paragraph in which all but the last line of text is justified. package { import flash.text.engine.*; import flash.display.Sprite; public class JustifyExample extends Sprite { public function JustifyExample() { var str:String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut " + "enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut " + "aliquip ex ea commodo consequat."; var format:ElementFormat = new ElementFormat(); var textElement:TextElement=new TextElement(str,format); var spaceJustifier:SpaceJustifier=new SpaceJustifier("en",LineJustification.ALL_BUT_LAST); var textBlock:TextBlock = new TextBlock(); textBlock.content=textElement; textBlock.textJustifier=spaceJustifier; createLines(textBlock); } private function createLines(textBlock:TextBlock):void { var yPos=20; var textLine:TextLine=textBlock.createTextLine(null,150); while (textLine) { addChild(textLine); textLine.x=15; yPos+=textLine.textHeight+2; textLine.y=yPos; textLine=textBlock.createTextLine(textLine,150); } } } } Last updated 3/21/2011 391 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine To vary spacing between letters as well as between words, set the SpaceJustifier.letterspacing property to true. Turning on letterspacing can reduce the occurrences of unsightly gaps between words, which can sometimes occur with simple justification. Justifying East Asian text Flash Player 10 and later, Adobe AIR 1.5 and later Justifying East Asian text entails additional considerations. It can be written from top to bottom and some characters, known as kinsoku, cannot appear at the beginning or end of a line. The JustificationStyle class defines the following constants, which specify the options for handling these characters. PRIORITIZE_LEAST_ADJUSTMENT bases justification on either expanding or compressing the line, depending on which one produces the most desirable result. PUSH_IN_KINSOKU bases justification on compressing kinsoku at the end of the line, or expanding it if no kinsoku occurs, or if that space is insufficient. PUSH_OUT_ONLY bases justification on expanding the line. To create a block of vertical Asian text, set the TextBlock.lineRotation property to TextRotation.ROTATE_90 and set the ElementFormat.textRotation property to TextRotation.AUTO, which is the default. Setting the textRotation property to AUTO causes the glyphs in the text to remain vertical instead of turning on their side when the line is rotated. The AUTO setting rotates 90° counter-clockwise for full width and wide glyphs only, as determined by the Unicode properties of the glyph. The following example displays a vertical block of Japanese text and justifies it using the PUSH_IN_KINSOKU option. package { import import import import flash.text.engine.*; flash.display.Stage; flash.display.Sprite; flash.system.Capabilities; public class EastAsianJustifyExample extends Sprite { public function EastAsianJustifyExample() { var Japanese_txt:String = String.fromCharCode( 0x5185, 0x95A3, 0x5E9C, 0x304C, 0x300C, 0x653F, 0x5E9C, 0x30A4, 0x30F3, 0x30BF, 0x30FC, 0x30CD, 0x30C3, 0x30C8, 0x30C6, 0x30EC, 0x30D3, 0x300D, 0x306E, 0x52D5, 0x753B, 0x914D, 0x4FE1, 0x5411, 0x3051, 0x306B, 0x30A2, 0x30C9, 0x30D3, 0x30B7, 0x30B9, 0x30C6, 0x30E0, 0x30BA, 0x793E, 0x306E) var textBlock:TextBlock = new TextBlock(); var font:FontDescription = new FontDescription(); var format:ElementFormat = new ElementFormat(); format.fontSize = 12; format.color = 0xCC0000; format.textRotation = TextRotation.AUTO; textBlock.baselineZero = TextBaseline.IDEOGRAPHIC_CENTER; var eastAsianJustifier:EastAsianJustifier = new EastAsianJustifier("ja", LineJustification.ALL_BUT_LAST); eastAsianJustifier.justificationStyle = JustificationStyle.PUSH_IN_KINSOKU; textBlock.textJustifier = eastAsianJustifier; textBlock.lineRotation = TextRotation.ROTATE_90; var linePosition:Number = this.stage.stageWidth - 75; if (Capabilities.os.search("Mac OS") > -1) // set fontName: Kozuka Mincho Pro R Last updated 3/21/2011 392 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine font.fontName = String.fromCharCode(0x5C0F, 0x585A, 0x660E, 0x671D) + " Pro R"; else font.fontName = "Kozuka Mincho Pro R"; textBlock.content = new TextElement(Japanese_txt, format); var previousLine:TextLine = null; while (true) { var textLine:TextLine = textBlock.createTextLine(previousLine, 200); if (textLine == null) break; textLine.y = 20; textLine.x = linePosition; linePosition -= 25; addChild(textLine); previousLine = textLine; } } } } Kerning and tracking Flash Player 10 and later, Adobe AIR 1.5 and later Kerning and tracking affect the distance between adjacent pairs of characters in a text block. Kerning controls how character pairs “fit” together, such as the pairs “WA” or “Va”. Kerning is set in the ElementFormat object. It is enabled by default (Kerning.ON), and can be set to OFF or AUTO, in which case kerning is only applied between characters if neither is Kanji, Hiragana, or Katakana. Tracking adds or subtracts a set number of pixels between all characters in a text block, and is also set in the ElementFormat object. It works with both embedded and device fonts. FTE supports two tracking properties, trackingLeft, which adds/subtracts pixels from the left side of a character, and trackingRight, which adds/subtracts from the right side. If kerning is being used, the tracking value is added to or subtracted from kerning values for each character pair. A B C VAY VAY VAY D E F VAY VAY VAY A. Kerning.OFF B. TrackingRight=5, Kerning.OFF C. TrackingRight=-5, Kerning.OFF D. Kerning.ON E. TrackingRight=-5, Kerning.ON F. TrackingRight=-5, Kerning.ON Last updated 3/21/2011 393 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine var ef1:ElementFormat = new ElementFormat(); ef1.kerning = Kerning.OFF; var ef2:ElementFormat = new ElementFormat(); ef2.kerning = Kerning.ON; ef2.trackingLeft = 0.8; ef2.trackingRight = 0.8; var ef3:ElementFormat = new ElementFormat(); ef3.trackingRight = -0.2; Line breaks for wrapped text Flash Player 10 and later, Adobe AIR 1.5 and later The breakOpportunity property of the ElementFormat object determines which characters can be used for breaking when wrapping text is broken into multiple lines. The default, BreakOpportunity.AUTO, uses standard Unicode properties, such as breaking between words and on hyphens. Using BreakOpportunity.ALL allows any character to be treated as a line break opportunity, which is useful for creating effects like text along a path. var ef:ElementFormat = new ElementFormat(); ef.breakOpportunity = BreakOpportunity.ALL; Tab stops Flash Player 10 and later, Adobe AIR 1.5 and later To set tab stops in a text block, define the tab stops by creating instances of the TabStop class. The parameters to the TabStop() constructor specify how the text aligns with the tab stop. These parameters specify the position of the tab stop, and for decimal alignment, the value on which to align, expressed as a string. Typically, this value is a decimal point but it could also be a comma, a dollar sign, or the symbol for the Yen or the Euro, for example. The following line of code creates a tab stop called tab1. var tab1:TabStop = new TabStop(TabAlignment.DECIMAL, 50, "."); Once you've created the tab stops for a text block, assign them to the tabStops property of a TextBlock instance. Because the tabStops property requires a Vector, though, first create a Vector and add the tab stops to it. The Vector allows you to assign a set of tab stops to the text block. The following example creates a Vector instance and adds a set of TabStop objects to it. Then it assigns the tab stops to the tabStops property of a TextBlock instance. var tabStops:Vector. = new Vector.(); tabStops.push(tab1, tab2, tab3, tab4); textBlock.tabStops = tabStops For more information on Vectors, see “Working with arrays” on page 25. The following example shows the effect of each of the TabStop alignment options. Last updated 3/21/2011 394 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package { import flash.text.engine.*; import flash.display.Sprite; public class TabStopExample extends Sprite { public function TabStopExample() { var format:ElementFormat = new ElementFormat(); format.fontDescription = new FontDescription("Arial"); format.fontSize = 16; var tabStops:Vector. = new Vector.(); tabStops.push( new TabStop(TabAlignment.START, 20), new TabStop(TabAlignment.CENTER, 140), new TabStop(TabAlignment.DECIMAL, 260, "."), new TabStop(TabAlignment.END, 380)); var textBlock:TextBlock = new TextBlock(); textBlock.content = new TextElement( "\tt1\tt2\tt3\tt4\n" + "\tThis line aligns on 1st tab\n" + "\t\t\t\tThis is the end\n" + "\tThe following fragment centers on the 2nd tab:\t\t\n" + "\t\tit's on me\t\t\n" + "\tThe following amounts align on the decimal point:\n" + "\t\t\t45.00\t\n" + "\t\t\t75,320.00\t\n" + "\t\t\t6,950.00\t\n" + "\t\t\t7.01\t\n", format); textBlock.tabStops = tabStops; var yPosition:Number = 60; var previousTextLine:TextLine = null; var textLine:TextLine; var i:int; for (i = 0; i < 10; i++) { textLine = textBlock.createTextLine(previousTextLine, 1000, 0); textLine.x = 20; textLine.y = yPosition; addChild(textLine); yPosition += 25; previousTextLine = textLine; } } } } Last updated 3/21/2011 395 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine Flash Text Engine example: News layout Flash Player 10 and later, Adobe AIR 1.5 and later This programming example shows the Flash Text Engine in use laying out a simple newspaper page. The page includes a large headline, a subhead, and a multicolumn body section. First, create an FLA file, and attach the following code to frame #2 of the default layer: import com.example.programmingas3.newslayout.StoryLayout ; // frame sc ript - create a 3-columned arti cle layout var story:StoryLayout = new StoryLayout(720, 500, 3, 10); story.x = 20; story.y = 80; addChild(story); stop(); StoryLayout.as is the controller script for this example. It sets the content, reads in style information from an external style sheet, and assigns those styles to ElementFormat objects. It then creates the headline, subhead and multicolumn text elements. package com.example.programmingas3.newslayout { import flash.display.Sprite; import flash.text.StyleSheet; import flash.text.engine.*; import import import import import flash.events.Event; flash.net.URLRequest; flash.net.URLLoader; flash.display.Sprite; flash.display.Graphics; public class StoryLayout extends Sprite { public var headlineTxt:HeadlineTextField; public var subtitleTxt:HeadlineTextField; public var storyTxt:MultiColumnText; public var sheet:StyleSheet; public var h1_ElFormat:ElementFormat; public var h2_ElFormat:ElementFormat; public var p_ElFormat:ElementFormat; private var loader:URLLoader; public public public public var var var var paddingLeft:Number; paddingRight:Number; paddingTop:Number; paddingBottom:Number; public var preferredWidth:Number; public var preferredHeight:Number; public var numColumns:int; public var bgColor:Number = 0xFFFFFF; Last updated 3/21/2011 396 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine public var headline:String = "News Layout Example"; public var subtitle:String = "This example formats text like a newspaper page using the Flash Text Engine API. "; public var rawTestData:String = "From the part Mr. Burke took in the American Revolution, it was natural that I should consider him a friend to mankind; and as our acquaintance commenced on that ground, it would have been more agreeable to me to have had cause to continue in that opinion than to change it. " + "At the time Mr. Burke made his violent speech last winter in the English Parliament against the French Revolution and the National Assembly, I was in Paris, and had written to him but a short time before to inform him how prosperously matters were going on. Soon after this I saw his advertisement of the Pamphlet he intended to publish: As the attack was to be made in a language but little studied, and less understood in France, and as everything suffers by translation, I promised some of the friends of the Revolution in that country that whenever Mr. Burke's Pamphlet came forth, I would answer it. This appeared to me the more necessary to be done, when I saw the flagrant misrepresentations which Mr. Burke's Pamphlet contains; and that while it is an outrageous abuse on the French Revolution, and the principles of Liberty, it is an imposition on the rest of the world. " + "I am the more astonished and disappointed at this conduct in Mr. Burke, as (from the circumstances I am going to mention) I had formed other expectations. " + "I had seen enough of the miseries of war, to wish it might never more have existence in the world, and that some other mode might be found out to settle the differences that should occasionally arise in the neighbourhood of nations. This certainly might be done if Courts were disposed to set honesty about it, or if countries were enlightened enough not to be made the dupes of Courts. The people of America had been bred up in the same prejudices against France, which at that time characterised the people of England; but experience and an acquaintance with the French Nation have most effectually shown to the Americans the falsehood of those prejudices; and I do not believe that a more cordial and confidential intercourse exists between any two countries than between America and France. "; public function StoryLayout(w:int = 400, h:int = 200, cols:int = 3, padding:int = 10):void { this.preferredWidth = w; this.preferredHeight = h; this.numColumns = cols; this.paddingLeft = padding; this.paddingRight = padding; this.paddingTop = padding; this.paddingBottom = padding; var req:URLRequest = new URLRequest("story.css"); loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onCSSFileLoaded); loader.load(req); } public function onCSSFileLoaded(event:Event):void { this.sheet = new StyleSheet(); this.sheet.parseCSS(loader.data); // convert headline styles to ElementFormat objects h1_ElFormat = getElFormat("h1", this.sheet); Last updated 3/21/2011 397 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine h1_ElFormat.typographicCase = TypographicCase.UPPERCASE; h2_ElFormat = getElFormat("h2", this.sheet); p_ElFormat = getElFormat("p", this.sheet); displayText(); } public function drawBackground():void { var h:Number = this.storyTxt.y + this.storyTxt.height + this.paddingTop + this.paddingBottom; var g:Graphics = this.graphics; g.beginFill(this.bgColor); g.drawRect(0, 0, this.width + this.paddingRight + this.paddingLeft, h); g.endFill(); } /** * Reads a set of style properties for a named style and then creates * a TextFormat object that uses the same properties. */ public function getElFormat(styleName:String, ss:StyleSheet):ElementFormat { var style:Object = ss.getStyle(styleName); if (style != null) { var colorStr:String = style.color; if (colorStr != null && colorStr.indexOf("#") == 0) { style.color = colorStr.substr(1); } var fd:FontDescription = new FontDescription( style.fontFamily, style.fontWeight, FontPosture.NORMAL, FontLookup.DEVICE, RenderingMode.NORMAL, CFFHinting.NONE); var format:ElementFormat = new ElementFormat(fd, style.fontSize, style.color, 1, TextRotation.AUTO, TextBaseline.ROMAN, TextBaseline.USE_DOMINANT_BASELINE, 0.0, Kerning.ON, 0.0, 0.0, "en", BreakOpportunity.AUTO, DigitCase.DEFAULT, DigitWidth.DEFAULT, LigatureLevel.NONE, TypographicCase.DEFAULT); if (style.hasOwnProperty("letterSpacing")) { Last updated 3/21/2011 398 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine format.trackingRight = style.letterSpacing; } } return format; } public function displayText():void { headlineTxt = new HeadlineTextField(h1_ElFormat,headline,this.preferredWidth); headlineTxt.x = this.paddingLeft; headlineTxt.y = 40 + this.paddingTop; headlineTxt.fitText(1); this.addChild(headlineTxt); subtitleTxt = new HeadlineTextField(h2_ElFormat,subtitle,this.preferredWidth); subtitleTxt.x = this.paddingLeft; subtitleTxt.y = headlineTxt.y + headlineTxt.height; subtitleTxt.fitText(2); this.addChild(subtitleTxt); storyTxt = new MultiColumnText(rawTestData, this.numColumns, 20, this.preferredWidth, this.preferredHeight, p_ElFormat); storyTxt.x = this.paddingLeft; storyTxt.y = subtitleTxt.y + subtitleTxt.height + 10; this.addChild(storyTxt); drawBackground(); } } } FormattedTextBlock.as is used as a base class for creating blocks of text. It also includes utility functions for changing font size and case. package com.example.programmingas3.newslayout { import flash.text.engine.*; import flash.display.Sprite; public class FormattedTextBlock extends Sprite { public var tb:TextBlock; private var te:TextElement; private var ef1:ElementFormat; private var textWidth:int; public var totalTextLines:int; public var blockText:String; public var leading:Number = 1.25; public var preferredWidth:Number = 720; public var preferredHeight:Number = 100; public function FormattedTextBlock(ef:ElementFormat,txt:String, colW:int = 0) { this.textWidth = (colW==0) ? preferredWidth : colW; blockText = txt; ef1 = ef; tb = new TextBlock(); Last updated 3/21/2011 399 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine tb.textJustifier = new SpaceJustifier("en",LineJustification.UNJUSTIFIED,false); te = new TextElement(blockText,this.ef1); tb.content = te; this.breakLines(); } private function breakLines() { var textLine:TextLine = null; var y:Number = 0; var lineNum:int = 0; while (textLine = tb.createTextLine(textLine,this.textWidth,0,true)) { textLine.x = 0; textLine.y = y; y += this.leading*textLine.height; this.addChild(textLine); } for (var i:int = 0; i < this.numChildren; i++) { TextLine(this.getChildAt(i)).validity = TextLineValidity.STATIC; } this.totalTextLines = this.numChildren; } private function rebreakLines() { this.clearLines(); this.breakLines(); } private function clearLines() { while(this.numChildren) { this.removeChildAt(0); } } Last updated 3/21/2011 400 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine public function changeSize(size:uint=12):void { if (size > 5) { var ef2:ElementFormat = ef1.clone(); ef2.fontSize = size; te.elementFormat = ef2; this.rebreakLines(); } } public function changeCase(newCase:String = "default"):void { var ef2:ElementFormat = ef1.clone(); ef2.typographicCase = newCase; te.elementFormat = ef2; } } } HeadlineTextBlock.as extends the FormattedTextBlock class and is used for creating headlines. It includes a function for fitting text within a defined space on the page. package com.example.programmingas3.newslayout { import flash.text.engine.*; public class HeadlineTextField extends FormattedTextBlock { public static var MIN_POINT_SIZE:uint = 6; public static var MAX_POINT_SIZE:uint = 128; public function HeadlineTextField(te:ElementFormat,txt:String,colW:int = 0) { super(te,txt); } public function fitText(maxLines:uint = 1, targetWidth:Number = -1):uint { if (targetWidth == -1) { targetWidth = this.width; } var pixelsPerChar:Number = targetWidth / this.blockText.length; var pointSize:Number = Math.min(MAX_POINT_SIZE, Math.round(pixelsPerChar * 1.8 * maxLines)); if (pointSize < 6) { // the point size is too small return pointSize; } this.changeSize(pointSize); if (this.totalTextLines > maxLines) Last updated 3/21/2011 401 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine { return shrinkText(--pointSize, maxLines); } else { return growText(pointSize, maxLines); } } public function growText(pointSize:Number, maxLines:uint = 1):Number { if (pointSize >= MAX_POINT_SIZE) { return pointSize; } this.changeSize(pointSize + 1); if (this.totalTextLines > maxLines) { // set it back to the last size this.changeSize(pointSize); return pointSize; } else { return growText(pointSize + 1, maxLines); } } public function shrinkText(pointSize:Number, maxLines:uint=1):Number { if (pointSize <= MIN_POINT_SIZE) { return pointSize; } this.changeSize(pointSize); if (this.totalTextLines > maxLines) { return shrinkText(pointSize - 1, maxLines); } else { return pointSize; } } } } MultiColumnText.as handles formatting text within a multicolumn design. It demonstrates the flexible use a TextBlock object as a factory for creating, formatting, and placing text lines. Last updated 3/21/2011 402 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine package com.example.programmingas3.newslayout { import flash.display.Sprite; import flash.text.engine.*; public class MultiColumnText extends Sprite { private var tb:TextBlock; private var te:TextElement; private var numColumns:uint = 2; private var gutter:uint = 10; private var leading:Number = 1.25; private var preferredWidth:Number = 400; private var preferredHeight:Number = 100; private var colWidth:int = 200; public function MultiColumnText(txt:String = "",cols:uint = 2, gutter:uint = 10, w:Number = 400, h:Number = 100, ef:ElementFormat = null):void { this.numColumns = Math.max(1, cols); this.gutter = Math.max(1, gutter); this.preferredWidth = w; this.preferredHeight = h; this.setColumnWidth(); var field:FormattedTextBlock = new FormattedTextBlock(ef,txt,this.colWidth); var totLines:int = field.totalTextLines; field = null; var linesPerCol:int = Math.ceil(totLines/cols); tb = new TextBlock(); te = new TextElement(txt,ef); tb.content = te; var textLine:TextLine = null; var x:Number = 0; var y:Number = 0; var i:int = 0; var j:int = 0; while (textLine = tb.createTextLine(textLine,this.colWidth,0,true)) { textLine.x = Math.floor(i/(linesPerCol+1))*(this.colWidth+this.gutter); textLine.y = y; y += this.leading*textLine.height; Last updated 3/21/2011 403 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Flash Text Engine j++; if(j>linesPerCol) { y = 0; j = 0; } i++; this.addChild(textLine); } } private function setColumnWidth():void { this.colWidth = Math.floor( (this.preferredWidth ((this.numColumns - 1) * this.gutter)) / this.numColumns); } } } Last updated 3/21/2011 404 Chapter 21: Using the Text Layout Framework Flash Player 10 and later, Adobe AIR 1.5 and later Overview of the Text Layout Framework Flash Player 10 and later, Adobe AIR 1.5 and later The Text Layout Framework (TLF) is an extensible ActionScript library. The TLF is built on the text engine in Adobe® Flash® Player 10 and Adobe® AIR® 1.5. The TLF provides advanced typographic and text layout features for innovative typography on the web. The framework can be used with Adobe® Flex® or Adobe® Flash® Professional. Developers can use or extend existing components, or they can use the framework to create their own text components. The TLF includes the following capabilities: • Bidirectional text, vertical text, and over 30 writing scripts including Arabic, Hebrew, Chinese, Japanese, Korean, Thai, Lao, Vietnamese, and others • Selection, editing, and flowing text across multiple columns and linked containers • Vertical text, Tate-Chu-Yoko (horizontal within vertical text) and justifier for East Asian typography • Rich typographical controls, including kerning, ligatures, typographic case, digit case, digit width, and discretionary hyphens • Cut, copy, paste, undo, and standard keyboard and mouse gestures for editing • Rich developer APIs to manipulate text content, layout, and markup and create custom text components • Robust list support including custom markers and numbering formats • Inline images and positioning rules The TLF is an ActionScript 3.0 library built on the Flash Text Engine (FTE) introduced in Flash Player 10. FTE can be accessed through the flash.text.engine package, which is part of the Flash Player 10 Application Programming Interface (API). The Flash Player API, however, provides low-level access to the text engine, which means that some tasks can require a relatively large amount of code. The TLF encapsulates the low-level code into simpler APIs. The TLF also provides a conceptual architecture that organizes the basic building blocks defined by FTE into a system that is easier to use. Unlike FTE, the TLF is not built in to Flash Player. Rather, it is an independent component library written entirely in ActionScript 3.0. Because the framework is extensible, it can be customized for specific environments. Both Flash Professional and the Flex SDK include components that are based on the TLF framework. More Help topics "Flow" TLF markup application Last updated 3/21/2011 405 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Complex script support Flash Player 10 and later, Adobe AIR 1.5 and later The TLF provides complex script support. Complex script support includes the ability to display and edit right-to-left scripts. The TLF also provides the ability to display and edit a mixture of left-to-right and right-to-left scripts such as Arabic and Hebrew. The framework not only supports vertical text layout for Chinese, Japanese, and Korean, but also supports tate-chu-yoko (TCY elements). TCY elements are blocks of horizontal text embedded into vertical runs of text. The following scripts are supported: • Latin (English, Spanish, French, Vietnamese, and so on) • Greek, Cyrillic, Armenian, Georgian, and Ethiopic • Arabic and Hebrew • Han ideographs and Kana (Chinese, Japanese, and Korean) and Hangul Johab (Korean) • Thai, Lao, and Khmer • Devanagari, Bengali, Gurmukhi, Malayalam, Telugu, Tamil, Gujarati, Oriya, Kannada, and Tibetan • Tifinagh, Yi, Cherokee, Canadian Syllabics, Deseret, Shavian, Vai, Tagalog, Hanunoo, Buhid, and Tagbanwa Using the Text Layout Framework in Flash Professional and Flex You can use the TLF classes directly to create custom components in Flash. In addition, Flash Professional CS5 provides a new class, fl.text.TLFTextField, that encapsulates the TLF functionality. Use the TLFTextField class to create text fields in ActionScript that use the advanced text display features of the TLF. Create a TLFTextField object the same way you create a text field with the TextField class. Then, use the textFlow property to assign advanced formatting from the TLF classes. You can also use Flash Professional to create the TLFTextField instance on the stage using the text tool. Then you can use ActionScript to control the formatting and layout of the text field content using the TLF classes. For more information, see TLFTextField in the ActionScript 3.0 Reference for the Adobe Flash Platform. If you are working in Flex, use the TLF classes. For more information, see “Using the Text Layout Framework” on page 405. Using the Text Layout Framework Flash Player 10 and later, Adobe AIR 1.5 and later If you are working in Flex or are building custom text components, use the TLF classes. The TLF is an ActionScript 3.0 library contained entirely within the textLayout.swc library. The TLF library contains about 100 ActionScript 3.0 classes and interfaces organized into ten packages. These packages are subpackages of the flashx.textLayout package. The Text Layout Framework classes Flash Player 10 and later, Adobe AIR 1.5 and later The TLF classes can be grouped into three categories: • Data structures and formatting classes Last updated 3/21/2011 406 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework • Rendering classes • User interaction classes Data structures and formatting classes The following packages contain the data structures and formatting classes for the TLF: • flashx.textLayout.elements • flashx.textLayout.formats • flashx.textLayout.conversion The main data structure of the TLF is the text flow hierarchy, which is defined in the elements package. Within this structure, you can assign styles and attributes to runs of text with the formats package. You can also control how text is imported to, and exported from, the data structure with the conversion package. Rendering classes The following packages contain the rendering classes for the TLF: • flashx.textLayout.factory • flashx.textLayout.container • flashx.textLayout.compose The classes in these packages facilitate the rendering of text for display by Flash Player. The factory package provides a simple way to display static text. The container package includes classes and interfaces that define display containers for dynamic text. The compose package defines techniques for positioning and displaying dynamic text in containers. User interaction classes The following packages contain the user interaction classes for the TLF: • flashx.textLayout.edit • flashx.textLayout.operations • flashx.textLayout.events The edit and operations packages define classes that you can use to allow editing of text stored in the data structures. The events package contains event handling classes. General steps for creating text with the Text Layout Framework The following steps describe the general process for creating text with the Text Layout Format: 1 Import formatted text into the TLF data structures. For more information, see “Structuring text with TLF” on page 410 and “Formatting text with TLF” on page 414. 2 Create one or more linked display object containers for the text. For more information, see “Managing text containers with TLF” on page 415. 3 Associate the text in the data structures with the containers and set editing and scrolling options. For more information, see “Enabling text selection, editing, and undo with TLF” on page 416. 4 Create an event handler to reflow the text in response to resize (or other) events. For more information, see “Event handling with TLF” on page 417. Last updated 3/21/2011 407 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Text Layout Framework example: News layout Flash Player 10 and later, Adobe AIR 1.5 and later The following example demonstrates using the TLF to lay out a simple newspaper page. The page includes a large headline, a subhead, and a multicolumn body section: package { import import import import import import import import import import import flash.display.Sprite; flash.display.StageAlign; flash.display.StageScaleMode; flash.events.Event; flash.geom.Rectangle; flashx.textLayout.compose.StandardFlowComposer; flashx.textLayout.container.ContainerController; flashx.textLayout.container.ScrollPolicy; flashx.textLayout.conversion.TextConverter; flashx.textLayout.elements.TextFlow; flashx.textLayout.formats.TextLayoutFormat; public class TLFNewsLayout extends Sprite { private var hTextFlow:TextFlow; private var headContainer:Sprite; private var headlineController:ContainerController; private var hContainerFormat:TextLayoutFormat; private private private private var var var var bTextFlow:TextFlow; bodyTextContainer:Sprite; bodyController:ContainerController; bodyTextContainerFormat:TextLayoutFormat; private const headlineMarkup:String = "TLF News Layout ExampleThis example formats text like a newspaper page with a headline, a subtitle, and multiple columns"; private const bodyMarkup:String = "There are many such lime-kilns in that tract of country, for the purpose of burning the white marble which composes a large part of the substance of the hills. Some of them, built years ago, and long deserted, with weeds growing in the vacant round of the interior, which is open to the sky, and grass and wild-flowers rooting themselves into the chinks of the stones, look already like relics of antiquity, and may yet be overspread with the lichens of centuries to come. Others, where the lime-burner still feeds his daily and nightlong fire, afford points of interest to the wanderer among the hills, who seats himself on a log of wood or a fragment of marble, to hold a chat with the solitary man. It is a lonesome, and, when the character is inclined to thought, may be an intensely thoughtful occupation; as it proved in the case of Ethan Brand, who had mused to such strange purpose, in days gone by, while the fire in this very kiln was burning.The man who now watched the fire was of a different order, and troubled himself with no thoughts save the very few that were requisite to his business. At frequent intervals, he flung back the clashing weight of the iron door, and, turning his face Last updated 3/21/2011 408 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework from the insufferable glare, thrust in huge logs of oak, or stirred the immense brands with a long pole. Within the furnace were seen the curling and riotous flames, and the burning marble, almost molten with the intensity of heat; while without, the reflection of the fire quivered on the dark intricacy of the surrounding forest, and showed in the foreground a bright and ruddy little picture of the hut, the spring beside its door, the athletic and coal-begrimed figure of the lime-burner, and the half-frightened child, shrinking into the protection of his father's shadow. And when again the iron door was closed, then reappeared the tender light of the halffull moon, which vainly strove to trace out the indistinct shapes of the neighboring mountains; and, in the upper sky, there was a flitting congregation of clouds, still faintly tinged with the rosy sunset, though thus far down into the valley the sunshine had vanished long and long ago."; public function TLFNewsLayout() { //wait for stage to exist addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } private function onAddedToStage(evtObj:Event):void { removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // Headline text flow and flow composer hTextFlow = TextConverter.importToFlow(headlineMarkup, TextConverter.TEXT_LAYOUT_FORMAT); // initialize the headline container and controller objects headContainer = new Sprite(); headlineController = new ContainerController(headContainer); headlineController.verticalScrollPolicy = ScrollPolicy.OFF; hContainerFormat = new TextLayoutFormat(); hContainerFormat.paddingTop = 4; hContainerFormat.paddingRight = 4; hContainerFormat.paddingBottom = 4; hContainerFormat.paddingLeft = 4; headlineController.format = hContainerFormat; hTextFlow.flowComposer.addController(headlineController); addChild(headContainer); stage.addEventListener(flash.events.Event.RESIZE, resizeHandler); // Body text TextFlow and flow composer bTextFlow = TextConverter.importToFlow(bodyMarkup, TextConverter.TEXT_LAYOUT_FORMAT); // The body text container is below, and has three columns bodyTextContainer = new Sprite(); bodyController = new ContainerController(bodyTextContainer); bodyTextContainerFormat = new TextLayoutFormat(); bodyTextContainerFormat.columnCount = 3; bodyTextContainerFormat.columnGap = 30; bodyController.format = bodyTextContainerFormat; bTextFlow.flowComposer.addController(bodyController); addChild(bodyTextContainer); Last updated 3/21/2011 409 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework resizeHandler(null); } private function resizeHandler(event:Event):void { const verticalGap:Number = 25; const stagePadding:Number = 16; var stageWidth:Number = stage.stageWidth - stagePadding; var stageHeight:Number = stage.stageHeight - stagePadding; var headlineWidth:Number = stageWidth; var headlineContainerHeight:Number = stageHeight; // Initial compose to get height of headline after resize headlineController.setCompositionSize(headlineWidth, headlineContainerHeight); hTextFlow.flowComposer.compose(); var rect:Rectangle = headlineController.getContentBounds(); headlineContainerHeight = rect.height; // Resize and place headline text container // Call setCompositionSize() again with updated headline height headlineController.setCompositionSize(headlineWidth, headlineContainerHeight ); headlineController.container.x = stagePadding / 2; headlineController.container.y = stagePadding / 2; hTextFlow.flowComposer.updateAllControllers(); // Resize and place body text container var bodyContainerHeight:Number = (stageHeight - verticalGap headlineContainerHeight); bodyController.format = bodyTextContainerFormat; bodyController.setCompositionSize(stageWidth, bodyContainerHeight ); bodyController.container.x = (stagePadding/2); bodyController.container.y = (stagePadding/2) + headlineContainerHeight + verticalGap; bTextFlow.flowComposer.updateAllControllers(); } } } The TLFNewsLayout class uses two text containers. One container displays a headline and subhead, and the other displays three-column body text. For simplicity, the text is hard-coded into the example as TLF Markup text. The headlineMarkup variable contains both the headline and the subhead, and the bodyMarkup variable contains the body text. For more information on TLF Markup, see “Structuring text with TLF” on page 410. After some initialization, the onAddedToStage() function imports the headline text into a TextFlow object, which is the main data structure of the TLF: hTextFlow = TextConverter.importToFlow(headlineMarkup, TextConverter.TEXT_LAYOUT_FORMAT); Next, a Sprite object is created for the container, and a controller is created and associated with the container: headContainer = new Sprite(); headlineController = new ContainerController(headContainer); The controller is initialized to set formatting, scrolling, and other options. The controller contains geometry that defines the bounds of the container that the text flows into. A TextLayoutFormat object contains the formatting options: Last updated 3/21/2011 410 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework hContainerFormat = new TextLayoutFormat(); The controller is assigned to the flow composer and the function adds the container to the display list. The actual composition and display of the containers is deferred to the resizeHandler() method. The same sequence of steps is performed to initialize the body TextFlow object. The resizeHandler() method measures the space available for rendering the containers and sizes the containers accordingly. An initial call to the compose() method allows for the calculation of the proper height of the headline container. The resizeHandler() method can then place and display the headline container with the updateAllControllers() method. Finally, the resizeHandler() method uses the size of the headline container to determine the placement of the body text container. Structuring text with TLF The TLF uses a hierarchical tree to represent text. Each node in the tree is an instance of a class defined in the elements package. For example, the root node of the tree is always an instance of the TextFlow class. The TextFlow class represents an entire story of text. A story is a collection of text and other elements that is treated as one unit, or flow. A single story can require more than one column or text container to display. Apart from the root node, the remaining elements are loosely based on XHTML elements. The following diagram shows the hierarchy of the framework: TextFlow Hierarchy Text Layout Framework markup Understanding the structure of the TLF is also helpful when dealing with TLF Markup. TLF Markup is an XML representation of text that is part of the TLF. Although the framework also supports other XML formats, TLF Markup is unique in that it is based specifically on the structure of the TextFlow hierarchy. If you export XML from a TextFlow using this markup format, the XML is exported with this hierarchy intact. TLF Markup provides the highest fidelity representation of text in a TextFlow hierarchy. The markup language provides tags for each of the TextFlow hierarchy’s basic elements, and also provides attributes for all formatting properties available in the TextLayoutFormat class. The following table contains the tags that can be used in TLF Markup. Last updated 3/21/2011 411 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Element Description Children Class textflow The root element of the markup. div, p TextFlow div A division within a TextFlow. May contain a group of paragraphs. div, list, p DivElement p A paragraph. a, tcy, span, img, tab, br, ParagraphElement g a A link. tcy, span, img, tab, br, g LinkElement tcy A run of horizontal text (used in a vertical TextFlow). a, span, img, tab, br, g TCYElement span A run of text within a paragraph. SpanElement img An image in a paragraph. InlineGraphicElement tab A tab character. TabElement br A break character. Used for ending a line within a paragraph; text continues on the next line, but remains in the same paragraph. BreakElement linkNormalFormat Defines the formatting attributes used for links in normal state. TextLayoutFormat TextLayoutFormat linkActiveFormat Defines the formatting attributes used for TextLayoutFormat links in active state, when the mouse is down on a link. TextLayoutFormat linkHoverFormat Defines the formatting attributes used for TextLayoutFormat links in hover state, when the mouse is within the bounds (rolling over) a link. TextLayoutFormat li A list item element. Must be inside a list element. ListItemElement list A list. Lists can be nested, or placed adjacent div, li, list, p to each other. Different labeling or numbering schemes can be applied to the list items. g A group element. Used for grouping elements in a paragraph. The lets you nest elements below the paragraph level. div, li, list, p ListElement a, tcy, span, img, tab, br, SubParagraphGroupE g lement More Help topics TLF 2.0 Lists Markup TLF 2.0 SubParagraphGroupElements and typeName Using numbered and bulleted lists You can use the ListElement and ListItemElement classes to add bulleted lists to your text controls. The bulleted lists can be nested and can be customized to use different bullets (or markers) and auto-numbering, as well as outline-style numbering. To create lists in your text flows, use the tag. You then use
  • tags within the tag for each list item in the list. You can customize the appearance of the bullets by using the ListMarkerFormat class. The following example creates simple lists: Last updated 3/21/2011 412 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Item 1 Item 2 Item 3 You can nest lists within other lists, as the following example shows: Item 1 Item 1a Item 1b Item 1c Item 2 Item 3 To customize the type of marker in the list, use the listStyleType property of the ListElement. This property can be any value defined by the ListStyleType class (such as check, circle, decimal, and box). The following example creates lists with various marker types and a custom counter increment: upperAlpha item another lowerAlpha item another upperRoman item another lowerRoman item another You use the ListMarkerFormat class to define the counter. In addition to defining the increment of a counter, you can also customize the counter by resetting it with the counterReset property. You can further customize the appearance of the markers in your lists by using the beforeContent and afterContent properties of the ListMarkerFormat. These properties apply to content that appears before and after the content of the marker. The following example adds the string “XX” before the marker, and the string “YY” after the marker: Item 1 Item 2 Item 3 The content property itself can define further customizations of the marker format. The following example displays an ordered, uppercase Roman numeral marker: Last updated 3/21/2011 413 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Item 1
  • Item 2 Item 3 As the previous example shows, the content property can also insert a suffix: a string that appears after the marker, but before the afterContent. To insert this string when providing XML content to the flow, wrap the string in "e; HTML entities rather than quotation marks (""). More Help topics TLF 2.0 Lists Markup Using padding in TLF Each FlowElement supports padding properties that you use to control the position of each element’s content area, and the space between the content areas. The total width of an element is the sum of its content’s width, plus the paddingLeft and paddingRight properties. The total height of an element is the sum of its content’s height, plus the paddingTop and paddingBottom properties. The padding is the space between the border and the content. The padding properties are paddingBottom, paddingTop, paddingLeft, and paddingRight. Padding can be applied to the TextFlow object, as well as the following child elements: • div • img • li • list • p Padding properties cannot be applied to span elements. The following example sets padding properties on the TextFlow: Valid values for the padding properties are a number (in pixels), “auto”, or “inherit”. The default value is “auto”, which means it is calculated automatically and set to 0, for all elements except the ListElement. For ListElements, “auto” is 0 except on the start side of the list where the value of the listAutoPadding property is used. The default value of listAutoPadding is 40, which gives lists a default indent. The padding properties do not, by default, inherit. The “auto” and “inherit” values are constants defined by the FormatValue class. Padding properties can be negative values. Last updated 3/21/2011 414 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework More Help topics Padding changes in TLF 2.0 Formatting text with TLF Flash Player 10 and later, Adobe AIR 1.5 and later The flashx.textLayout.formats package contains interfaces and classes that allow you to assign formats to any FlowElement in the text flow hierarchy tree. There are two ways to apply formatting. You can assign a specific format individually or assign a group of formats simultaneously with a special formatting object. The ITextLayoutFormat interface contains all of the formats that can be applied to a FlowElement. Some formats apply to an entire container or paragraph of text, but do not logically apply to individual characters. For example, formats such as justification and tab stops apply to whole paragraphs, but are not applicable to individual characters. Assigning formats to a FlowElement with properties Flash Player 10 and later, Adobe AIR 1.5 and later You can set formats on any FlowElement through property assignment. The FlowElement class implements the ITextLayoutFormat interface, so any subclass of the FlowElement class must also implement that interface. For example, the following code shows how to assign individual formats to an instance of ParagraphElement: var p:ParagraphElement = new ParagraphElement(); p.fontSize = 18; p.fontFamily = "Arial"; Assigning formats to a FlowElement with the TextLayoutFormat class Flash Player 10 and later, Adobe AIR 1.5 and later You can apply formats to a FlowElement with the TextLayoutFormat class. You use this class to create a special formatting object that contains all of the formatting values you want. You can then assign that object to the format property of any FlowElement object. Both TextLayoutFormat and FlowElement implement the ITextLayoutFormat interface. This arrangement ensures that both classes contain the same format properties. For more information, see TextLayoutFormat in the ActionScript 3.0 Reference for the Adobe Flash Platform. Format inheritance Flash Player 10 and later, Adobe AIR 1.5 and later Formats are inherited through the text flow hierarchy. If you assign an instance of TextLayoutFormat to a FlowElement instance with children, the framework initiates a process called a cascade. During a cascade, the framework recursively examines each node in the hierarchy that inherits from your FlowElement. It then determines whether to assign the inherited values to each formatting property. The following rules are applied during the cascade: 1 Property values are inherited only from an immediate ancestor (sometimes called the parent). 2 Property values are inherited only if a property does not already have a value (that is, the value is undefined). Last updated 3/21/2011 415 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework 3 Some attributes do not inherit values when undefined, unless the attribute’s value is set to “inherit” or the constant flashx.textLayout.formats.FormatValue.INHERIT. For example, if you set the fontSize value at the TextFlow level, the setting applies to all elements in the TextFlow. In other words, the values cascade down the text flow hierarchy. You can, however, override the value in a given element by assigning a new value directly to the element. As a counter-example, if you set the backgroundColor value for at the TextFlow level, the children of the TextFlow do not inherit that value. The backgroundColor property is one that does not inherit from its parent during a cascade. You can override this behavior by setting the backgroundColor property on each child to flashx.textLayout.formats.FormatValue.INHERIT. For more information, see TextLayoutFormat in the ActionScript 3.0 Reference for the Adobe Flash Platform. Importing and exporting text with TLF Flash Player 10 and later, Adobe AIR 1.5 and later The TextConverter class in the flashx.textLayout.conversion.* package lets you import text to, and export text from, the TLF. Use this class if you plan to load text at runtime instead of compiling the text into the SWF file. You can also use this class to export text that is stored in a TextFlow instance into a String or XML object. Both import and export are straightforward procedures. You call either the export() method or the importToFlow() method, both of which are part of the TextConverter class. Both methods are static, which means that you call the methods on the TextConverter class rather than on an instance of the TextConverter class. The classes in the flashx.textLayout.conversion package provide considerable flexibility in where you choose to store your text. For example, if you store your text in a database, you can import the text into the framework for display. You can then use the classes in the flashx.textLayout.edit package to change the text, and export the changed text back to your database. For more information, see flashx.textLayout.conversion in the ActionScript 3.0 Reference for the Adobe Flash Platform. Managing text containers with TLF Flash Player 10 and later, Adobe AIR 1.5 and later Once text is stored in the TLF data structures, Flash Player can display it. The text that is stored in the flow hierarchy must be converted into a format that Flash Player can display. The TLF offers two ways to create display objects from a flow. The first, more simple approach is suitable for displaying static text. The second, more complicated approach lets you create dynamic text that can be selected and edited. In both cases, the text is ultimately converted into instances of the TextLine class, which is part of the flash.text.engine.* package in Flash Player 10. Creating static text The simple approach uses the TextFlowTextLineFactory class, which can be found in the flashx.textLayout.factory package. The advantage of this approach, beyond its simplicity, is that it has a smaller memory footprint than does the FlowComposer approach. This approach is advisable for static text that the user does not need to edit, select, or scroll. For more information, see TextFlowTextLineFactory in the ActionScript 3.0 Reference for the Adobe Flash Platform. Last updated 3/21/2011 416 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Creating dynamic text and containers Use a flow composer if you want to have more control over the display of text than that provided by TextFlowTextLineFactory. For example, with a flow composer, your users can select and edit the text. For more information, see “Enabling text selection, editing, and undo with TLF” on page 416. A flow composer is an instance of the StandardFlowComposer class in the flashx.textLayout.compose package. A flow composer manages the conversion of TextFlow into TextLine instances, and also the placement of those TextLine instances into one or more containers. TextFlow Stage IFlowComposer Sprite ContainerController TextLine TextLine An IFlowComposer has zero or more ContainerControllers Every TextFlow instance has a corresponding object that implements the IFlowComposer interface. This IFlowComposer object is accessible through the TextFlow.flowComposer property. You can call methods defined by the IFlowComposer interface through this property. These methods allow you to associate the text with one or more containers and prepare the text for display within a container. A container is an instance of the Sprite class, which is a subclass of the DisplayObjectContainer class. Both of these classes are part of the Flash Player display list API. A container is a more advanced form of the bounding rectangle used in with TextLineFactory class. Like the bounding rectangle, a container defines the area where TextLine instances appear. Unlike a bounding rectangle, a container has a corresponding “controller” object. The controller manages scrolling, composition, linking, formatting, and event handling for a container or set of containers. Each container has a corresponding controller object that is an instance of the ContainerController class in the flashx.textLayout.container package. To display text, create a controller object to manage the container and associate it with the flow composer. Once you have the container associated, compose the text so that it can be displayed. Accordingly, containers have two states: composition and display. Composition is the process of converting the text from the text flow hierarchy into TextLine instances and calculating whether those instances fit into the container. Display is the process of updating the Flash Player display list. For more information, see IFlowComposer, StandardFlowComposer, and ContainerController in the ActionScript 3.0 Reference for the Adobe Flash Platform. Enabling text selection, editing, and undo with TLF Flash Player 9.0 and later, Adobe AIR 1.0 and later The ability to select or edit text is controlled at the text flow level. Every instance of the TextFlow class has an associated interaction manager. You can access a TextFlow object’s interaction manager through the object’s TextFlow.interactionManager property. To enable text selection, assign an instance of the SelectionManager class to the interactionManager property. To enable both text selection and editing, assign an instance of the EditManager class instead of an instance of the SelectionManager class. To enable undo operations, create an instance of the UndoManager class and include it as an argument when calling the constructor for EditManager. The UndoManager class maintains a history of the user's most recent editing activities and lets the user undo or redo specific edits. All three of these classes are part of the edit package. Last updated 3/21/2011 417 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework For more information, see SelectionManager, EditManager, and UndoManager in the ActionScript 3.0 Reference for the Adobe Flash Platform. Event handling with TLF Flash Player 10 and later, Adobe AIR 1.5 and later TextFlow objects dispatch events in many circumstances, including: • When the text or layout changes • Before an operation begins and after an operation completes • When the status of a FlowElement object changes • When a compose operation completes For more information, see flashx.textLayout.events in the ActionScript 3.0 Reference for the Adobe Flash Platform. More Help topics TLF FlowElement and LinkElement Events and EventMirrors Positioning images within text To position the InlineGraphicElement within the text, you use the following properties: • float property of the InlineGraphicElement class • clearFloats property of the FlowElement The float property controls the placement of the graphic and the text around it. The clearFloats property controls the placement of the paragraph elements relative to the float. To control the location of an image within a text element, you use the float property. The following example adds an image to a paragraph and aligns it to the left so the text wraps around the right: Images in a flow are a good thing. For example, here is a float. It should show on the left: Don't you agree? Another sentence here. Another sentence here. Another sentence here. Another sentence here. Another sentence here. Another sentence here. Another sentence here. Another sentence here. Valid values for the float property are “left”, “right”, “start”, “end”, and “none”. The Float class defines these constants. The default value is “none”. The clearFloats property is useful in cases where you want to adjust the starting position of subsequent paragraphs that would normally wrap around the image. For example, assume that you have an image that is larger than the first paragraph. To be sure the second paragraph starts after the image, set the clearFloats property. The following example uses an image that is taller than the text in the first paragraph. To get the second paragraph to start after the image in the text block, this example sets the clearFloats property on the second paragraph to “end”. Last updated 3/21/2011 418 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Using the Text Layout Framework Here is another float, it should show up on the right: We'll add another paragraph that should clear past it.This should appear after the previous float on the right. Valid values for the clearFloats property are “left”, “right”, “end”, “start”, “none”, and “both”. The ClearFloats class defines these constants. You can also set the clearFloats property to “inherit”, which is a constant defined by the FormatValue class. The default value is “none”. More Help topics TLF Floats Last updated 3/21/2011 419 Chapter 22: Working with sound Flash Player 9 and later, Adobe AIR 1.0 and later ActionScript is made for immersive, interactive applications—and one often overlooked element of powerfully immersive applications is sound. You can add sound effects to a video game, audio feedback to an application user interface, or even make a program that analyzes mp3 files loaded over the Internet, with sound at the core of the application. You can load external audio files and work with audio that’s embedded in a SWF. You can control the audio, create visual representations of the sound information, and capture sound from a user’s microphone. More Help topics flash.media package flash.events.SampleDataEvent Basics of working with sound Flash Player 9 and later, Adobe AIR 1.0 and later Computers can capture and encode digital audio—computer representation of sound information—and can store it and retrieve it to play back over speakers. You can play back sound using either Adobe® Flash® Player or Adobe® AIR™ and ActionScript. When sound data is converted to digital form, it has various characteristics, such as the sound’s volume and whether it is stereo or mono sound. When you play back a sound in ActionScript, you can adjust these characteristics as well— make the sound louder, or make it seem to be coming from a certain direction, for instance. Before you can control a sound in ActionScript, you need to have the sound information loaded into Flash Player or AIR. There are five ways you can get audio data into Flash Player or AIR so that you can work with it using ActionScript. • Load an external sound file such as an mp3 file into the SWF. • Embed the sound information into the SWF file directly when it’s being created. • Capture audio from a microphone attached to a user’s computer. • Stream audio from a server. • Dynamically generate and play audio. When you load sound data from an external sound file, you can begin playing back the start of the sound file while the rest of the sound data is still loading. Although there are various sound file formats used to encode digital audio, ActionScript 3.0, Flash Player and AIR support sound files that are stored in the mp3 format. They cannot directly load or play sound files in other formats like WAV or AIFF. Last updated 3/21/2011 420 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound While you’re working with sound in ActionScript, you’ll likely work with several classes from the flash.media package. The Sound class is the class you use to get access to audio information by loading a sound file or assigning a function to an event that samples sound data and then starting playback. Once you start playing a sound, Flash Player and AIR give you access to a SoundChannel object. Since an audio file that you’ve loaded may only be one of several sounds that you play on a user’s computer, each individual sound that’s playing uses its own SoundChannel object; the combined output of all the SoundChannel objects mixed together is what actually plays over the computer’s speakers. You use this SoundChannel instance to control properties of the sound and to stop its playback. Finally, if you want to control the combined audio, the SoundMixer class gives you control over the mixed output. You can also use several other classes to perform more specific tasks when you’re working with sound in ActionScript; for more information on all the sound-related classes, see “Understanding the sound architecture” on page 420. Important concepts and terms The following reference list contains important terms that you may encounter: Amplitude The distance of a point on the sound waveform from the zero or equilibrium line. Bit rate The amount of data that is encoded or streamed for each second of a sound file. For mp3 files, the bit rate is usually stated in terms of thousands of bits per second (kbps). A higher bit rate generally means a higher quality sound wave. Buffering The receiving and storing of sound data before it is played back. mp3 MPEG-1 Audio Layer 3, or mp3, is a popular sound compression format. Panning The positioning of an audio signal between the left and right channels in a stereo soundfield. Peak The highest point in a waveform. Sampling rate Defines the number of samples per second taken from an analog audio signal to make a digital signal. The sampling rate of standard compact disc audio is 44.1 kHz or 44,100 samples per second. Streaming The process of playing the early portions of a sound file or video file while later portions of that file are still being loaded from a server. Volume The loudness of a sound. Waveform The shape of a graph of the varying amplitudes of a sound signal over time. Understanding the sound architecture Flash Player 9 and later, Adobe AIR 1.0 and later Your applications can load sound data from five main sources: • External sound files loaded at run time • Sound resources embedded within the application’s SWF file • Sound data from a microphone attached to the user’s system • Sound data streamed from a remote media server, such as Flash Media Server • Sound data being generated dynamically through the use of the sampleData event handler Sound data can be fully loaded before it is played back, or it can be streamed, meaning that it is played back while it is still loading. Last updated 3/21/2011 421 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound The ActionScript 3.0 sound classes support sound files that are stored in the mp3 format. They cannot directly load or play sound files in other formats, such as WAV or AIFF. However, starting with Flash Player 9.0.115.0, AAC audio files can be loaded and played using the NetStream class. This is the same technique as is used for loading and playing video content. For more information on this technique, see “Working with video” on page 452. Using Adobe Flash Professional, you can import WAV or AIFF sound files and then embed them into your application’s SWF files in the mp3 format. The Flash Authoring tool also lets you compress embedded sound files to reduce their file size, though this size reduction comes at the expense of sound quality. For more information see “Importing Sounds” in Using Flash. The ActionScript 3.0 sound architecture makes use of the following classes in the flash.media package. Class Description flash.media.Sound The Sound class handles the loading of sound, manages basic sound properties, and starts a sound playing. flash.media.SoundChannel When an application plays a Sound object, a new SoundChannel object is created to control the playback. The SoundChannel object controls the volume of both the left and right playback channels of the sound. Each sound that plays has its own SoundChannel object. flash.media.SoundLoaderContext The SoundLoaderContext class specifies how many seconds of buffering to use when loading a sound, and whether Flash Player or AIR looks for a policy file from the server when loading a file. A SoundLoaderContext object is used as a parameter to the Sound.load() method. flash.media.SoundMixer The SoundMixer class controls playback and security properties that pertain to all sounds in an application. In effect, multiple sound channels are mixed through a common SoundMixer object, so property values in the SoundMixer object will affect all SoundChannel objects that are currently playing. flash.media.SoundTransform The SoundTransform class contains values that control sound volume and panning. A SoundTransform object can be applied to an individual SoundChannel object, to the global SoundMixer object, or to a Microphone object, among others. flash.media.ID3Info An ID3Info object contains properties that represent ID3 metadata information that is often stored in mp3 sound files. flash.media.Microphone The Microphone class represents a microphone or other sound input device attached to the user’s computer. Audio input from a microphone can be routed to local speakers or sent to a remote server. The Microphone object controls the gain, sampling rate, and other characteristics of its own sound stream. Each sound that is loaded and played needs its own instance of the Sound class and the SoundChannel class. The output from multiple SoundChannel instances is then mixed together by the global SoundMixer class during playback, The Sound, SoundChannel, and SoundMixer classes are not used for sound data obtained from a microphone or from a streaming media server like Flash Media Server. Loading external sound files Flash Player 9 and later, Adobe AIR 1.0 and later Each instance of the Sound class exists to load and trigger the playback of a specific sound resource. An application can’t reuse a Sound object to load more than one sound. If it wants to load a new sound resource, it should create a new Sound object. If you are loading a small sound file, such as a click sound to be attached to a button, your application can create a new Sound and have it automatically load the sound file, as shown below: Last updated 3/21/2011 422 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound var req:URLRequest = new URLRequest("click.mp3"); var s:Sound = new Sound(req); The Sound() constructor accepts a URLRequest object as its first parameter. When a value for the URLRequest parameter is supplied, the new Sound object starts loading the specified sound resource automatically. In all but the simplest cases, your application should pay attention to the sound’s loading progress and watch for errors during loading. For example, if the click sound is fairly large, it might not be completely loaded by the time the user clicks the button that triggers the sound. Trying to play an unloaded sound could cause a run-time error. It’s safer to wait for the sound to load completely before letting users take actions that might start sounds playing. A Sound object dispatches a number of different events during the sound loading process. Your application can listen for these events to track loading progress and make sure that the sound loads completely before playing. The following table lists the events that can be dispatched by a Sound object. Event Description open (Event.OPEN) Dispatched right before the sound loading operation begins. progress (ProgressEvent.PROGRESS) Dispatched periodically during the sound loading process when data is received from the file or stream. id3 (Event.ID3) Dispatched when ID3 data is available for an mp3 sound. complete (Event.COMPLETE) Dispatched when all of the sound resource’s data has been loaded. ioError (IOErrorEvent.IO_ERROR) Dispatched when a sound file cannot be located or when the loading process is interrupted before all sound data can be received. The following code illustrates how to play a sound after it has finished loading: import flash.events.Event; import flash.media.Sound; import flash.net.URLRequest; var s:Sound = new Sound(); s.addEventListener(Event.COMPLETE, onSoundLoaded); var req:URLRequest = new URLRequest("bigSound.mp3"); s.load(req); function onSoundLoaded(event:Event):void { var localSound:Sound = event.target as Sound; localSound.play(); } First, the code sample creates a new Sound object without giving it an initial value for the URLRequest parameter. Then, it listens for the Event.COMPLETE event from the Sound object, which causes the onSoundLoaded() method to execute when all the sound data is loaded. Next, it calls the Sound.load() method with a new URLRequest value for the sound file. The onSoundLoaded() method executes when the sound loading is complete. The target property of the Event object is a reference to the Sound object. Calling the play() method of the Sound object then starts the sound playback. Last updated 3/21/2011 423 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Monitoring the sound loading process Flash Player 9 and later, Adobe AIR 1.0 and later Sound files can be very large and take a long time to load. While Flash Player and AIR let your application play sounds even before they are fully loaded, you might want to give the user an indication of how much of the sound data has been loaded and how much of the sound has already been played. The Sound class dispatches two events that make it relatively easy to display the loading progress of a sound: ProgressEvent.PROGRESS and Event.COMPLETE. The following example shows how to use these events to display progress information about the sound being loaded: import import import import flash.events.Event; flash.events.ProgressEvent; flash.media.Sound; flash.net.URLRequest; var s:Sound = new Sound(); s.addEventListener(ProgressEvent.PROGRESS, onLoadProgress); s.addEventListener(Event.COMPLETE, onLoadComplete); s.addEventListener(IOErrorEvent.IO_ERROR, onIOError); var req:URLRequest = new URLRequest("bigSound.mp3"); s.load(req); function onLoadProgress(event:ProgressEvent):void { var loadedPct:uint = Math.round(100 * (event.bytesLoaded / event.bytesTotal)); trace("The sound is " + loadedPct + "% loaded."); } function onLoadComplete(event:Event):void { var localSound:Sound = event.target as Sound; localSound.play(); } function onIOError(event:IOErrorEvent) { trace("The sound could not be loaded: " + event.text); } This code first creates a Sound object and then adds listeners to that object for the ProgressEvent.PROGRESS and Event.COMPLETE events. After the Sound.load() method has been called and the first data is received from the sound file, a ProgressEvent.PROGRESS event occurs and triggers the onSoundLoadProgress() method. The percentage of the sound data that has been loaded is equal to the value of the bytesLoaded property of the ProgressEvent object divided by the value of the bytesTotal property. The same bytesLoaded and bytesTotal properties are available on the Sound object as well. The example above simply shows messages about the sound loading progress, but you can easily use the bytesLoaded and bytesTotal values to update progress bar components, such as the ones that come with the Adobe Flex framework or the Adobe Flash authoring tool. This example also shows how an application can recognize and respond to an error when loading sound files. For example, if a sound file with the given filename cannot be located, an Event.IO_ERROR event is dispatched by the Sound object. In the previous code, the onIOError() method executes and displays a brief error message when an error occurs. Last updated 3/21/2011 424 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Working with embedded sounds Flash Player 9 and later, Adobe AIR 1.0 and later Using embedded sounds, instead of loading sound from an external file, is most useful for small sounds that are used as indicators within your application’s user interface, such as sounds that play when buttons are clicked. When you embed a sound file in your application, the size of the resulting SWF file increases by the size of the sound file. In other words, embedding large sound files in your application can increase the size of your SWF file to an undesirable size. The exact method of embedding a sound file into your application’s SWF file varies according to your development environment. Using an embedded sound file in Flash Flash Player 9 and later, Adobe AIR 1.0 and later The Flash authoring tool lets you import sounds in a number of sound formats and store them as symbols in the Library. You can then assign them to frames in the timeline or to the frames of a button state, use them with Behaviors, or use them directly in ActionScript code. This section describes how to use embedded sounds in ActionScript code with the Flash authoring tool. For information about the other ways to use embedded sounds in Flash, see “Importing Sounds” in Using Flash. To embed a sound file using the Flash authoring tool: 1 Select File > Import > Import to Library, and then select a sound file and import it. 2 Right-click the name of the imported file in the Library panel, and select Properties. Click the Export for ActionScript checkbox. 3 In the Class field, enter a name to use when referring to this embedded sound in ActionScript. By default, it will use the name of the sound file in this field. If the filename includes a period, as in the name “DrumSound.mp3”, you must change it to something like “DrumSound”; ActionScript does not allow a period character in a class name. The Base Class field should still show flash.media.Sound. 4 Click OK. You might see a dialog box saying that a definition for this class could not be found in the classpath. Click OK and continue. If you entered a class name that doesn’t match the name of any of the classes in your application’s classpath, a new class that inherits from the flash.media.Sound class is automatically generated for you. 5 To use the embedded sound, you reference the class name for that sound in ActionScript. For example, the following code starts by creating a new instance of the automatically generated DrumSound class: var drum:DrumSound = new DrumSound(); var channel:SoundChannel = drum.play(); DrumSound is a subclass of the flash.media.Sound class so it inherits the Sound class’s methods and properties, including the play() method as shown above. Using an embedded sound file in Flex Flash Player 9 and later, Adobe AIR 1.0 and later There are many ways to embed sound assets in a Flex application, including: • Using the [Embed] metadata tag in a script Last updated 3/21/2011 425 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound • Using the @Embed directive in MXML to assign an embedded asset as a property of a component like a Button or a SoundEffect. • Using the @Embed directive within a CSS file This section describes the first option: how to embed sounds in ActionScript code within a Flex application using the [Embed] metadata tag. To embed an asset in ActionScript code, use the [Embed] metadata tag. Place the sound file in the main source folder or another folder that is in your project’s build path. When the compiler encounters an Embed metadata tag, it creates the embedded asset class for you. You can access the class through a variable of data type Class that you declare immediately after the [Embed] metadata tag. The following code embeds a sound named smallSound.mp3 and uses a variable named soundClass to store a reference to the embedded asset class associated with that sound. The code then creates an instance of the embedded asset class, casts it as an instance of the Sound class, and calls the play() method on that instance: package { import flash.display.Sprite; import flash.media.Sound; import flash.media.SoundChannel; public class EmbeddedSoundExample extends Sprite { [Embed(source="smallSound.mp3")] public var soundClass:Class; public function EmbeddedSoundExample() { var smallSound:Sound = new soundClass() as Sound; smallSound.play(); } } } To use the embedded sound to set a property of a Flex component, it should be cast as an instance of the mx.core.SoundAsset class instead of as an instance of the Sound class. For a similar example that uses the SoundAsset class see “Embedded asset classes” in Learning ActionScript 3.0. Working with streaming sound files Flash Player 9 and later, Adobe AIR 1.0 and later When a sound file or video file is playing back while its data is still being loaded, it is said to be streaming. External sound files that are loaded from a remote server are often streamed so that the user doesn’t have to wait for all the sound data to load before listening to the sound. The SoundMixer.bufferTime property represents the number of milliseconds of sound data that Flash Player or AIR should gather before letting the sound play. In other words, if the bufferTime property is set to 5000, Flash Player or AIR loads at least 5000 milliseconds worth of data from the sound file before the sound begins to play. The default SoundMixer.bufferTime value is 1000. Last updated 3/21/2011 426 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Your application can override the global SoundMixer.bufferTime value for an individual sound by explicitly specifying a new bufferTime value when loading the sound. To override the default buffer time, first create a new instance of the SoundLoaderContext class, set its bufferTime property, and then pass it as a parameter to the Sound.load() method, as shown below: import flash.media.Sound; import flash.media.SoundLoaderContext; import flash.net.URLRequest; var s:Sound = new Sound(); var req:URLRequest = new URLRequest("bigSound.mp3"); var context:SoundLoaderContext = new SoundLoaderContext(8000, true); s.load(req, context); s.play(); As playback continues, Flash Player and AIR try to keep the sound buffer at the same size or greater. If the sound data loads faster than the playback speed, playback will continue without interruption. However, if the data loading rate slows down because of network limitations, the playhead could reach the end of the sound buffer. If this happens, playback is suspended, though it automatically resumes once more sound data has been loaded. To find out if playback is suspended because Flash Player or AIR is waiting for data to load, use the Sound.isBuffering property. Working with dynamically generated audio Flash Player 10 and later, Adobe AIR 1.5 and later Note: The ability to dynamically generate audio is available starting with Flash Player 10 and Adobe AIR 1.5. Instead of loading or streaming an existing sound, you can generate audio data dynamically. You can generate audio data when you assign an event listener for the sampleData event of a Sound object. (The sampleData event is defined in the SampleDataEvent class in the flash.events package.) In this environment, the Sound object doesn’t load sound data from a file. Instead, it acts as a socket for sound data that is being streamed to it through the use of the function you assign to this event. When you add a sampleData event listener to a Sound object, the object periodically requests data to add to the sound buffer. This buffer contains data for the Sound object to play. When you call the play() method of the Sound object, it dispatches the sampleData event when requesting new sound data. (This is true only when the Sound object has not loaded mp3 data from a file.) The SampleDataEvent object includes a data property. In your event listener, you write ByteArray objects to this data object. The byte arrays you write to this object add to buffered sound data that the Sound object plays. The byte array in the buffer is a stream of floating-point values from -1 to 1. Each floating-point value represents the amplitude of one channel (left or right) of a sound sample. Sound is sampled at 44,100 samples per second. Each sample contains a left and right channel, interleaved as floating-point data in the byte array. In your handler function, you use the ByteArray.writeFloat() method to write to the data property of the sampleData event. For example, the following code generates a sine wave: Last updated 3/21/2011 427 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound var mySound:Sound = new Sound(); mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator); mySound.play(); function sineWaveGenerator(event:SampleDataEvent):void { for (var i:int = 0; i < 8192; i++) { var n:Number = Math.sin((i + event.position) / Math.PI / 4); event.data.writeFloat(n); event.data.writeFloat(n); } } When you call Sound.play(), the application starts calling your event handler, requesting sound sample data. The application continues to send events as the sound plays back until you stop providing data, or until you call SoundChannel.stop(). The latency of the event varies from platform to platform, and could change in future versions of Flash Player and AIR. Do not depend on a specific latency; calculate it instead. To calculate the latency, use the following formula: (SampleDataEvent.position / 44.1) - SoundChannelObject.position Provide from 2048 through 8192 samples to the data property of the SampleDataEvent object (for each call to the event listener). For best performance, provide as many samples as possible (up to 8192). The fewer samples you provide, the more likely it is that clicks and pops will occur during playback. This behavior can differ on various platforms and can occur in various situations—for example, when resizing the browser. Code that works on one platform when you provide only 2048 sample might not work as well when run on a different platform. If you require the lowest latency possible, consider making the amount of data user-selectable. If you provide fewer than 2048 samples (per call to the sampleData event listener), the application stops after playing the remaining samples. The SoundChannel object then dispatches a SoundComplete event. Modifying sound from mp3 data Flash Player 10 and later, Adobe AIR 1.5 and later You use the Sound.extract() method to extract data from a Sound object. You can use (and modify) that data to write to the dynamic stream of another Sound object for playback. For example, the following code uses the bytes of a loaded MP3 file and passes them through a filter function, upOctave(): Last updated 3/21/2011 428 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound var mySound:Sound = new Sound(); var sourceSnd:Sound = new Sound(); var urlReq:URLRequest = new URLRequest("test.mp3"); sourceSnd.load(urlReq); sourceSnd.addEventListener(Event.COMPLETE, loaded); function loaded(event:Event):void { mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound); mySound.play(); } function processSound(event:SampleDataEvent):void { var bytes:ByteArray = new ByteArray(); sourceSnd.extract(bytes, 8192); event.data.writeBytes(upOctave(bytes)); } function upOctave(bytes:ByteArray):ByteArray { var returnBytes:ByteArray = new ByteArray(); bytes.position = 0; while(bytes.bytesAvailable > 0) { returnBytes.writeFloat(bytes.readFloat()); returnBytes.writeFloat(bytes.readFloat()); if (bytes.bytesAvailable > 0) { bytes.position += 8; } } return returnBytes; } Limitations on generated sounds Flash Player 10 and later, Adobe AIR 1.5 and later When you use a sampleData event listener with a Sound object, the only other Sound methods that are enabled are Sound.extract() and Sound.play(). Calling any other methods or properties results in an exception. All methods and properties of the SoundChannel object are still enabled. Playing sounds Flash Player 9 and later, Adobe AIR 1.0 and later Playing a loaded sound can be as simple as calling the Sound.play() method for a Sound object, as follows: var snd:Sound = new Sound(new URLRequest("smallSound.mp3")); snd.play(); When playing back sounds using ActionScript 3.0, you can perform the following operations: • Play a sound from a specific starting position • Pause a sound and resume playback from the same position later Last updated 3/21/2011 429 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound • Know exactly when a sound finishes playing • Track the playback progress of a sound • Change volume or panning while a sound plays To perform these operations during playback, use the SoundChannel, SoundMixer, and SoundTransform classes. The SoundChannel class controls the playback of a single sound. The SoundChannel.position property can be thought of as a playhead, indicating the current point in the sound data that’s being played. When an application calls the Sound.play() method, a new instance of the SoundChannel class is created to control the playback. Your application can play a sound from a specific starting position by passing that position, in terms of milliseconds, as the startTime parameter of the Sound.play() method. It can also specify a fixed number of times to repeat the sound in rapid succession by passing a numeric value in the loops parameter of the Sound.play() method. When the Sound.play() method is called with both a startTime parameter and a loops parameter, the sound is played back repeatedly from the same starting point each time, as shown in the following code: var snd:Sound = new Sound(new URLRequest("repeatingSound.mp3")); snd.play(1000, 3); In this example, the sound is played from a point one second after the start of the sound, three times in succession. Pausing and resuming a sound Flash Player 9 and later, Adobe AIR 1.0 and later If your application plays long sounds, like songs or podcasts, you probably want to let users pause and resume the playback of those sounds. A sound cannot literally be paused during playback in ActionScript; it can only be stopped. However, a sound can be played starting from any point. You can record the position of the sound at the time it was stopped, and then replay the sound starting at that position later. For example, let’s say your code loads and plays a sound file like this: var snd:Sound = new Sound(new URLRequest("bigSound.mp3")); var channel:SoundChannel = snd.play(); While the sound plays, the SoundChannel.position property indicates the point in the sound file that is currently being played. Your application can store the position value before stopping the sound from playing, as follows: var pausePosition:int = channel.position; channel.stop(); To resume playing the sound, pass the previously stored position value to restart the sound from the same point it stopped at before. channel = snd.play(pausePosition); Monitoring playback Flash Player 9 and later, Adobe AIR 1.0 and later Your application might want to know when a sound stops playing so it can start playing another sound, or clean up some resources used during the previous playback. The SoundChannel class dispatches an Event.SOUND_COMPLETE event when its sound finishes playing. Your application can listen for this event and take appropriate action, as shown below: Last updated 3/21/2011 430 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound import flash.events.Event; import flash.media.Sound; import flash.net.URLRequest; var snd:Sound = new Sound(); var req:URLRequest = new URLRequest("smallSound.mp3"); snd.load(req); var channel:SoundChannel = snd.play(); channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); public function onPlaybackComplete(event:Event) { trace("The sound has finished playing."); } The SoundChannel class does not dispatch progress events during playback. To report on playback progress, your application can set up its own timing mechanism and track the position of the sound playhead. To calculate what percentage of a sound has been played, you can divide the value of the SoundChannel.position property by the length of the sound data that’s being played: var playbackPercent:uint = 100 * (channel.position / snd.length); However, this code only reports accurate playback percentages if the sound data was fully loaded before playback began. The Sound.length property shows the size of the sound data that is currently loaded, not the eventual size of the entire sound file. To track the playback progress of a streaming sound that is still loading, your application should estimate the eventual size of the full sound file and use that value in its calculations. You can estimate the eventual length of the sound data using the bytesLoaded and bytesTotal properties of the Sound object, as follows: var estimatedLength:int = Math.ceil(snd.length / (snd.bytesLoaded / snd.bytesTotal)); var playbackPercent:uint = 100 * (channel.position / estimatedLength); The following code loads a larger sound file and uses the Event.ENTER_FRAME event as its timing mechanism for showing playback progress. It periodically reports on the playback percentage, which is calculated as the current position value divided by the total length of the sound data: Last updated 3/21/2011 431 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound import flash.events.Event; import flash.media.Sound; import flash.net.URLRequest; var snd:Sound = new Sound(); var req:URLRequest = new URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3"); snd.load(req); var channel:SoundChannel; channel = snd.play(); addEventListener(Event.ENTER_FRAME, onEnterFrame); channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); function onEnterFrame(event:Event):void { var estimatedLength:int = Math.ceil(snd.length / (snd.bytesLoaded / snd.bytesTotal)); var playbackPercent:uint = Math.round(100 * (channel.position / estimatedLength)); trace("Sound playback is " + playbackPercent + "% complete."); } function onPlaybackComplete(event:Event) { trace("The sound has finished playing."); removeEventListener(Event.ENTER_FRAME, onEnterFrame); } After the sound data starts loading, this code calls the snd.play() method and stores the resulting SoundChannel object in the channel variable. Then it adds an event listener to the main application for the Event.ENTER_FRAME event and another event listener to the SoundChannel object for the Event.SOUND_COMPLETE event that occurs when playback is complete. Each time the application reaches a new frame in its animation, the onEnterFrame() method is called. The onEnterFrame() method estimates the total length of the sound file based on the amount of data that has already been loaded, and then it calculates and displays the current playback percentage. When the entire sound has been played, the onPlaybackComplete() method executes, removing the event listener for the Event.ENTER_FRAME event so that it doesn’t try to display progress updates after playback is done. The Event.ENTER_FRAME event can be dispatched many times per second. In some cases, you won’t want to display playback progress that frequently. In those cases, your application can set up its own timing mechanism using the flash.util.Timer class; see “Working with dates and times” on page 1. Stopping streaming sounds Flash Player 9 and later, Adobe AIR 1.0 and later There is a quirk in the playback process for sounds that are streaming—that is, for sounds that are still loading while they are being played. When your application calls the SoundChannel.stop() method on a SoundChannel instance that is playing back a streaming sound, the sound playback stops for one frame, and then on the next frame, it restarts from the beginning of the sound. This occurs because the sound loading process is still underway. To stop both the loading and the playback of a streaming sound, call the Sound.close() method. Last updated 3/21/2011 432 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Security considerations when loading and playing sounds Flash Player 9 and later, Adobe AIR 1.0 and later Your application’s ability to access sound data can be limited according to the Flash Player or AIR security model. Each sound is subject to the restrictions of two different security sandboxes, the sandbox for the content itself (the “content sandbox”), and the sandbox for the application or object that loads and plays the sound (the “owner sandbox”). For AIR application content in the application security sandbox, all sounds, including those loaded from other domains, are accessible to content in the application security sandbox. However, content in other security security sandboxes observe the same rules as content running in Flash Player. For more information about the Flash Player security model in general, and the definition of sandboxes, see “Security” on page 1008. The content sandbox controls whether detailed sound data can be extracted from the sound using the id3 property or the SoundMixer.computeSpectrum() method. It doesn’t restrict the loading or playing of the sound file itself. The domain of origin of the sound file defines the security limitations of the content sandbox. Generally, if a sound file is located in the same domain or folder as the SWF file of the application or object that loads it, the application or object will have full access to that sound file. If the sound comes from a different domain than the application does, it can still be brought within the content sandbox by using a policy file. Your application can pass a SoundLoaderContext object with a checkPolicyFile property as a parameter to the Sound.load() method. Setting the checkPolicyFile property to true tells Flash Player or AIR to look for a policy file on the server from which the sound is loaded. If a policy file exists, and it grants access to the domain of the loading SWF file, the SWF file can load the sound file, access the id3 property of the Sound object, and call the SoundMixer.computeSpectrum() method for loaded sounds. The owner sandbox controls local playback of the sounds. The application or object that starts playing a sound defines the owner sandbox. The SoundMixer.stopAll() method silences the sounds in all SoundChannel objects that are currently playing, as long as they meet the following criteria: • The sounds were started by objects within the same owner sandbox. • The sounds are from a source with a policy file that grants access to the domain of the application or object that calls the SoundMixer.stopAll() method. However, in an AIR application, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations. To find out if the SoundMixer.stopAll() method will indeed stop all playing sounds, your application can call the SoundMixer.areSoundsInaccessible() method. If that method returns a value of true, some of the sounds being played are outside the control of the current owner sandbox and will not be stopped by the SoundMixer.stopAll() method. The SoundMixer.stopAll() method also stops the playhead from continuing for all sounds that were loaded from external files. However, sounds that are embedded in FLA files and attached to frames in the timeline using the Flash Authoring tool might start playing again if the animation moves to a new frame. Last updated 3/21/2011 433 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Controlling sound volume and panning Flash Player 9 and later, Adobe AIR 1.0 and later An individual SoundChannel object controls both the left and the right stereo channels for a sound. If an mp3 sound is a monaural sound, the left and right stereo channels of the SoundChannel object will contain identical waveforms. You can find out the amplitude of each stereo channel of the sound being played using the leftPeak and rightPeak properties of the SoundChannel object. These properties show the peak amplitude of the sound waveform itself. They do not represent the actual playback volume. The actual playback volume is a function of the amplitude of the sound wave and the volume values set in the SoundChannel object and the SoundMixer class. The pan property of a SoundChannel object can be used to specify a different volume level for each of the left and right channels during playback. The pan property can have a value ranging from -1 to 1, where -1 means the left channel plays at top volume while the right channel is silent, and 1 means the right channel plays at top volume while the left channel is silent. Numeric values in between -1 and 1 set proportional values for the left and right channel values, and a value of 0 means that both channels play at a balanced, mid-volume level. The following code example creates a SoundTransform object with a volume value of 0.6 and a pan value of -1 (top left channel volume and no right channel volume). It passes the SoundTransform object as a parameter to the play() method, which applies that SoundTransform object to the new SoundChannel object that is created to control the playback. var snd:Sound = new Sound(new URLRequest("bigSound.mp3")); var trans:SoundTransform = new SoundTransform(0.6, -1); var channel:SoundChannel = snd.play(0, 1, trans); You can alter the volume and panning while a sound is playing by setting the pan or volume properties of a SoundTransform object and then applying that object as the soundTransform property of a SoundChannel object. You can also set global volume and pan values for all sounds at once using the soundTransform property of the SoundMixer class, as the following example shows: SoundMixer.soundTransform = new SoundTransform(1, -1); You can also use a SoundTransform object to set volume and pan values for a Microphone object (see “Capturing sound input” on page 439) and for Sprite objects and SimpleButton objects. The following example alternates the panning of the sound from the left channel to the right channel and back while the sound plays. Last updated 3/21/2011 434 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound import import import import import flash.events.Event; flash.media.Sound; flash.media.SoundChannel; flash.media.SoundMixer; flash.net.URLRequest; var snd:Sound = new Sound(); var req:URLRequest = new URLRequest("bigSound.mp3"); snd.load(req); var panCounter:Number = 0; var trans:SoundTransform; trans = new SoundTransform(1, 0); var channel:SoundChannel = snd.play(0, 1, trans); channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame(event:Event):void { trans.pan = Math.sin(panCounter); channel.soundTransform = trans; // or SoundMixer.soundTransform = trans; panCounter += 0.05; } function onPlaybackComplete(event:Event):void { removeEventListener(Event.ENTER_FRAME, onEnterFrame); } This code starts by loading a sound file and then creating a new SoundTransform object with volume set to 1 (full volume) and pan set to 0 (evenly balanced between left and right). Then it calls the snd.play() method, passing the SoundTransform object as a parameter. While the sound plays, the onEnterFrame() method executes repeatedly. The onEnterFrame() method uses the Math.sin() function to generate a value between -1 and 1, a range that corresponds to the acceptable values of the SoundTransform.pan property. The SoundTransform object’s pan property is set to the new value, and then the channel’s soundTransform property is set to use the altered SoundTransform object. To run this example, replace the filename bigSound.mp3 with the name of a local mp3 file. Then run the example. You should hear the left channel volume getting louder while the right channel volume gets softer, and vice versa. In this example, the same effect could be achieved by setting the soundTransform property of the SoundMixer class. However, that would affect the panning of all sounds currently playing, not just the single sound being played by this SoundChannel object. Working with sound metadata Flash Player 9 and later, Adobe AIR 1.0 and later Sound files that use the mp3 format can contain additional data about the sound in the form of ID3 tags. Last updated 3/21/2011 435 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Not every mp3 file contains ID3 metadata. When a Sound object loads an mp3 sound file, it dispatches an Event.ID3 event if the sound file contains ID3 metadata. To prevent run-time errors, your application should wait to receive the Event.ID3 event before accessing the Sound.id3 property for a loaded sound. The following code shows how to recognize when the ID3 metadata for a sound file has been loaded: import flash.events.Event; import flash.media.ID3Info; import flash.media.Sound; var s:Sound = new Sound(); s.addEventListener(Event.ID3, onID3InfoReceived); s.load("mySound.mp3"); function onID3InfoReceived(event:Event) { var id3:ID3Info = event.target.id3; trace("Received ID3 Info:"); for (var propName:String in id3) { trace(propName + " = " + id3[propName]); } } This code starts by creating a Sound object and telling it to listen for the Event.ID3 event. When the sound file’s ID3 metadata is loaded, the onID3InfoReceived() method is called. The target of the Event object that is passed to the onID3InfoReceived() method is the original Sound object, so the method then gets the Sound object’s id3 property and then iterates through all of its named properties to trace their values. Accessing raw sound data Flash Player 9 and later, Adobe AIR 1.0 and later The SoundMixer.computeSpectrum() method lets an application read the raw sound data for the waveform that is currently being played. If more than one SoundChannel object is currently playing the SoundMixer.computeSpectrum() method shows the combined sound data of every SoundChannel object mixed together. The sound data is returned as a ByteArray object containing 512 bytes of data, each of which contains a floating point value between -1 and 1. These values represent the amplitude of the points in the sound waveform being played. The values are delivered in two groups of 256, the first group for the left stereo channel and the second group for the right stereo channel. The SoundMixer.computeSpectrum() method returns frequency spectrum data rather than waveform data if the FFTMode parameter is set to true. The frequency spectrum shows amplitude arranged by sound frequency, from lowest frequency to highest. A Fast Fourier Transform (FFT) is used to convert the waveform data into frequency spectrum data. The resulting frequency spectrum values range from 0 to roughly 1.414 (the square root of 2). The following diagram compares the data returned from the computeSpectrum() method when the FFTMode parameter is set to true and when it is set to false. The sound whose data was used for this diagram contains a loud bass sound in the left channel and a drum hit sound in the right channel. Last updated 3/21/2011 436 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Left Channel Right Channel A Left Channel Right Channel B Values returned by the SoundMixer.computeSpectrum() method A. fftMode=true B. fftMode=false The computeSpectrum() method can also return data that has been resampled at a lower bit rate. Generally, this results in smoother waveform data or frequency data at the expense of detail. The stretchFactor parameter controls the rate at which the computeSpectrum() method data is sampled. When the stretchFactor parameter is set to 0, the default, the sound data is sampled at a rate of 44.1 kHz. The rate is halved at each successive value of the stretchFactor parameter, so a value of 1 specifies a rate of 22.05 kHz, a value of 2 specifies a rate of 11.025 kHz, and so on. The computeSpectrum() method still returns 256 bytes per stereo channel when a higher stretchFactor value is used. The SoundMixer.computeSpectrum() method has some limitations: • Because sound data from a microphone or from RTMP streams do not pass through the global SoundMixer object, the SoundMixer.computeSpectrum() method will not return data from those sources. • If one or more of the sounds being played come from sources outside the current content sandbox, security restrictions will cause the SoundMixer.computeSpectrum() method to throw an error. For more detail about the security limitations of the SoundMixer.computeSpectrum() method please see “Security considerations when loading and playing sounds” on page 432and “Accessing loaded media as data” on page 1030. However, in an AIR application, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations. Building a simple sound visualizer Flash Player 9 and later, Adobe AIR 1.0 and later The following example uses the SoundMixer.computeSpectrum() method to show a chart of the sound waveform that animates with each frame: Last updated 3/21/2011 437 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound import import import import import import flash.display.Graphics; flash.events.Event; flash.media.Sound; flash.media.SoundChannel; flash.media.SoundMixer; flash.net.URLRequest; const PLOT_HEIGHT:int = 200; const CHANNEL_LENGTH:int = 256; var snd:Sound = new Sound(); var req:URLRequest = new URLRequest("bigSound.mp3"); snd.load(req); var channel:SoundChannel; channel = snd.play(); addEventListener(Event.ENTER_FRAME, onEnterFrame); snd.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete); var bytes:ByteArray = new ByteArray(); function onEnterFrame(event:Event):void { SoundMixer.computeSpectrum(bytes, false, 0); var g:Graphics = this.graphics; g.clear(); g.lineStyle(0, 0x6600CC); g.beginFill(0x6600CC); g.moveTo(0, PLOT_HEIGHT); var n:Number = 0; // left channel for (var i:int = 0; i < CHANNEL_LENGTH; i++) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); Last updated 3/21/2011 438 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound g.endFill(); // right channel g.lineStyle(0, 0xCC0066); g.beginFill(0xCC0066, 0.5); g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT); for (i = CHANNEL_LENGTH; i > 0; i--) { n = (bytes.readFloat() * PLOT_HEIGHT); g.lineTo(i * 2, PLOT_HEIGHT - n); } g.lineTo(0, PLOT_HEIGHT); g.endFill(); } function onPlaybackComplete(event:Event) { removeEventListener(Event.ENTER_FRAME, onEnterFrame); } This example first loads and plays a sound file and then listens for the Event.ENTER_FRAME event which will trigger the onEnterFrame() method while the sound plays. The onEnterFrame() method starts by calling the SoundMixer.computeSpectrum() method, which stores the sound wave data in the bytes ByteArray object. The sound waveform is plotted using the vector drawing API. A for loop cycles through the first 256 data values, representing the left stereo channel, and draws a line from each point to the next using the Graphics.lineTo() method. A second for loop cycles through the next set of 256 values, plotting them in reverse order this time, from right to left. The resulting waveform plots can produce an interesting mirror-image effect, as shown in the following image. Last updated 3/21/2011 439 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Capturing sound input Flash Player 9 and later, Adobe AIR 1.0 and later The Microphone class lets your application connect to a microphone or other sound input device on the user’s system and broadcast the input audio to that system’s speakers or send the audio data to a remote server, such as Flash Media Server. You can access the raw audio data from the microphone and record or process it; you can also send the audio directly to the system’s speakers or send compressed audio data to a remote server. You can use either Speex or Nellymoser codec for data sent to a remote server. (The Speex codec is supported starting with Flash Player 10 and Adobe AIR 1.5.) More Help topics Michael Chaize: AIR, Android, and the Microphone Christophe Coenraets: Voice Notes for Android Accessing a microphone Flash Player 9 and later, Adobe AIR 1.0 and later The Microphone class does not have a constructor method. Instead, you use the static Microphone.getMicrophone() method to obtain a new Microphone instance, as shown below: var mic:Microphone = Microphone.getMicrophone(); Calling the Microphone.getMicrophone() method without a parameter returns the first sound input device discovered on the user’s system. A system can have more than one sound input device attached to it. Your application can use the Microphone.names property to get an array of the names of all available sound input devices. Then it can call the Microphone.getMicrophone() method with an index parameter that matches the index value of a device’s name in the array. A system might not have a microphone or other sound input device attached to it. You can use the Microphone.names property or the Microphone.getMicrophone() method to check whether the user has a sound input device installed. If the user doesn’t have a sound input device installed, the names array has a length of zero, and the getMicrophone() method returns a value of null. When your application calls the Microphone.getMicrophone() method, Flash Player displays the Flash Player Settings dialog box, which prompts the user to either allow or deny Flash Player access to the camera and microphone on the system. After the user clicks on either the Allow button or the Deny button in this dialog, a StatusEvent is dispatched. The code property of that StatusEvent instance indicates whether microphone access was allowed or denied, as shown in this example: Last updated 3/21/2011 440 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound import flash.media.Microphone; var mic:Microphone = Microphone.getMicrophone(); mic.addEventListener(StatusEvent.STATUS, this.onMicStatus); function onMicStatus(event:StatusEvent):void { if (event.code == "Microphone.Unmuted") { trace("Microphone access was allowed."); } else if (event.code == "Microphone.Muted") { trace("Microphone access was denied."); } } The StatusEvent.code property will contain “Microphone.Unmuted” if access was allowed, or “Microphone.Muted” if access was denied. The Microphone.muted property is set to true or false when the user allows or denies microphone access, respectively. However, the muted property is not set on the Microphone instance until the StatusEvent has been dispatched, so your application should also wait for the StatusEvent.STATUS event to be dispatched before checking the Microphone.muted property. In order for Flash Player to display the settings dialog, the application window must be large enough to display it (at least 215 by 138 pixels). Otherwise, access is denied automatically. Content running in the AIR application sandbox does not need the permission of the user to access the microphone. Thus, status events for muting and unmuting the microphone are never dispatched. Content running in AIR outside the application sandbox does require permission from the user, so these status events can be dispatched. Routing microphone audio to local speakers Flash Player 9 and later, Adobe AIR 1.0 and later Audio input from a microphone can be routed to the local system speakers by calling the Microphone.setLoopback() method with a parameter value of true. When sound from a local microphone is routed to local speakers, there is a risk of creating an audio feedback loop, which can cause loud squealing sounds and can potentially damage sound hardware. Calling the Microphone.setUseEchoSuppression() method with a parameter value of true reduces, but does not completely eliminate, the risk that audio feedback will occur. Adobe recommends you always call Microphone.setUseEchoSuppression(true) before calling Microphone.setLoopback(true), unless you are certain that the user is playing back the sound using headphones or something other than speakers. The following code shows how to route the audio from a local microphone to the local system speakers: var mic:Microphone = Microphone.getMicrophone(); mic.setUseEchoSuppression(true); mic.setLoopBack(true); Last updated 3/21/2011 441 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Altering microphone audio Flash Player 9 and later, Adobe AIR 1.0 and later Your application can alter the audio data that comes from a microphone in two ways. First, it can change the gain of the input sound, which effectively multiplies the input values by a specified amount to create a louder or quieter sound. The Microphone.gain property accepts numeric values between 0 and 100 inclusive. A value of 50 acts like a multiplier of one and specifies normal volume. A value of zero acts like a multiplier of zero and effectively silences the input audio. Values above 50 specify higher than normal volume. Your application can also change the sample rate of the input audio. Higher sample rates increase sound quality, but they also create denser data streams that use more resources for transmission and storage. The Microphone.rate property represents the audio sample rate measured in kilohertz (kHz). The default sample rate is 8 kHz. You can set the Microphone.rate property to a value higher than 8 kHz if your microphone supports the higher rate. For example, setting the Microphone.rate property to a value of 11 sets the sample rate to 11 kHz; setting it to 22 sets the sample rate to 22 kHz, and so on. The sample rates available depend on the selected codec. When you use the Nellymoser codec, you can specify 5, 8, 11, 16, 22 and 44 kHz as the sample rate. When you use Speex codec (available starting in Flash Player 10 and Adobe AIR 1.5), you can only use 16 kHz. Detecting microphone activity Flash Player 9 and later, Adobe AIR 1.0 and later To conserve bandwidth and processing resources, Flash Player tries to detect when no sound is being transmitted by a microphone. When the microphone’s activity level stays below the silence level threshold for a period of time, Flash Player stops transmitting the audio input and dispatches a simple ActivityEvent instead. If you use the Speex codec (available in Flash Player 10 or later and Adobe AIR 1.5 or later), set the silence level to 0, to ensure that the application continuously transmits audio data. Speex voice activity detection automatically reduces bandwidth. Note: A Microphone object only dispatches Activity events when your application is monitoring the microphone. Thus, if you do not call setLoopBack( true ), add a listener for sample data events, or attach the microphone to a NetStream object, then no activity events are dispatched. Three properties of the Microphone class monitor and control the detection of activity: • The read-only activityLevel property indicates the amount of sound the microphone is detecting, on a scale from 0 to 100. • The silenceLevel property specifies the amount of sound needed to activate the microphone and dispatch an ActivityEvent.ACTIVITY event. The silenceLevel property also uses a scale from 0 to 100, and the default value is 10. • The silenceTimeout property describes the number of milliseconds that the activity level must stay below the silence level, until an ActivityEvent.ACTIVITY event is dispatched to indicate that the microphone is now silent. The default silenceTimeout value is 2000. Both the Microphone.silenceLevel property and the Microphone.silenceTimeout property are read only, but their values can be changed by using the Microphone.setSilenceLevel() method. In some cases, the process of activating the microphone when new activity is detected can cause a short delay. Keeping the microphone active at all times can remove such activation delays. Your application can call the Microphone.setSilenceLevel() method with the silenceLevel parameter set to zero to tell Flash Player to keep the microphone active and keep gathering audio data, even when no sound is being detected. Conversely, setting the silenceLevel parameter to 100 prevents the microphone from being activated at all. Last updated 3/21/2011 442 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound The following example displays information about the microphone and reports on activity events and status events dispatched by a Microphone object: import flash.events.ActivityEvent; import flash.events.StatusEvent; import flash.media.Microphone; var deviceArray:Array = Microphone.names; trace("Available sound input devices:"); for (var i:int = 0; i < deviceArray.length; i++) { trace(" " + deviceArray[i]); } var mic:Microphone = Microphone.getMicrophone(); mic.gain = 60; mic.rate = 11; mic.setUseEchoSuppression(true); mic.setLoopBack(true); mic.setSilenceLevel(5, 1000); mic.addEventListener(ActivityEvent.ACTIVITY, this.onMicActivity); mic.addEventListener(StatusEvent.STATUS, this.onMicStatus); var micDetails:String = "Sound input device name: " + mic.name + '\n'; micDetails += "Gain: " + mic.gain + '\n'; micDetails += "Rate: " + mic.rate + " kHz" + '\n'; micDetails += "Muted: " + mic.muted + '\n'; micDetails += "Silence level: " + mic.silenceLevel + '\n'; micDetails += "Silence timeout: " + mic.silenceTimeout + '\n'; micDetails += "Echo suppression: " + mic.useEchoSuppression + '\n'; trace(micDetails); function onMicActivity(event:ActivityEvent):void { trace("activating=" + event.activating + ", activityLevel=" + mic.activityLevel); } function onMicStatus(event:StatusEvent):void { trace("status: level=" + event.level + ", code=" + event.code); } When you run the above example, speak or makes noises into your system microphone and watch the resulting trace statements appear in a console or debug window. Sending audio to and from a media server Flash Player 9 and later, Adobe AIR 1.0 and later Additional audio capabilities are available when using ActionScript with a streaming media server such as Flash Media Server. Last updated 3/21/2011 443 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound In particular, your application can attach a Microphone object to a NetStream object and transmit data directly from the user’s microphone to the server. Audio data can also be streamed from the server to an application and played back as part of a MovieClip or by using a Video object. The Speex codec is available starting with Flash Player 10 and Adobe AIR 1.5. To set the codec used for compressed audio sent to the media server, set the codec property of the Microphone object. This property can have two values, which are enumerated in the SoundCodec class. Setting the codec property to SoundCodec.SPEEX selects the Speex codec for compressing audio. Setting the property to SoundCodec.NELLYMOSER (the default) selects the Nellymoser codec for compressing audio. For more information, see the Flash Media Server documentation online at www.adobe.com/go/learn_fms_docs_en. Capturing microphone sound data Flash Player 10.1 and later, Adobe AIR 2 and later In Flash Player 10.1 and AIR 2, or later, you can capture data from a microphone data as a byte array of floating point values. Each value represents a sample of monophonic audio data. To get microphone data, set an event listener for the sampleData event of the Microphone object. The Microphone object dispatches sampleData events periodically as the microphone buffer is filled with sound samples. The SampleDataEvent object has a data property that is a byte array of sound samples. The samples are each represented as floating point values, each representing a monophonic sound sample. The following code captures microphone sound data into a ByteArray object named soundBytes: var mic:Microphone = Microphone.getMicrophone(); mic.setSilenceLevel(0, DELAY_LENGTH); mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); function micSampleDataHandler(event:SampleDataEvent):void { while(event.data.bytesAvailable) { var sample:Number = event.data.readFloat(); soundBytes.writeFloat(sample); } } You can reuse the sample bytes as playback audio for a Sound object. If you do, you should set the rate property of the Microphone object to 44, which is the sample rate used by Sound objects. (You can also convert microphone samples captured at a lower rate to 44 kHz rate required by the Sound object.) Also, keep in mind that the Microphone object captures monophonic samples, whereas the Sound object uses stereo sound; so you should write each of the bytes captured by the Microphone object to the Sound object twice. The following example captures 4 seconds of microphone data and plays it back using a Sound object: Last updated 3/21/2011 444 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound const DELAY_LENGTH:int = 4000; var mic:Microphone = Microphone.getMicrophone(); mic.setSilenceLevel(0, DELAY_LENGTH); mic.gain = 100; mic.rate = 44; mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); var timer:Timer = new Timer(DELAY_LENGTH); timer.addEventListener(TimerEvent.TIMER, timerHandler); timer.start(); function micSampleDataHandler(event:SampleDataEvent):void { while(event.data.bytesAvailable) { var sample:Number = event.data.readFloat(); soundBytes.writeFloat(sample); } } var sound:Sound = new Sound(); var channel:SoundChannel; function timerHandler(event:TimerEvent):void { mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler); timer.stop(); soundBytes.position = 0; sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler); channel.addEventListener( Event.SOUND_COMPLETE, playbackComplete ); channel = sound.play(); } function playbackSampleHandler(event:SampleDataEvent):void { for (var i:int = 0; i < 8192 && soundBytes.bytesAvailable > 0; i++) { trace(sample); var sample:Number = soundBytes.readFloat(); event.data.writeFloat(sample); event.data.writeFloat(sample); } } function playbackComplete( event:Event ):void { trace( "Playback finished."); } For more information on playing back sounds from sound sample data, see “Working with dynamically generated audio” on page 426. Last updated 3/21/2011 445 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound Sound example: Podcast Player Flash Player 9 and later, Adobe AIR 1.0 and later A podcast is a sound file that is distributed over the Internet, on demand or by subscription. Podcasts are usually published as part of a series, which is also called a podcast channel. Because podcast episodes can last anywhere from one minute to many hours, they are usually streamed while playing. Podcast episodes, which are also called items, are usually delivered in the mp3 file format. Video podcasts are also popular, but this sample application plays only audio podcasts that use mp3 files. This example is not a full-featured podcast aggregator application. For example, it does not manage subscriptions to specific podcasts or remember which podcasts the user has listened to the next time the application is run. It could serve as a starting point for a more full-featured podcast aggregator. The Podcast Player example illustrates the following ActionScript programming techniques: • Reading an external RSS feed and parsing its XML content • Creating a SoundFacade class to simplify loading and playback of sound files • Displaying sound playback progress • Pausing and resuming sound playback To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The Podcast Player application files can be found in the folder Samples/PodcastPlayer. The application consists of the following files: File Description PodcastPlayer.mxml The user interface for the application for Flex (MXML) or Flash (FLA). or PodcastPlayer.fla comp/example/progra Document class containing the user interface logic for the podcast player (Flash only). mmingas3/podcastplay er/PodcastPlayer.as SoundPlayer.mxml An MXML component that displays playback buttons and progress bars and controls sound playback, for Flex only. main.css Styles for the application user interface (Flex only). images/ Icons for styling the buttons (Flex only). comp/example/progra Class for the SoundPlayer movie clip symbol containing the user interface logic for the sound player (Flash only). mmingas3/podcastplay er/SoundPlayer.as comp/example/progra Custom cell renderer for displaying a play button in a data grid cell (Flash only). mmingas3/podcastplay er/PlayButtonRenderer. as com/example/program A base class that provides common properties and methods for the RSSChannel class and the RSSItem class. mingas3/podcastplayer /RSSBase.as com/example/program An ActionScript class that holds data about an RSS channel. mingas3/podcastplayer /RSSChannel.as Last updated 3/21/2011 446 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound File Description com/example/program An ActionScript class that holds data about an RSS item. mingas3/podcastplayer /RSSItem.as com/example/program The main ActionScript class for the application. It encapsulates the methods and events of the Sound class and the mingas3/podcastplayer SoundChannel class and adds support for pausing and resuming playback. /SoundFacade.as com/example/program An ActionScript class that retrieves data from a remote URL. mingas3/podcastplayer /URLService.as playerconfig.xml An XML file containing a list of RSS feeds that represent podcast channels. comp/example/progra Class that is used for easy date formatting (Flash only). mmingas3/utils/DateUt il.as Reading RSS data for a podcast channel Flash Player 9 and later, Adobe AIR 1.0 and later The Podcast Player application starts by reading information about a number of podcast channels and their episodes: 1. First, the application reads an XML configuration file that contains a list of podcast channels and displays the list of channels to the user. 2. When the user selects one of the podcast channels, it reads the RSS feed for the channel and displays a list of the channel episodes. This example uses the URLLoader utility class to retrieve text-based data from a remote location or a local file. The Podcast Player first creates a URLLoader object to get a list of RSS feeds in XML format from the playerconfig.xml file. Next, when the user selects a specific feed from the list, a new URLLoader object is created to read the RSS data from that feed’s URL. Simplifying sound loading and playback using the SoundFacade class Flash Player 9 and later, Adobe AIR 1.0 and later The ActionScript 3.0 sound architecture is powerful but complex. Applications that only need basic sound loading and playback features can use a class that hides some of the complexity by providing a simpler set of method calls and events. In the world of software design patterns, such a class is called a facade. The SoundFacade class presents a single interface for performing the following tasks: • Loading sound files using a Sound object, a SoundLoaderContext object, and the SoundMixer class • Playing sound files using the Sound object and the SoundChannel object • Dispatching playback progress events • Pausing and resuming playback of the sound using the Sound object and the SoundChannel object The SoundFacade class tries to offer most of the functionality of the ActionScript sound classes with less complexity. The following code shows the class declaration, the class properties, and the SoundFacade() constructor method: Last updated 3/21/2011 447 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound public class SoundFacade extends EventDispatcher { public var s:Sound; public var sc:SoundChannel; public var url:String; public var bufferTime:int = 1000; public public public public public public var var var var var var isLoaded:Boolean = false; isReadyToPlay:Boolean = false; isPlaying:Boolean = false; isStreaming:Boolean = true; autoLoad:Boolean = true; autoPlay:Boolean = true; public var pausePosition:int = 0; public static const PLAY_PROGRESS:String = "playProgress"; public var progressInterval:int = 1000; public var playTimer:Timer; public function SoundFacade(soundUrl:String, autoLoad:Boolean = true, autoPlay:Boolean = true, streaming:Boolean = true, bufferTime:int = -1):void { this.url = soundUrl; // Sets Boolean values that determine the behavior of this object this.autoLoad = autoLoad; this.autoPlay = autoPlay; this.isStreaming = streaming; // Defaults to the global bufferTime value if (bufferTime < 0) { bufferTime = SoundMixer.bufferTime; } // Keeps buffer time reasonable, between 0 and 30 seconds this.bufferTime = Math.min(Math.max(0, bufferTime), 30000); if (autoLoad) { load(); } } The SoundFacade class extends the EventDispatcher class so that it can dispatch its own events. The class code first declares properties for a Sound object and a SoundChannel object. The class also stores the value of the URL of the sound file and a bufferTime property to use when streaming the sound. In addition, it accepts some Boolean parameter values that affect the loading and playback behavior: • The autoLoad parameter tells the object that sound loading should start as soon as this object is created. • The autoPlay parameter indicates that sound playing should start as soon as enough sound data has been loaded. If this is a streaming sound, playback will begin as soon as enough data, as specified by the bufferTime property, has loaded. Last updated 3/21/2011 448 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound • The streaming parameter indicates that this sound file can start playing before loading has completed. The bufferTime parameter defaults to a value of -1. If the constructor method detects a negative value in the bufferTime parameter, it sets the bufferTime property to the value of SoundMixer.bufferTime. This lets the application default to the global SoundMixer.bufferTime value as desired. If the autoLoad parameter is set to true, the constructor method immediately calls the following load() method to start loading the sound file: public function load():void { if (this.isPlaying) { this.stop(); this.s.close(); } this.isLoaded = false; this.s = new Sound(); this.s.addEventListener(ProgressEvent.PROGRESS, onLoadProgress); this.s.addEventListener(Event.OPEN, onLoadOpen); this.s.addEventListener(Event.COMPLETE, onLoadComplete); this.s.addEventListener(Event.ID3, onID3); this.s.addEventListener(IOErrorEvent.IO_ERROR, onIOError); this.s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onIOError); var req:URLRequest = new URLRequest(this.url); var context:SoundLoaderContext = new SoundLoaderContext(this.bufferTime, true); this.s.load(req, context); } The load() method creates a new Sound object and then adds listeners for all of the important sound events. Then it tells the Sound object to load the sound file, using a SoundLoaderContext object to pass in the bufferTime value. Because the url property can be changed, a SoundFacade instance can be used to play different sound files in succession: simply change the url property and call the load() method, and the new sound file will be loaded. The following three event listener methods show how the SoundFacade object tracks loading progress and decides when to start playing the sound: Last updated 3/21/2011 449 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound public function onLoadOpen(event:Event):void { if (this.isStreaming) { this.isReadyToPlay = true; if (autoPlay) { this.play(); } } this.dispatchEvent(event.clone()); } public function onLoadProgress(event:ProgressEvent):void { this.dispatchEvent(event.clone()); } public function onLoadComplete(event:Event):void { this.isReadyToPlay = true; this.isLoaded = true; this.dispatchEvent(evt.clone()); if (autoPlay && !isPlaying) { play(); } } The onLoadOpen() method executes when sound loading starts. If the sound can be played in streaming mode, the onLoadComplete() method sets the isReadyToPlay flag to true right away. The isReadyToPlay flag determines whether the application can start the sound playing, perhaps in response to a user action like clicking a Play button. The SoundChannel class manages the buffering of sound data, so there is no need to explicitly check whether enough data has been loaded before calling the play() method. The onLoadProgress() method executes periodically during the loading process. It simply dispatches a clone of its ProgressEvent object for use by code that uses this SoundFacade object. When the sound data has been fully loaded the onLoadComplete() method executes, calling the play() method for non-streaming sounds if needed. The play() method itself is shown below. Last updated 3/21/2011 450 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound public function play(pos:int = 0):void { if (!this.isPlaying) { if (this.isReadyToPlay) { this.sc = this.s.play(pos); this.sc.addEventListener(Event.SOUND_COMPLETE, onPlayComplete); this.isPlaying = true; this.playTimer = new Timer(this.progressInterval); this.playTimer.addEventListener(TimerEvent.TIMER, onPlayTimer); this.playTimer.start(); } } } The play() method calls the Sound.play() method if the sound is ready to play. The resulting SoundChannel object is stored in the sc property. The play() method then creates a Timer object that will be used to dispatch playback progress events at regular intervals. Displaying playback progress Flash Player 9 and later, Adobe AIR 1.0 and later Creating a Timer object to drive playback monitoring is complex operation that you should only have to code once. Encapsulating this Timer logic in a reusable class like the SoundFacade class lets applications listen to the same kinds of progress events when a sound is loading and when it is playing. The Timer object that is created by the SoundFacade.play() method dispatches a TimerEvent instance every second. The following onPlayTimer() method executes whenever a new TimerEvent arrives: public function onPlayTimer(event:TimerEvent):void { var estimatedLength:int = Math.ceil(this.s.length / (this.s.bytesLoaded / this.s.bytesTotal)); var progEvent:ProgressEvent = new ProgressEvent(PLAY_PROGRESS, false, false, this.sc.position, estimatedLength); this.dispatchEvent(progEvent); } The onPlayTimer() method implements the size estimation technique described in the section “Monitoring playback” on page 429. Then it creates a new ProgressEvent instance with an event type of SoundFacade.PLAY_PROGRESS, with the bytesLoaded property set to the current position of the SoundChannel object and the bytesTotal property set to the estimated length of the sound data. Pausing and resuming playback Flash Player 9 and later, Adobe AIR 1.0 and later The SoundFacade.play() method shown previously accepts a pos parameter corresponding to a starting position in the sound data. If the pos value is zero, the sound starts playing from the beginning. The SoundFacade.stop() method also accepts a pos parameter as shown here: Last updated 3/21/2011 451 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with sound public function stop(pos:int = 0):void { if (this.isPlaying) { this.pausePosition = pos; this.sc.stop(); this.playTimer.stop(); this.isPlaying = false; } } Whenever the SoundFacade.stop() method is called, it sets the pausePosition property so that the application knows where to position the playhead if the user wants to resume playback of the same sound. The SoundFacade.pause() and SoundFacade.resume() methods shown below invoke the SoundFacade.stop() and SoundFacade.play() methods respectively, passing a pos parameter value each time. public function pause():void { stop(this.sc.position); } public function resume():void { play(this.pausePosition); } The pause() method passes the current SoundChannel.position value to the play() method, which stores that value in the pausePosition property. The resume() method starts playing the same sound again using the pausePosition value as the starting point. Extending the Podcast Player example Flash Player 9 and later, Adobe AIR 1.0 and later This example presents a bare-bones Podcast Player that showcases the use of the reusable SoundFacade class. You could add other features to enhance the usefulness of this application, including the following: • Store the list of feeds and usage information about each episode in a SharedObject instance that can be used the next time the user runs the application. • Let the user add his or her own RSS feeds to the list of podcast channels. • Remember the position of the playhead when the user stops or leaves an episode, so it can be restarted from that point next time the user runs the application. • Download mp3 files of episodes for listening offline, when the user is not connected to the Internet. • Add subscription features that periodically check for new episodes in a podcast channel and update the episode list automatically. • Add podcast searching and browsing functionality using an API from a podcast hosting service like Odeo.com. Last updated 3/21/2011 452 Chapter 23: Working with video Flash Player 9 and later, Adobe AIR 1.0 and later Flash video is one of the standout technologies on the Internet. However, the traditional presentation of video—in a rectangular screen with a progress bar and control buttons underneath—is only one possible use of video. Through ActionScript, you have fine-tuned access to and control over video loading, presentation, and playback. Basics of video Flash Player 9 and later, Adobe AIR 1.0 and later One important capability of Adobe® Flash® Player and Adobe® AIR™ is the ability to display and manipulate video information with ActionScript in the same way that you can manipulate other visual content such as images, animation, text, and so on. When you create a Flash Video (FLV) file in Adobe Flash CS4 Professional, you have the option to select a skin that includes common playback controls. However, there is no reason you need to limit yourself to the options available. Using ActionScript, you have fine-tuned control over loading, displaying, and playback of video—meaning you could create your own video player skin, or use your video in any less traditional way that you want. Working with video in ActionScript involves working with a combination of several classes: • Video class: The classic video content box on the Stage is an instance of the Video class. The Video class is a display object, so it can be manipulated using the same techniques that can be applied to other display objects, such as positioning, applying transformations, applying filters and blending modes, and so forth. • StageVideo class: The Video class typically uses software decoding and rendering. When GPU hardware acceleration is available on a device, your application can take best advantage of hardware rendering by switching to the StageVideo class. The StageVideo API includes a set of events that tell your code when to switch between StageVideo and Video objects. Stage video imposes some minor restrictions on video playback. If your application accepts those limitations, implement the StageVideo API. See “Guidelines and limitations” on page 488. • NetStream class: When you’re loading a video file to be controlled by ActionScript, a NetStream instance represents the source of the video content—in this case, a stream of video data. Using a NetStream instance also involves using a NetConnection object, which is the connection to the video file—like the tunnel that the video data is fed through. • Camera class: When you’re working with video data from a camera connected to the user’s computer, a Camera instance represents the source of the video content—the user’s camera and the video data it makes available. When you’re loading external video, you can load the file from a standard web server for progressive download, or you can work with streaming video delivered by a specialized server such as Adobe’s Flash® Media Server. Important concepts and terms Cue point A marker that can be placed at a specific moment in time in a video file, for example to act like a bookmark for locating that point in time, or to provide additional data that is associated with that moment in time. Encoding The process of taking video data in one format and converting it to another video data format; for example, taking a high-resolution source video and converting it to a format that’s suitable for Internet delivery. Frame A single segment of video information; each frame is like a still image representing a snapshot of a moment in time. By playing frames in sequence at high speed, the illusion of motion is created. Last updated 3/21/2011 453 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with video Keyframe A video frame which contains the full information for the frame. Other frames that follow a keyframe only contain information about how they differ from the keyframe, rather than containing the full frame’s worth of information. Metadata Information about a video file that is embedded within the video file and retrieved when the video has loaded. Progressive download When a video file is delivered from a standard web server, the video data is loaded using progressive download, meaning the video information loads in sequence. This has the benefit that the video can begin playing before the entire file is downloaded; however, it prevents you from jumping ahead to a part of the video that hasn’t loaded. Streaming As an alternative to progressive download, a special video server can be used to deliver video over the Internet using a technique known as streaming (sometimes called “true streaming”). With streaming, the viewer’s computer never downloads the entire video at one time. To speed up download times, at any moment the computer only needs a portion of the total video information. Because a special server controls the delivery of the video content, any part of the video can be accessed at any time, rather than needing to wait for it to download before accessing it. Understanding video formats Flash Player 9 and later, Adobe AIR 1.0 and later In addition to the Adobe FLV video format, Flash Player and Adobe AIR support video and audio encoded in H.264 and HE-AAC from within MPEG-4 standard file formats. These formats stream high quality video at lower bit rates. Developers can leverage industry standard tools, including Adobe Premiere Pro and Adobe After Effects, to create and deliver compelling video content. Type Format Container Video H.264 MPEG-4: MP4, M4V, F4V, 3GPP Video Sorenson Spark FLV file Video ON2 VP6 FLV file Audio AAC+ / HE-AAC / AAC v1 / AAC v2 MPEG-4:MP4, M4V, F4V, 3GPP Audio Mp3 Mp3 Audio Nellymoser FLV file Audio Speex FLV file More Help topics Flash Media Server: Supported codecs Adobe HTTP Dynamic Streaming Encoding video for mobile devices AIR on Android can decode a wide range of H.264 videos. However, only a small subset of H.264 videos is suited to have a smooth playback on mobile phones. It is because many mobile phones are constrained for processing power. Adobe Flash Player for mobile can decode H.264 videos using in-built hardware acceleration. This decoding assures better quality at lower power consumption. Last updated 3/21/2011 454 ACTIONSCRIPT 3.0 DEVELOPER’S GUIDE Working with video H.264 standard supports several encoding techniques. Only high-end devices smoothly play videos with complex profiles and levels. However, a majority of devices can play video encoded in baseline profile. On mobile devices, hardware acceleration is available for a subset of these techniques. The profile and the level parameters define this subset of encoding techniques and settings used by the encoder. For developers, it translates into encoding the video in selected resolution which plays well on most devices. Though resolutions that benefit from hardware acceleration vary from device to device, but most devices support the following standard resolutions. Aspect ratio Recommended resolutions 4:3 640 × 480 512 × 384 480 × 360 16:9 640 × 360 512 x 288 480 × 272 Note: Flash Player supports every level and profile of the H.264 standard. Adhering to these recommendations ensures hardware acceleration and better user experience on most devices. These recommendations are not mandatory. For a detailed discussion and encoding settings in Adobe Media Encoder CS5, see Recommendations for encoding H.264 video for Flash Player 10.1 on mobile devices. Note: On iOS, only video encoded with the Sorenson Spark and On2 VP6 codecs can be played back using the Video class. You can play back H.264 encoded video in the device video player by launching the URL to the video using the flash.net.navigateToURL() function. You can also play back H.264 video using the