ActionScript 3.0 Bible - Roger Braunstein - E-Book

ActionScript 3.0 Bible E-Book

Roger Braunstein

0,0
34,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

The updated edition on all the latest features and capabilities of ActionScript 3.0 and Flash Player 10. ActionScript is a popular programming language used primarily for the development of Web sites and software. This update to the successful previous version introduces you to all the exciting new capabilities of ActionScript 3.0. You'll see how ActionScript 3.0 goes beyond its primary use of scripting Flash animations and is now an object-oriented evolution that runs ten times faster than previous versions and can be used in Adobe's new platforms, including Flex and AIR. Hands-on instruction and step-by-step tutorials enhance your learning process as you discover everything you need to know in order to harness the power of ActionScript 3.0. You'll learn the nitty gritty of building Rich Internet Applications (RIA) and the ins and outs of putting the new features of ActionScript 3.0 to work for you. * ActionScript 3.0 goes beyond its original role as a scripting language and has added development features, incredible speed, and the ability to work with Flex and AIR * Shows you how to apply advanced graphic effects using Pixel Blender * Demonstrates the all-new text and typography capabilities * Addresses your new enhanced control over dynamic sound * Explains templated types and new vector data structures ActionScript 3.0 Bible offers you a soup-to-nuts guide on all things ActionScript 3.0 so you can get started working with it immediately.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 1566

Veröffentlichungsjahr: 2011

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Title Page

Copyright

About the Author

Credits

Acknowledgments

Diagram of Classes

Introduction

What's New in This Edition

Make Sure You Buy the Right Book

How This Book Is Organized

Conventions and Features

Running Example Code

On the Web Site

Where to Go from Here

Part I: ActionScript 3.0 Language Basics

Chapter 1: Introducing ActionScript 3.0

What Is ActionScript 3.0?

Exploring the Flash Platform

From ActionScript 2.0 to ActionScript 3.0

Summary

Chapter 2: ActionScript 3.0 Language Basics

The Bare Essentials

Using Variables

Commenting Your Code

Introducing Scope

Introducing the Data Types

Connecting You to an Operator

Making Logical Choices with Conditionals

Repeating Actions Using Loops

Summary

Chapter 3: Functions and Methods

Calling Functions

Creating Custom Functions

Returning Results

Anonymous Functions

Functions as Objects

Recursive Functions

Summary

Chapter 4: Object Oriented Programming

Understanding Classes

Object Oriented Terminology

Encapsulation: Classes Are Like, So Selfish

Packages: Classes, Functions, and Packing Peanuts

Using Inheritance

Access Control Attributes

Methods and Constructors

Properties

Using Static Methods and Properties

Overriding Behavior

Designing Interfaces

Manipulating Types

Creating Dynamic Classes

Summary

Chapter 5: Validating Your Program

Introducing Errors

Fixing Errors

Summary

Part II: Core ActionScript 3.0 Data Types

Chapter 6: Text, Strings, and Characters

Working with String Literals

Converting to and from Strings

Combining Strings

Converting the Case of a String

Using the Individual Characters in a String

Searching within a String

String Dissection

String Encoding and International Text

Summary

Chapter 7: Numbers, Math, and Dates

Understanding Numeric Types

Using Numbers in ActionScript

Manipulating Numbers

Performing Arithmetic

Performing Trigonometric Calculations

Generating Randomness

Manipulating Dates and Times

Summary

Chapter 8: Arrays

Array Basics

Converting Arrays to Strings

Adding and Removing Items from an Array

Slicing, Splicing, and Dicing

Iterating through the Items in an Array

Searching for Elements

Reordering Your Array

Applying Actions to All Elements of an Array

Alternatives to Arrays

Amazing Properties of Arrays

Summary

Chapter 9: Vectors

Vector Basics

Generics and Parameterized Types

Generic Methods of Vector

Creating and Converting Vectors

Summary

Chapter 10: Objects and Dictionaries

Working with Objects

Using Objects and Dictionaries as Associative Arrays

Using Objects for Named Arguments

Using Objects as Nested Data

