Using ActionScript, you can create Flash applications that do just about anything you can imagine. But before launching into the vast possibilities, let's start with the basic foundation. The good news is that ActionScript commands follow a well-defined pattern, sharing similar syntax, structure, and concepts. Mastering the fundamental grammar puts you well on the way to mastering ActionScript.
This chapter addresses the frequent tasks and problems that relate to core ActionScript knowledge. Whether you are a beginner or master--or somewhere in between--these recipes help you handle situations that arise in every ActionScript project.
This book assumes that you have obtained a copy of Flex Builder 2 and have successfully installed it on your computer. It's also helpful if you have some experience using a previous version of ActionScript as well.
When you launch Flex Builder 2, the Eclipse IDE should start up and present you with a welcome screen. You are presented with various options to get started and more information about Flex and ActionScript 3, such as links to documentation, tutorials, and more. You can close that screen by clicking on the small "x" on its tab. Now you are in the Eclipse IDE itself, ready to start coding; but where do you go from here?
Flex Builder 2 allows you to create three kinds of projects: a Flex project, Flex Library project, and an ActionScript project. The difference is that Flex projects have access to the entire Flex Framework, which includes all of the Flex components, layout management, transitions, styles, themes, data binding, and all the other stuff that goes into making a Flex Rich Internet Application. Flex applications are written in MXML (a form of XML), which describes the layout and relationship between components. They use ActionScript for their business logic. Although you can use the ActionScript knowledge you learn from here in Flex applications you write, this book concentrates on ActionScript projects exclusively.
Now, if you are familiar with Flash 8 or earlier versions of the Flash IDE, you may be a bit baffled the first time you open up Flex Builder 2. There is no timeline, no library, no drawing tools or color pickers. You'll be doing pretty much everything by code alone, which is why it is called an ActionScript project, rather than a Flash project. So we'll first cover how to create a project and then to get you started with entering your first ActionScript statements.
You've launched Flex Builder 2 and want to create an ActionScript project.
Use the New ActionScript Project Wizard to set up your project.
An ActionScript project usually consists of at least one class file and a folder named bin that contains the SWF and HTML files output by the compiler. It also consists of a lot of internal settings to let the compiler know where everything is and how to compile it all. Flex Builder 2 takes care of most of this for you when you use the New ActionScript Project Wizard. There are a few ways to start this wizard. You can use the menu FilenScript Project from the list of available projects there. You can also click the small arrow next to the New button, which gives you the same list.
Whichever route you take to get there, you should wind up with the New ActionScript Project Wizard. Here you'll be prompted to type in a name for your project, such as ExampleApplication. Once you've created the project, you'll notice that the main application file is automatically set to the same name as the project name, with a .as extension.
Clicking the Next button gives you the opportunity to set custom class paths, additional libraries, and specify your output folder to something than the default bin. For now, you don't need to do anything here, so just press Finish to exit the wizard.
Flex Builder 2 now creates the necessary folders and files and applies all the default compiler settings for your project. In the Navigator view, you should now see a ExampleApplication project, which contains an empty bin folder and a ExampleApplication.as class file. Note that it has created this main class file for you automatically and has opened it up for editing in the code view. Also, in the Outline view, you can see a tree representation of the class, including its methods, properties, and any import statements.
This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.
To run your new application, you can press one of two buttons in the toolbar. One has a bug-like icon, which when pressed debugs the application, meaning it includes some extra information for debugging purposes and allows the use of trace statements. The button next to it--a circle with an arrow--runs the application. Both actions will create a .swf file and an HTML file, and then launch the HTML file in your default browser.
Of course, at this point, you haven't added anything to the application, so it is the equivalent of testing a blank .fla file in the Flash IDE. But go ahead and do so just to verify that everything is set up properly. You should get an empty web page with a blue background.
You want to change the dimensions of the output .swf, or its background color, frame rate, etc.
Specify properties as ActionScript Compiler arguments or metadata in the class file.
Unlike earlier versions of Flash, the ActionScript 3.0 compiler is actually a command-line compiler. Technically, you could create all your classes and directories and run the compiler from the command line with a long chain of parameters. However, it's much easier to let Eclipse keep track of all those parameters, add all of them, and run the compiler when you tell it to run.
When you create a new ActionScript project, it sets up default parameters that result in an 500 Next, choose ActionScript Compiler from the list on the left. This allows you to change several aspects of how the compiler does its job. Look for the text field labeled "Additional compiler arguments." Anything you type in this text field is passed directly to the command-line compiler as an argument.
Here are the most common arguments you will probably be using:
-default-size width height
-default-background-color color
-default-frame-rate fps
You enter them exactly as presented, with numbers for arguments, like so:
-default-size 800 600 -default-background-color 0xffffff -default-frame-rate 31
The first example sets the resulting size of the resulting .swf to 800x600 pixels. The second sets its background to white, and the last sets its frame rate to 31 fps. Multiple arguments would just be placed one after the other on the same line, like so:
-default-size 800 600 -default-frame-rate 31
Check the Flex Builder 2 help files for mxmlc options to see the full list of command-line arguments you can enter here.
The second way to change these properties is through metadata in your main class file. Metadata consists of any statements that are not directly interpreted as ActionScript, but which the compiler uses to determine how to compile the final output files. The metadata statement that is equivalent to the previous example looks like this:
[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")]
This line is placed inside the main package block, but outside any class definitions (usually just before or after any import statements).
You have a new ActionScript project and need to know where to put the code for it to execute properly.
Place ActionScript code in the constructor and additional methods of the class.
In ActionScript 1.0 and 2.0, you had many choices as to where to place your code: on the timeline, on buttons and movie clips, on the timeline of movie clips, in external .as files referenced with #include, or as external class files. ActionScript 3.0 is completely class-based, so all code must be placed in methods of your project's classes.
When you create a new ActionScript project, the main class is automatically created, and opened in the Code view. It should look something like this:
package {
import flass.display.Sprite;
public class ExampleApplication extends Sprite
{
public function ExampleApplication()
{
}
}
}
Even if you are familiar with classes in ActionScript 2.0, there are some new things here. There is a lot more information on this subject in Chapter 2, but let's go through the basics here.
The first thing you'll notice is the word package at the top of the code listing. Packages are used to group classes of associated functionality together in ActionScript 2.0, packages were inferred through the directory structure used to hold the class files. In ActionScript 3.0, however, you must explicitly specify packages. For
example, you could have a package
of utility classes. This would be declared like so:
package com.as3cb.utils {
}
If you don't specify a package name, your class is created in the default, top-level package. You should still include the package keyword and braces.
Next, place any import
statements. Importing a class makes that class available to the code
in the file and sets up a shortcut so you don't have to type the full
package name every time you want to refer to that class. For example,
you can use the following import statement:
import com.as3cb.utils.StringUtils;
Thereafter you can refer to the StringUtils class directly without typing the rest of the path. As shown in the earlier example, you will need to import the Sprite class from the flash.display package, as the default class extends the Sprite class.
Next up is the main class, ExampleApplication. You might notice the
keyword public in front of the class definition.
Although you can't have private classes within a package, you should
label the class public. Note that the main class extends Sprite. Also, a .swf itself is a type of sprite or movie
clip, which is why you can load a .swf
into another .swf and
largely treat it as if it were just another nested sprite or movie
clip. This main class represents the .swf
as a whole, so it should extend the Sprite class or any class that extends the
Sprite class (such as MovieClip).
Finally, there is a public function (or method, in class terminology) with the same name as the class itself. This makes it a constructor. A class's constructor is automatically run as soon as an instance of the class is created. In this case, it is executed as soon as the .swf is loaded into the Flash player. So where do you put your code to get it to execute? Generally, you start out by putting some code in the constructor method. Here's a very simple example that just draws a bunch of random lines to the screen:
package {
import flash.display.Sprite;
public class ExampleApplication extends Sprite {
public function ExampleApplication( ) {
graphics.lineStyle(1, 0, 1);
for(var i:int=0;i<100;i++) {
graphics.lineTo(Math.random( ) * 400, Math.random( ) * 400);
}
}
}
}
Save and run the application. Your browser should open the resulting HTML file and display the .swf with 100 random lines in it. As you can see, the constructor was executed as soon as the file was loaded into the player.
In practice, you usually want to keep code in the constructor to a bare minimum. Ideally the constructor would just contain a call to another method that initializes the application. See Recipes 1.13 and 1.14 for more on methods.
For beginners, now that you know where to enter code, here is quick primer on terminology. These definitions are briefly stated and intended to orient people who have never programmed before. For more complete definitions, refer to the Flash help files.