Tải bản đầy đủ (.pdf) (15 trang)

Tài liệu Built-In Object Classes ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (45.29 KB, 15 trang )


< Day Day Up >

Built-In Object Classes
In the next section, we'll briefly review many of the built-in object classes that
ActionScript provides, as well as some ways they are used. Before we do, however, it's
important to discuss how to actually get instances of classes into your project in the first
place, so you can begin to utilize their power and functionality.
You can create object instances in one of two ways. To create a new instance of the
MovieClip, Button, or TextField class, you create it on the stage or drag it onto the stage
from the library. However, only MovieClip, Button, and TextField instances can be
created in this way. To create an instance of any other class, you must use a constructor—
a simple line of code that tells Flash to create an object instance of a particular class. A
constructor looks like this:

nameOfInstance:nameOfClass = new nameOfClass();


If you wanted to create an instance of the Sound class, the constructor would look like
this:

mySound:Sound = new Sound();


Whenever you create an object instance, you must give it a unique name. This allows you
to change a property or invoke a method as it relates to that particular instance
(remember, an instance inherits all the properties and methods of the class to which it
belongs). We'll demonstrate the concept in examples. As you gain programming
experience, you'll begin to understand when you should use constructors.
While instances of the MovieClip, TextField, and Button classes can be created on the
stage manually by dragging them from the library, they can also be created dynamically


using ActionScript.
NOTE
Object names follow the same naming conventions as variables, which means they can't
contain spaces, symbols, or a number as the first character.

Some object classes are known as top-level classes, which means you can't create
instances of them using the methods we've shown. What differentiates these classes from
those of which you create instances? Top-level classes represent and control global
functionalities within your project. Take, for example, the Mouse class, which controls
cursor visibility (among other things). You have just one cursor, so it wouldn't make
sense to be able to create instances of this class; instead, you use the methods available to
it, to do various things with the mouse. Look at the example:

Mouse.hide()


This line of script hides the cursor. Notice that the name of an instance is not referenced
in relation to the hide() method. Instead, the name of the top-level class is referenced (in
this case, Mouse), followed by the name of the method you wish to use. Similar syntax is
used with any top-level class. As we go through the rest of this lesson, we'll introduce
you to other top-level classes and the syntax required to use them.
In the Actions panel under the Built-in Classes book, you can access all of Flash's built-in
classes, each of which is contained in one of these five subbooks:


Core. These classes deal with information storage and manipulation, not including
information that's being moved into or out of Flash itself.

Media. These classes assist with manipulating sound and video in your Flash
movie, such as playing sounds, gaining access to the system camera, and

streaming video.

Movie. These classes deal with visual content and system-related information such
as movie clips, text fields, the stage, and accessibility.

Client/Server. These classes control the movement of information in and out of
Flash.

Authoring. These classes assist you in creating custom actions and custom
components.
The following describes many of the built-in classes available in ActionScript as well as
where and how you might use them. We'll indicate whether a class is a top-level class
(creating instances is not required), or not, in which case you must create individual
instances.
Accessibility Class (Top-Level)
This class contains read-only information about the computer's ability to use a screen
reader:

Accessibility.isActive();


This script returns a result of either true or false. If the result is true, the user's computer
can employ a screen reader.
Array Class (Instances)
An array is a storage device for multiple pieces of information. Arrays store information
that can be set and referenced using a numbering system. For example:

var cakeType:Array = new Array();

cakeType[0] = "Chocolate";


cakeType[1] = "Angel Food";

cakeType[2] = "Baked Alaska";


The first line creates a new instance of the Array class called cakeType using the Array
constructor. The next lines place data inside that array.
The Array class contains many useful methods that will help you add, remove, and sort
array items from instances of that class.
NOTE
For more on arrays, see Lesson 6
, "Creating and Manipulating Data."

Boolean Class (Instances)
Instances of the Boolean class store one of two values, true or false. You can create a
Boolean object by using the Boolean constructor or by using the = assign operator. For
example:

var toggle:Boolean = new Boolean(false);


and

var toggle:Boolean = false;


create identical objects.
Button Class (Instances)
When you place a button on the stage, you create an instance of the Button class. Only

MovieClip and TextField objects are created in a similar fashion—that is, by placing
actual instances on the stage. The Button class contains properties and methods that allow
you to control the appearance, tab order, functionality, and more of button instances.
Capabilities Class (Top-Level)
This class contains information about the user's computer, such as screen resolution and
whether it can play sounds. The following script places the horizontal resolution of the
user's computer into myVariable:

var myVariable:Number = System.capabilities.screenResolutionX;


TIP
Being able to access computer information allows you to create movies that tailor
themselves to the capabilities of your user's computer. For example, you can determine
whether a handheld computer is accessing the movie and, if so, redirect the user to a page
designed expressly for handheld devices.

Color Class (Instances)
You use an instance of this class to change a movie clip's color dynamically. When you
create a Color object, you point it at a particular movie clip. Using the Color class's
methods, you can alter your movie clip's color. You create a Color object using the Color
class constructor method:

var myColor:Color = new Color(pathToTimeline);


Later in this lesson you'll complete an exercise using an instance of the Color class.
ContextMenu Class (Instances)
The context menu is the menu seen in the Flash player when you right-click (Control-
click on the Macintosh). This class is used in conjunction with instances of the

ContextMenuItems class (described shortly) to create customized context menus (with
custom commands), which appear when the user right-clicks (or Control-clicks) a visual
element in the Flash player window. This class also allows you to enable or disable any
and all of the built-in context menu commands (such as Play, Stop, and Print) even as
your movie plays.
This script creates a new ContextMenu object named myCustomMenu:

var myCustomMenu:ContextMenu = new ContextMenu();

myCustomMenu.hideBuiltInItems();

myCustomMenu.builtInItems.print = true;

myMovieClip_mc.menu = myCustomMenu;

myButton_btn.menu = myCustomMenu;

myTextField_txt.menu = myCustomMenu;


The second line of the script uses the hideBuiltInItems() method to hide all the built-in
menu commands, and the third line enables the Print command so that it will appear
when the menu is opened. The last three lines assign the custom menu to a movie clip,
button, and text field instance. Right-clicking (or Control-clicking) any of these instances
will cause the custom menu to appear.

ContextMenuItems Class (Instances)
This class is used in conjunction with the ContextMenu class (described just above) to
create items that appear in a custom context menu. Your Flash project can be scripted to
capture when a user clicks a custom menu item so that you can have a specific action or

actions occur. For example, you can create a custom context menu that will allow a user
to right-click in your project and choose to mute the volume.
NOTE
You will see more on the ContextMenu and ContextMenuItems classes in Lesson 20
,
"Maximum-Strength SWFs".

Date Class (Instances)
With this class you can access the current time as local time or Greenwich Mean Time, as
well as easily determine the current day, week, month, or year. To create a new instance

×