Summary

Chapter 11: XML and E4X

Getting Started with XML in ActionScript

Querying XML

Modifying XML

Converting to and from Strings

Loading XML Data from External Sources

Gathering Meta-Information about XML Nodes

Using Namespaces

Setting XML Options

Summary

Chapter 12: Regular Expressions

Introducing Regular Expressions

Writing a Regular Expression

Applying Regular Expressions

Constructing Expressions

Regular Expression Flags

Constructing Advanced Expressions

Using the RegExp Class

Summary

Chapter 13: Binary Data and ByteArrays

Binary Concepts

Bit Math and Operators

Binary Types in ActionScript

Using ByteArray

Common Uses of ByteArrays

Summary

Part III: The Display List

Chapter 14: Visual Programming with the Display List

Introducing Display Lists and Display Objects

Display Object Classes

Geometry Classes

Putting the Display List to Use

Rendering and Performance

Summary

Chapter 15: Working in Three Dimensions

Introducing 3D in ActionScript 3.0

DisplayObject Revisited

Geometry Revisited

Mouse and Point Translation in 3D

Modifying the Projection

Software 3D Libraries

Summary

Chapter 16: Working with DisplayObjects in Flash Professional

The Stage, Symbols, and the Library

Exporting and Using Assets

Summary

Chapter 17: Text, Styles, and Fonts

Introducing TextFields

Text with HTML and CSS

Background and Border Treatments

Styling Text with TextFormats

Input TextFields

Interaction with TextField Events

Interactive Typography

Fonts

Summary

Chapter 18: Advanced Text Layout

Understanding Advanced Text Controls

The Flash Text Engine

The Text Layout Framework

Fonts Revisited

Summary

Chapter 19: Printing

Why Print from Flash?

Controlling Printer Output from Flash

Adding Print Functionality to Applications

Summary

Part IV: Event-Driven Programming

Chapter 20: Events and the Event Flow

Introducing Events

The EventDispatcher Class

Working with Event Objects

Adding and Removing Event Listeners

The Event Flow

Preventing Default Behaviors

Summary

Chapter 21: Interactivity with the Mouse and Keyboard

Mouse and Keyboard Event Handling

Mouse Interactions

Keyboard Interactions

Focus

Context Menus

Summary

Chapter 22: Timers and Time-Driven Programming

Timer Basics

Handling Timer Events

Delaying the Execution of a Function

Creating a World Clock

Enterframe Events

Other Time-Related Functions

Aside: Threads

Summary

Chapter 23: Multitouch and Accelerometer Input

Planning for Your Audience

Using Multitouch

Using the Accelerometer

Other Sensors

Summary

Part V: Error Handling

Chapter 24: Errors and Exceptions

Comparing Ways to Fail

Understanding Exceptions

Errors Generated by Flash Player

Custom Exceptions

Handling Asynchronous Errors

Capturing Unhandled Events

Summary

Chapter 25: Using the AVM2 Debugger

Introducing Debugging

Launching the Debugger

Taking Control of Execution

Pulling Back the Curtain

Navigating through Code

Debugging a Simple Example

Using the Debugger Effectively

Summary

Chapter 26: Making Your Application Fault-Tolerant

Developing a Strategy

Determining What Errors to Handle

Categorizing Failures

Logging Errors

Messaging the User

Degrading Styles: An Example

Summary

Part VI: External Data

Chapter 27: Networking Basics and Flash Player Security

HTTP in Brief

Introducing URLRequest

Navigating to a Web Page

Loader

URLLoader

Sending Variables with URLRequest

Understanding Flash Player Security

Summary

Chapter 28: Communicating with Remote Services

Web Services Using HTTP

Socket Services

XML Socket Services

Flash Remoting

Summary

Chapter 29: Storing and Sending Data with SharedObject

Comparing Approaches to Persistent Storage

Identifying Useful Situations for Shared Objects

Shared Objects and Remoting

Using SharedObjects

Sharing Information between SWFs

Working with Size Constraints

Storing Custom Classes

Summary

Chapter 30: File Access

