Beginning ActionScript 2.0 2006
.pdfContents
Chapter 17: Automated Transitions |
403 |
The Tween Class |
403 |
Absolute Versus Relative Tweens |
404 |
Built-In Easing Classes and Methods |
404 |
Tween Class Methods |
405 |
continueTo() |
406 |
fforward() |
407 |
nextFrame() |
407 |
prevFrame() |
408 |
resume() |
408 |
rewind() |
409 |
start() |
409 |
stop() |
409 |
yoyo() |
410 |
Tween Class Properties and Events |
410 |
Playing Tweens in Parallel |
414 |
Playing Tweens in a Sequence |
417 |
Animating with the Drawing API |
425 |
Summary |
432 |
Exercises |
432 |
Chapter 18: Handling Text |
433 |
Creating Text Fields with ActionScript |
434 |
Field Instance Names Versus Field Variable Names |
434 |
System Fonts and Embedded Fonts |
434 |
Creating a Text Field On-the-Fly |
434 |
Working with Text Display Properties |
436 |
antiAliasType |
436 |
sharpness |
437 |
thickness |
437 |
Rich Text Formatting Options |
439 |
Using TextFormat |
439 |
New Text Format Options for Flash 8 |
440 |
Justification |
440 |
letterSpacing |
440 |
Kerning |
441 |
A Note about setNewTextFormat() |
445 |
Displaying HTML |
446 |
Image and SWF Support in HTML Text Fields |
448 |
Font Support |
450 |
Hyperlinks and the ASFunction Method |
452 |
xviii
|
Contents |
Using Cascading Style Sheets |
453 |
Creating a Cascading Style Sheet Object |
454 |
Associating a StyleSheet with a Text Field |
454 |
Define Styles Directly on a StyleSheet Object |
456 |
How to Define Tags, Class, and Properties |
458 |
Using CSS with XML |
459 |
Scroll Text Options |
461 |
Scrolling Text Using the TextArea Component |
462 |
Scrolling Text Using the ScrollBar Component |
463 |
Summary |
469 |
Exercises |
470 |
Chapter 19: Using ActionScript for Media |
471 |
Managing Images |
471 |
Smart Preloading |
473 |
Using the MovieClipLoader to Preload an Image |
473 |
Managing Sound |
474 |
Sound Class Methods |
474 |
Sound Class Events and Properties |
476 |
Creating a Sound Object |
477 |
Loading Library Sounds |
477 |
Loading External MP3 Files |
478 |
Using ID3 Tags |
478 |
Available Properties in the V2 Tags |
478 |
Streaming MP3 Audio |
480 |
Starting, Stopping, and Tracking Sound |
480 |
Event Sounds |
483 |
Controlling Volume, setPan, and setTransform |
483 |
Volume |
483 |
Pan |
483 |
Transform |
483 |
Working with the Microphone |
489 |
Microphone Class Methods |
489 |
Microphone Class Properties and Events |
490 |
Microphone Activity |
492 |
netStream |
494 |
Summary |
494 |
Exercises |
495 |
Chapter 20: Managing Video |
497 |
Terms, Technology, and Quality |
497 |
Data Rate |
497 |
Progressive — HTTP |
498 |
xix
Contents
Streaming — Flash Communication Server |
498 |
Video Keyframes |
498 |
Variable Bit Rate |
499 |
Interlacing |
499 |
Frame Rate |
499 |
Creating Flash Video |
500 |
Converting a Video |
500 |
Loading an External Video |
503 |
Exporting to Video |
503 |
Loading an External FLV File |
504 |
The netStream Class |
504 |
The Video Class |
506 |
clear() |
506 |
attachVideo() |
506 |
Video Class Properties |
506 |
Controlling Video Position |
508 |
Using Transparency within Video |
510 |
Working with Video Quality |
513 |
Deblocking and Deringing |
513 |
Scaling and Transparency |
514 |
Working with a Camera |
515 |
The Camera Class Methods, Properties, and Events |
515 |
Creating a Camera Object |
516 |
Displaying a Camera Feed as Video on the Stage |
517 |
Security Pop-Up Considerations |
522 |
Summary |
522 |
Exercises |
522 |
Chapter 21: Using ActionScript to Communicate with a Server |
523 |
External Data and Usability Concerns |
523 |
Data Latency: Asynchronous Communication |
524 |
Using LoadVars |
524 |
Ampersand-Delimited Data |
525 |
LoadVars Class Methods |
525 |
Creating a LoadVars Object |
527 |
LoadVars Event Handlers |
529 |
onData() |
529 |
onLoad() |
529 |
Loading and Parsing Raw Text |
531 |
Summary |
534 |
Exercises |
534 |
xx
|
Contents |
Chapter 22: Reading XML |
535 |
Using the Built-in XML Class |
535 |
All Wrapped Up in Tags |
535 |
CDATA and Character Formats |
537 |
The Flash XML Class |
537 |
XML Class Methods |
538 |
XML Event Handlers |
541 |
onData() |
541 |
onLoad() |
542 |
ignoreWhite: A Special Property |
542 |
Loading External XML Files |
542 |
Measuring Bytes |
545 |
A Note about Security and Locality |
545 |
getBytesTotal Isn’t Working! |
548 |
Understanding the Parent-Child Node Relationship |
549 |
Navigating an Example Node Tree |
549 |
Using Attributes |
552 |
Using the XML Socket Connection |
552 |
The XML in the XMLSocket Class |
553 |
Null Bytes |
553 |
XMLSocket Class Methods and Events |
553 |
sendAndLoad |
556 |
Using HTTP GET and POST |
557 |
Apples and Oranges |
558 |
How Will You Know? |
558 |
Using the XML Component |
559 |
Explaining Cross-Domain Security |
562 |
Understanding Why Flash Uses Domain Policies |
563 |
Knowing When Policy Files Are Required |
564 |
Setting Up Policy Files |
564 |
I’m Offering Web Services; I Want to Allow Any Domain! |
567 |
Considering HTTPS |
567 |
Load a Policy File from a Subfolder |
568 |
Using a Policy File with an XML Socket, without HTTP |
569 |
Using Shims and Proxies |
570 |
Proxies: Forcing Data Access |
570 |
Shims: Allowing Script Access |
571 |
Summary |
573 |
Exercises |
574 |
xxi
Contents
Chapter 23: Communicating Between the Macromedia Flash |
|
Plug-in and the Browser |
577 |
LocalConnection |
577 |
Creating a localConnection Object |
579 |
Security |
581 |
Storing Data Locally with Shared Objects |
582 |
Acceptable Data Types |
584 |
Using Shared Objects as Cookies |
585 |
Working with the User |
587 |
Managing Disk Space |
587 |
Sharing SharedObects |
588 |
Giving a Flash Movie Data on Startup with FlashVars |
588 |
Introduction to Flash Object and Embed Tag Parameters |
589 |
Adding FlashVars |
590 |
Creating FlashVars with JavaScript |
591 |
Passing FlashVars via a Servlet Page |
593 |
Summary |
594 |
Exercises |
594 |
Chapter 24: Putting JavaScript to Work |
595 |
Changing Security Settings |
595 |
Calling JavaScript from within Flash |
597 |
Introduction to getUrl() |
598 |
Creating a JavaScript Command with Nested Variables |
598 |
Considering Limitations |
602 |
Calling JavaScript Functions Using fscommand |
602 |
Calling Flash from JavaScript |
604 |
Basic JavaScript Methods and Events |
606 |
Using the Flash JavaScript Integration Kit |
607 |
Setting Up the Integration Kit |
608 |
Calling JavaScript Functions from ActionScript |
608 |
Calling ActionScript Functions from JavaScript |
609 |
Using the External API |
612 |
ExternalInterface Class Methods |
612 |
addCallback() |
612 |
call() |
614 |
Calling ActionScript Functions and Methods |
615 |
Calling an ActionScript Function from JavaScript |
615 |
Calling an ActionScript Method from JavaScript |
616 |
Calling JavaScript Functions from ActionScript |
617 |
Opening a Browser Window |
618 |
Setting and Getting Data Using ActionScript |
619 |
xxii
|
Contents |
Opening Browser Windows |
629 |
Using getURL() to Create Pop-Up Windows |
629 |
Use ExternalInterface to Create Pop-Up Windows |
630 |
Calling a JavaScript Wrapper Function |
630 |
Defining Browser Window Parameters |
631 |
Summary |
635 |
Exercise |
635 |
Chapter 25: Uploading and Downloading Files |
637 |
FileReference Class Methods |
637 |
Using browse() |
638 |
Using cancel() |
638 |
Using download() |
639 |
Using upload() |
639 |
FileReference Class Properties |
640 |
Downloading Files |
641 |
Uploading Files |
645 |
Summary |
651 |
Exercise |
651 |
Chapter 26: Communicating between the Flash Plug-in and |
|
the Operating System |
653 |
Using the System Object to Retrieve System Information |
653 |
System Object Limitations among Player Versions |
655 |
The setClipBoard Method |
655 |
Projectors, Flash Executables, and Other Executables |
656 |
Limitations |
656 |
Using fscommand to Call Projector Functions |
656 |
SDK |
658 |
Third Parties |
658 |
Summary |
659 |
Exercises |
659 |
Chapter 27: Creating Custom Classes |
661 |
Working with Classes |
661 |
Defining the Class |
661 |
Public Versus Private |
663 |
Defining the Constructor |
665 |
Passing Startup Data |
666 |
Defining Methods |
669 |
xxiii
Contents
Defining Properties |
675 |
Working with Getters and Setters |
676 |
Implications of Getters and Setters |
676 |
Adding Functionality to Existing Classes |
681 |
Subclassing |
681 |
Static Library |
682 |
Composition |
686 |
Summary |
695 |
Exercises |
695 |
Appendix A: Exercise Answers |
699 |
Appendix B: Flash Keyboard Shortcuts |
759 |
Tools Panel |
759 |
Integrated Script Editor Shortcuts |
760 |
File Menu |
760 |
Edit Menu |
761 |
View Menu |
761 |
Tools Menu |
761 |
Control Menu |
762 |
Actions Panel Shortcuts |
762 |
Drawing IDE Shortcuts |
763 |
File Menu |
763 |
Edit Menu |
763 |
View Menu |
764 |
Insert Menu |
765 |
Modify Menu |
765 |
Text Menu |
767 |
Control Menu |
767 |
Window Menu |
768 |
Help Menu |
769 |
Index |
771 |
xxiv
Introduction
Macromedia Flash began as a vector animation tool, designed to help put a bit more punch into otherwise motionless web pages. At the time, web pages were simple and data-driven sites were few, so animation was one way to differentiate your pages from the rest of the crowd. Many people still hold the perception that Flash is only a tool for animation, in part due to the preponderance of Flash-based site intros and a very visible presence in banner and pop-up ads. While it remains a very good tool for animation, it has grown into a powerful means to do much more.
Macromedia Flash is a full-fledged application development environment. The real focus on using Flash for coding came with version 6, which introduced a new coding and event model leading to a cleaner, more flexible coding style. That version was the first major step in giving the developer power to create many interesting and useful applications. Flash 7 added a major scripting update, resulting in ActionScript 2.0. It provides a more structured, flexible, and maintainable programming style. Finally, Macromedia Flash 8 has expanded the offering, adding bitmap support, filters, and blending modes, file upload and download capabilities, an interface for communicating with the browser, and much more. All of the add-ons can be manipulated with script, not just with visual tools within the development environment.
The mature capabilities of Macromedia Flash 8 have the potential to redefine user interactions and usability on the Web. Flash enables you to develop full-fledged applications that either run in a web browser or run standalone as an executable.
Regardless of whether you are just interested in dabbling with scripting, or you want to develop applications with thousands of lines of code, ActionScript is for you. Want to build a major e-business application, to create a personal portfolio, to create a game, or to work with media? This book will help you out. If you’ve done any work with any other scripting language such as JavaScript or programming language such as Java or Perl, you are already halfway there. Even if you’ve never touched a line of code, you still can learn to apply ActionScript to Macromedia Flash projects through the many examples shown in the successful Wrox “Try It Out” format.
Who This Book Is For
The Macromedia Flash development environment is useful for a wide range of tasks, from creating marketing pieces, to creating a personal portfolio, to creating enterprise-level e-commerce applications, to creating games. As such, the diverse group of people who use it have different backgrounds, different mixes of technical and artistic skills, and different goals. This book assumes no prior programming or Macromedia Flash development experience. If you do have basic knowledge of ActionScript, there is plenty of content about how to work with different aspects of Macromedia Flash with script. This book includes techniques for animating using only code, working with audio, video, and graphics, interacting with the user, communicating with a server, and much more. This book also includes a primer on objectoriented programming, and the last chapter shows you how to create your own custom classes, a very valuable skill to acquire.
Introduction
Regardless of what you want to get out of Macromedia Flash, this book is a good starting point to get you to where you want to go.
How This Book Is Structured
Beginning ActionScript 2.0 teaches by example, using small, well-chosen programming examples, presented in the Wrox trademark “Try It Out” format. By typing each example, running it, and examining the results, you gain experience with the entire process of coding, running, and debugging Macromedia Flash projects. Experience has shown that a hands-on approach is one of the most effective ways to learn. In addition to the code samples, each example provides you with detailed descriptions of exactly what the code does, and why it was done that particular way. Where appropriate, the example overview also talks about some of the implications of developing code in that particular way, and with a mind toward other ways of performing the same task.
Here’s a breakdown of what this book provides:
In Chapter 1, you learn the aspects of the development environment, including how to create, work with, and publish a Macromedia Flash project. You create your first scripted project, put it in an HTML wrapper, and make your project viewable on the Web.
Chapters 2–4 take you right into the guts of the language. You learn all the aspects of ActionScript, including variables, data types, conditionals, loops, events, and errors. By the time you get through these chapters, you will have a good understanding of how to use the core aspects of the language.
Chapters 5 and 6 get you up and running with ActionScript, explaining such things as where to place your code and how to structure your Macromedia Flash project. You learn simple coding conventions that will help you create projects that are easier to read and to update. You see different ways to structure your project, learn the benefits and issues with each, and find out about some techniques that will help you through the debugging process.
In Chapters 7 and 8, you start using ActionScript to work with a core Macromedia Flash technology: components. You find out all about components—how to apply them to your project, have them communicate with each other, and customize their look on-the-fly through component styles. When you’ve finished these chapters, you’ll be well on your way to building form-based applications.
Chapters 9–11 show you how to work with the core container: the movie clip. You explore how to create blank movie clips and how to draw in them on-the-fly, how to work with movie clips in the library, and how to load external movie clips. You learn how to change variables and content in one movie clip from another and how to implement a movie pre-loader. You also study different ways to structure movie clips within a project. After you have completed this chapter, you will have a good understanding of many aspects the Macromedia Flash core building block.
Chapter 12 teaches you some techniques for debugging code. You learn some of the methodology that makes debugging less of a hit-or-miss scenario, how to use the debugger, and how to use the output panel to get a better sense of what is going on with your code.
Chapters 13–15 teach you how to work with drawing and to apply effects with ActionScript. You learn how to use the drawing API, how to apply filters and blending modes, and how to work with the new Bitmap class to manipulate individual pixels.
xxvi
Introduction
You extend your knowledge of the movie clip in Chapters 16 and 17. You learn the concepts of timebased and frame-based animation, and how to animate with ActionScript. That knowledge is extended by adding basic physics to your animations, and is topped off by using the Tween class to simplify some types of animations. After finishing these chapters, you’ll know how to create scripted animations.
In Chapter 18, you learn the intricacies of working with text, including the different types of text fields, issues related to embedding fonts into your project, and how to apply visual styles to text. Because text handling is one of the largest sources of problems with new Macromedia Flash developers, this chapter is replete with annotated examples illustrating how to deal with this subject.
Chapters 19 and 20 show you how to integrate images, sound, video, and live camera feeds into your project. You learn to work with features such as loading, streaming, and embedding media. Through this chapter, you get a good grasp of the various media management issues, and learn ways to deal with issues specific to working with different media types.
Chapters 21 and 22 explain how to get your project to pass data between the web browser and a server. You learn the details of communicating with a server using a number of different communication techniques, including LoadVars and XML. Security issues are reviewed and solutions are presented for dealing with many of those issues.
Chapters 23–25 provide techniques for communication between a movie and the browser, and between separate movies. You learn to use new classes for communicating between the browser and the Macromedia Flash plug-in, for starting up movies on-the-fly with JavaScript, and for saving information long-term on the user’s computer. Cross-platform communication issues are addressed, so you can ensure that your project will work in different browsers on different platforms.
Chapter 26 examines the capabilities and limitations of working with the user’s operating system. You learn to retrieve information about the user’s operating environment through the System object. You learn to launch programs, open windows, and make your own system calls from a standalone projector. You also see examples of using third-party tools to greatly expand the capabilities of the projector. Basically, this chapter teaches you how to interact with the operating system from within the Macromedia Flash plug-in.
Chapter 27 shows you how to create custom ActionScript classes, how to think in a more object-oriented way, and some techniques for working with your own classes.
Each chapter ends with exercises for you to work on your own, and Appendix A provides solutions to those exercises.
What You Need to Use This Book
This book targets Flash 8, but because ActionScript 2.0 made its debut in Macromedia Flash MX 2004 and because the language has not substantially changed in Flash 8, much of this book is also relevant to those using Macromedia Flash MX 2004. A few features require Flash 8, and that’s noted as they’re used. You can use either Macromedia Flash MX 2004 or Flash 8 development environments, although this book assumes that you are using the professional version. You can download a fully functioning 30-day demo of Flash 8 from www.macromedia.com/downloads. Flash player version 7 does exist for Linux, although version 8 is not yet available for Linux. Macromedia does not provide a Linux development environment. As a result, you need to use either at least Mac OS X 10.3 or at least Windows 2000 to run the Flash development environment.
xxvii