Abilities of the File API

Introducing FileReference

Uploading a File

Adding Upload Handling to a Server

Downloading a File to Disk

Loading a File into Memory

Saving Data to Disk

Summary

Part VII: Sound and Video

Chapter 31: Playing and Generating Sound

An Overview of the Sound System

Prepping and Playing Sound Objects

Controlling Sound Playback

Applying Sound Transformations

Working with a Sound's Metadata

Sampling Audio

Extracting Audio

Synthesizing Audio

Detecting Audio Capabilities

Summary

Chapter 32: Playing Video

Video and the Flash Platform

Implementing a Video Player

Summary

Chapter 33: Capturing Sound and Video

Video Input Using a Camera

Sound Input Using a Microphone

Flash Media Servers

Summary

Part VIII: Graphics Programming and Animation

Chapter 34: Geometric and Color Transformations

DisplayObject and the Transform Object

2D Affine Transformations

Color Transforms

3D Transformations

Summary

Chapter 35: Programming Vector Graphics

Overview

Drawing Basics

Setting Drawing Styles

Drawing Primitives

Example: A Drawing Application

Batched Drawing

3D Drawing

Summary

Chapter 36: Programming Bitmap Graphics

Bitmaps and Their Applications

Creating and Displaying Bitmaps

Capturing and Copying Bitmaps

Pixel-Level Access

Working with Colors

Bitmap Effects

Summary

Chapter 37: Applying Filters

Introducing Filters

Blurs

Drop Shadows

Bevels

Gradient Bevels

Glows

Gradient Glows

Color Effects

Convolution Filters

Displacement Maps

Shaders

Summary

Chapter 38: Writing Shaders with Pixel Bender

Introducing Pixel Bender

Pixel Bender Kernel Language

Interfacing with Pixel Bender Kernels

Bending Other Data

Summary

Chapter 39: Scripting Animation

Understanding Flash Player and Animation

Animating with Code

Animating Using Flash Professional

Animating Using Flex

Choosing a Third-Party Animation Toolkit

Summary

Chapter 40: Advanced 3D

Game Plan

Projecting Batches of Points

Triangle Strips

Backface Culling

Texture Mapping

Z-Sorting, Shading, and Further Topics

Summary

Part IX: Flash in Context

Chapter 41: Globalization, Accessibility, and Color Correction

Globalization and Localization

Accessibility

Color Correction

Summary

Chapter 42: Deploying Flash on the Web

Embedding Flash in a Page

Embedding Flash Using SWFObject

Enabling Flash Player Options

Passing Variables to a SWF

Automatically Upgrading Flash Player

Summary

Chapter 43: Interfacing with JavaScript

Using ExternalInterface

Making a Hybrid Application with ExternalInterface

Summary

Chapter 44: Local Connections between Flash Applications

Local Connections and Their Uses

Implementing a Local Connection

Local Connections and the Security Policy

Example: Following the Mouse

Summary

Index

Advertisement

ActionScript® 3.0 Bible

Published by

Wiley Publishing, Inc.

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com

Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-470-52523-4

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2009943640

Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. ActionScript is a registered trademark of Adobe Systems Incorporated. All other trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.

About the Author

Roger Braunstein is a multidisciplinary programmer and the Chief Roger Officer of partlyhuman inc., an independent development and design shop in Brooklyn, NY. Roger is an author of the first edition of ActionScript 3.0 Bible, and a short e-book, Introduction to Flex 2. Roger builds games, mobile apps, interactive art, tools, web apps, and websites using any technology he can wrap his head around. In just the past two years he's built stuff for LEGO, MoMA, Tim Burton, Antony and the Johnsons, AOL, HBO, Puma, General Mills, Sport Chalet, Pepsi, Coke, OMGPOP, the French Culinary Institute, and more. Additionally, he keeps busy with a series of independent projects. When not in front of a computer, Roger enjoys normal human activities such as biking, cooking, reading, traveling, taking photos, roller-skating, and dancing to music made on GameBoys. He is perpetually too preoccupied to put anything interesting on his site http://partlyhuman.com/, but you can use it to get in touch with him.

Credits

Acquisitions Editor

Scott Meyers

Project Editor

Brian MacDonald

Technical Editor

Caleb Johnston

Production Editor

Daniel Scribner

Copy Editor

Karen Gill

Editorial Director

Robyn B. Siesky

Editorial Manager

Mary Beth Wakefield

Marketing Manager

David Mayhew

Production Manager

Tim Tate

Vice President and Executive Group Publisher

Richard Swadley

Vice President and Executive Publisher

Barry Pruett

Associate Publisher

Jim Minatel

Project Coordinator, Cover

Lynsey Stanford

Proofreader

Nancy Bell

Indexer

Robert Swanson

Cover Image

Joyce Haughey

Cover Designer

Michael E. Trent

Acknowledgments

This book, like all gargantuan projects, was a team effort. Its pages were touched by many hands, at Wiley and elsewhere. Losing even a single pair of these hands would mean you wouldn't have this book in yours right now, so I want to thank each and every person on my team, whether I sent you 20 e-mails a day or never met you.

My most heartfelt thanks go to my project editor Brian MacDonald, and my acquisitions editor Scott Meyers, who were nothing short of bodacious to work with. When I think of how many times I pushed for some new, unconventional feature or change to the established Bible series, and then look at this book and see all of my ideas in print, I know it's Brian and Scott who have somehow done their magic, convinced the right people, and made it happen. Thank you.

Perhaps every author forms a good relationship with his project editor just because he's the person who's there from the first day of writing through the last, exhausted round of edits. But I think Brian MacDonald is exceptional. I've never worked with an editor half as good. He had the right solution for every problem. He was behind me and my ideas through thick and thin. His edits were always spot-on. He was always available, always funny, and he even got my nerdy jokes. Trust me, if you're writing a programming book, bribe whoever you need to hire Brian.

I can't talk about my team without mentioning the exhaustive copy editing done by Karen Gill. She kept tabs on all my writing, cut down my useless tiresome redundant logorrhea, and made this book less of a slog to get through. Trust me, you owe her one. I have the feeling her job is a little thankless, but I do thank her.

Caleb Johnston, besides being a good friend, was a terrific technical editor. Somehow, with a full course load for his master's degree, he managed to find the time to review this entire book by himself. (The previous edition, although smaller, had three technical editors!) More than just scrutinizing it, he made detailed and thoughtful criticisms, with as much ruthlessness as I demanded of him. He found some quirky errors, and because he found them, you won't have to.

Special thanks go out to my friend Corey Lucier at Adobe, who was kind enough to answer a few questions about the internal workings of Flash Player or forward them on to Flash Player engineers. Their additions helped me be even more accurate in this book, and Corey's help was invaluable.

Special thanks also go to Whitney Gardner, who made two sublime illustrations for this book when I was at my wit's end.

Thanks to the U.S. economy for making my day job so unprofitable it made sense to sit in my room and write for nine months. Thanks to coffee: you complete me. Thanks to my crazy SafeType keyboard, which saved me from crippling arm pain. Thanks to my friends for, I dunno, whatever, I like you.

Last but not least, thanks and lotsa lotsa love to my parents.

Part I

THE DISPLAY LIST

Chapter 1 Introducing ActionScript 3.0

Chapter 2 ActionScript 3.0 Language Basics

Chapter 3 Functions and Methods

Chapter 4 Object-Oriented Programming

Chapter 5 Validating Your Program

Chapter 1

Introducing ActionScript 3.0

In this chapter you'll look at what ActionScript is, where you can use it, and what you can do with it. You'll learn where ActionScript fits in the grand scheme of the Flash Platform, and you'll take a complete tour of the tools and technologies involved therein.

What Is ActionScript 3.0?

You may well already know the answer to this question, because you had enough interest in it to buy this book! ActionScript 3.0 is the language used to program interactive Flash content. Where this content goes and how you can build it is the subject of the following section.

ActionScript 3.0 is a well-organized, mature language that shares much of its syntax and methodologies with other object oriented, strongly typed languages, so an experienced programmer can readily pick it up. Don't fear, though, for this book introduces ActionScript from the bottom up and starts gently.

If you've used Flash before but never ActionScript, you might know that you can build content for Flash Player without ActionScript—but without ActionScript, Flash is just an animation tool (though, admittedly, a good one). ActionScript is necessary when you want to create Flash content that is highly dynamic, responsive, reusable, and customizable. Here's just a short list of the many things you can accomplish using ActionScript:

Loading imagesPlaying audio and videoDrawing programmaticallyLoading data such as XML filesResponding to user events such as mouse clicks

To get the most out of the Flash Platform, you're gonna need ActionScript 3.0. And as you learn what it can do, you'll be amazed at its power. But let's look at how ActionScript fits into the Flash universe.

Exploring the Flash Platform

The wild world of Flash is by no means small, and wrapping your head around ActionScript and all the technologies related to it is challenging. Even if you don't know all the latest technologies in the peripheries, it's essential that you know major parts of the Flash Platform and how they work together. Defining that term and what it encompasses will be the goal of this section.

You'll examine the different parts of the Flash Platform from the perspectives of you building content and the user getting and running content. I'll define and discuss the tools, languages, platforms, and runtimes involved in the process. Figure 1.1 shows a bird's-eye view of the Flash Platform.

Figure 1.1 A high-level overview of the Flash Platform

You, the programmer, write in a computer programming language, ActionScript 3.0. You use tools, like Flash Builder, and specifically a compiler, to convert your code into an executable, in this case a SWF. The SWF is delivered to the end user on her platform and executes inside a runtime, usually Flash Player, of which there are many versions. Let's break this down.

A Programmer's Perspective

For all the ways that the Flash Platform is unique, it shares the same basic steps as most programming environments. You write code in a language using some kind of editor, tool, or integrated development environment, and you use a compiler to convert that code into a file that can be run on the target environment.

Language

A computer programming language defines the grammar and lexicon that you'll be working in to create beautiful code. ActionScript 3.0, Python, Java, Lua, and C# are different languages. They all look different and have different rules for what you type where, what words are reserved, how to loop and how to write comments, and even where you can and can't put spaces. ActionScript 3.0 is not the same language as ActionScript 2.0, and neither is ActionScript 1.0.

ActionScript 3.0 has some features in common with modern JavaScript, because both are designed to adhere to specifications of a family of languages called ECMAScript. At the time of writing, this fact is little more than a curiosity, because most JavaScript in use is written to a baseline standard far behind the kind of JavaScript that starts to look like ActionScript 3.0. Furthermore, adherence to ECMAScript standards has provided little visible benefit, and progress marches on. In general, ActionScript 3.0 looks most like Java or C#. Coming to ActionScript 3.0 from either of these languages, or ActionScript 2.0, should be a fairly smooth ride. In this second edition, I've removed any emphasis put on transitioning from ActionScript 2.0 to ActionScript 3.0.

I'll describe some features of the language here. This might help some of you who have several languages under your belts already and can benefit from a description of ActionScript 3.0. If you don't understand any of these terms, please don't fret! The rest of this Part exists to investigate these qualities of the language in depth.

ActionScript 3.0 can use both dynamic and strong typing, but the compiler, the language, this book, and most of the world want you to use it with strong typing. If you want to live in a dynamic world, you can do so by turning off strict mode in your compiler or development environment. ActionScript 3.0 is an object oriented language that makes heavy use of namespaces. It has facilities for reflection. It embodies some elements of functional programming.

Maybe it goes without saying, but ActionScript 3.0 is the primary language used in the Flash universe. However, it's not the only one.

Depending on the platform and runtime you're targeting—that is to say, where, on what device, and on what software you want your content to run—you might use another language that this book is not about. I'll get into this more once you look at the platforms and runtimes that exist in the Flash universe. But rest assured that ActionScript 3.0 is the way to go for the Flash Platform right now. Most other options are for older technology.

There's another language that's a big part of the Flash Platform, and that's MXML. MXML is a declarative XML language used to program Flex. The interesting thing is that MXML compiles into ActionScript 3.0 during building. It's also used interchangeably with ActionScript 3.0. I won't discuss MXML or Flex in this book, but because Flex is a superset of Flash, this book provides an excellent, maybe prerequisite, background for any Flex developer.

Mixed into ActionScript 3.0 are several microlanguages, tailor-made for solving specific kinds of problems more efficiently than the grammar of ActionScript 3.0 would allow. Technically, these are part of the ActionScript 3.0 language specification, but when you use them you can tell instantly that another dialect is being spoken. These are E4X, a language for manipulating XML; regular expressions, a language for searching for and manipulating patterns of text; and XML itself, a way to store hierarchical information. Furthermore, there are closely associated languages, like Pixel Bender language, used to write Pixel Bender shaders. Although this language can't be written directly into ActionScript 3.0 code like E4X, it is necessary to use some of Flash Player's features. All of these languages are important parts of Flash Platform development and cannot be ignored by an ActionScript 3.0 developer.

There are two major points of confusion when speaking about the ActionScript 3.0 language. First is the fact that ActionScript 3.0 can change, and has changed, without changes to that little “3.0” number sitting in its title. It's a living language, and it's being developed even as you read this book. (Hello readers from the future! Have we learned how to speak to dolphins yet or what?) So how can ActionScript 3.0 change? Well, there are two ends that have to agree: the compiler has to make something out of your code, and the runtime has to be able to run what the compiler makes. But the fact is, all it takes is for the compiler to change to support a change in the language. Both can be modified at the same time, or the compiler only could change, to allow for alterations in the language. The compiler is the most important because it's the only part of the entire Flash Platform that sees actual ActionScript 3.0 source. This kind of change has already happened. The compiler shipped in Flash CS4, Flash Builder, and in newer builds of the Flex SDK supports syntax for parameterized types that you'll see in Chapter 9, “Vectors.” This syntax, TypeA.<TypeB>, looks like utter rubbish to an older ActionScript 3.0 compiler like that in Flash CS3, yet it is (now) part of ActionScript 3.0. We simply have to be careful. The second point of confusion when speaking about ActionScript 3.0 is the difference between ActionScript 3.0 and the Flash Player API.

API

Where a language determines keywords (like for, class, and is), syntax (like where to put curly braces), and grammar (like how subexpressions are evaluated and what can and can't go on the left side of an assignment), it's really the Application Programming Interface, or API, that gets most of the work done. It's easy to confuse these two, so let's untangle them once and for all.

The language by itself can't do much of anything. Without an API it's little more than a glorified calculator. You can do operations like creating variables, assigning values, summing up things, and concatenating strings; you can even create classes and functions. That said, there's a whole lot to learn about the language itself, and you could in theory make it through the end of Part II, “Core ActionScript 3.0 Data Types,” before using any of the Flash Player API, if you keep your eyes closed strategically. It's the runtime (Flash Player or AIR) that provides most of the exciting stuff the Flash Platform has to offer: graphics, sound, animation, networking, video, and so on. None of this is built into the language.

You can draw an analogy between programming languages and spoken or written languages. It's necessary to understand the grammar and pronunciation of a language, but that alone is not sufficient for communication. You need a rich vocabulary, and that's what the API provides.

If you don't mind skipping ahead to some topics discussed in Chapter 4, “Object Oriented Programming,” an easy way to determine what's part of the core language and what's part of the API is to look at how it's namespaced. Any classes and functions in the default package are part of the language, like Error, XML, int, and Number. Anything in the flash.* package and its subpackages is part of the Flash Player API, like flash.display.Sprite and flash.geom.Matrix3D. The Flash Player API is a library of classes and functions that get real stuff done.

Each runtime you target when building a program has its own API associated with it. In this book, I'll only cover the Flash Player runtime (specifically only versions 9 and up) and the Flash Player API. There is one more runtime I could potentially target: the AIR runtime. The good thing about the AIR API is that it's a superset of the Flash Player API. In other words, if you want to build AIR apps, everything you learn in this book will be applicable, and much will be necessary.

The runtime and the API it supports are fundamentally linked. Adobe engineers add new features into new versions of the Flash Player (the runtime) and simultaneously expose those features to programmers through additions to the Flash Player API. Changes to the API are far more frequent than changes to the language. That's why, in this book, I note when topics I discussed are particular to a certain version of the API.

Libraries

The Flash Player API is a staggeringly large collection of classes and methods that help you make interesting things happen on the screen, as the weight of this book attests to. But the brilliant thing about code is its extensibility. For every ability that the Flash Player API enables, there are dozens of ways that people have taken advantage of it to build something more complicated or make some task easier. And many of these people have been so kind as to share their hard-written code with the world (although a few do expect compensation).

When you build applications for the Flash platform, in addition to using the Flash Player API—a given—you can use any number of other libraries, leveraging their capabilities to pull off some impressive feats without breaking a sweat yourself. My favorite libraries include tweening libraries that let you animate things about the screen with a single line of code; physics engines that let you simulate collisions, friction, gravity, and other forces; 3D engines that let you present 3D objects and scenes; loading libraries that let you streamline the slightly annoying process of getting several types of assets loaded into a larger application; and data structure libraries that provide optimized storage for specific purposes.

A library is simply a collection of code that you can use. Sometimes you have the actual ActionScript 3.0 code that makes it up, and sometimes you use a precompiled binary (a SWC). In either case, you use libraries like you use the API. You create and access classes that they contain to get the job done. Libraries extend your programming toolbox with new, usually job-specific tools. In fact, the API is itself a library; what makes it special is that it's built into the runtime.

Because there are as many libraries as there are stars in the sky, I only mention them when there's a specific task that developers overwhelmingly use a library to accomplish, and in this case I'll just give a quick description of the library and let you know where you can find more information.

Compilers, Tools, and IDEs

There's one critical piece in this puzzle I haven't covered yet. ActionScript 3.0 code is text. You write it down in plaintext files. But to get from plaintext files to something you can actually run—in this case a SWF—you have to compile it. To put it simply, compiling translates the text of a program—the source code—into a simpler language that the runtime can run directly. We speak and understand human languages like English and French. Your computer speaks a certain instruction set that depends on what processor is inside it. It would be quite painful to program directly in the computer's language. (Some people come close by programming in assembly languages.) And it would be quite difficult and imprecise to have the computer interpret meanings from a regular, spoken language. So we have computer languages to bridge this gap—and compilers to translate. Unlike translating spoken languages, there can be no ambiguity in the compiler's translation. If the compiler doesn't understand the code you've written or believes it's incorrect, it fails to compile. You'll learn to recognize and deal with compiler errors in Chapter 5, “Validating Your Program.”

So how do you use these compilers, and where do you obtain them? This all depends on the development environment you set up for yourself. In the Flash universe, most of the available versions of the ActionScript 3.0 compiler are integrated tightly with some tool.

The tools and Integrated Development Environments, or IDEs, you're likely to come across are

Adobe Flash BuilderAdobe Flash ProfessionalFlashDevelopFDT

In addition, there are free and open-source versions of the Flex SDK available for download from Adobe. These toolchains are not IDEs, but they contain a compiler. All the tools in the previous list are either integrated with their own compiler or integrate tightly with a copy of the Flex SDK that you provide or that comes bundled with it. Because of the tight integration between these tools and the compiler, you may not even be aware of the compiler. For example, when you choose Publish or Test Movie in Flash, you invoke Flash's ActionScript compiler. With the default settings in Flash Builder, the compiler is invoked every time you save a file!

For every tool in this list but Flash, the compilers used are part of Adobe's Flex SDK, which comes in both free and open-source flavors. You can get the latest and greatest versions of these at no cost at http://opensource.adobe.com/. Specifically, these come with five major compilers: mxmlc to compile MXML and ActionScript 3.0; asc to compile ActionScript 3.0; fsch, a shell for repeated compilation; compc to compile SWCs instead of SWFs; and adl to compile and package AIR applications.