OBJECT TYPES


Objects are organized in classes, which represent sets of objects that share the same properties and methods. In the real world, cars represent one class of objects; fish, books, and even people represent others. The 6 billion people on this planet can each be thought of as instances of the Person class. Although each of these people is unique, they share similar properties (name, age, hair color, height, and so on) and actions (breathing, walking, touching, seeing, and so on). Since each new movie clip instance that you create is an instance of the MovieClip object, it inherits all of the properties and methods available to that class of objects.

graphics/04fig04.gif

Most projects contain multiple instances of some classes of objects for example, the MovieClip object we just discussed or the String object (since your project may comprise various strings, each considered an instance of the String object). In contrast, other objects are universal or global, meaning your project can contain only once instance of them. Take, for example, the Mouse object, which controls cursor visibility (among other things). Since you have just one cursor, you can't create instances of it; instead, you simply use the methods available to it to do various things with the mouse.

You can create object instances in one of two ways: To create a new instance of the MovieClip object, you simply drag it onto the stage. However, you can only create a few types of objects in this way. To create other object instances, you must use a constructor a simple line of code that tells Flash to create an instance of a particular object. A constructor looks something like this:

 nameOfInstance = new nameOfObject(); 

Thus, if you wanted to create an instance of the Sound object, the constructor would look like this:

 nameOfSoundInstance = 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 as we'll demonstrate in the following examples. As you gain programming experience, you'll begin to understand when you should use constructors.

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.


TIP

Flash allows you to attach a suffix to an object name in order to facilitate code-hinting in the Actions panel. For example, instead of naming a movie clip instance myMovieClip, you could name it myMovieClip_mc. Using this naming convention enables the Actions panel to determine when an object of a certain type is being scripted and thus quickly provide you with a drop-down list of appropriate properties and methods for that object. Consult the ActionScript dictionary for a complete list of these suffixes.


In the Actions panel under the Objects book, you can access all of Flash's objects, each of which is contained in one of the following four objects' subbooks:

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

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

  • Client/Server. These objects control how information is moved in and out of Flash.

  • Authoring. These objects assist you in creating custom actions and custom components.

The following describes the many object classes available in ActionScript as well as where and how you might use them. We'll also indicate whether an object is global, or you must create individual instances.

Accessibility Object (Global)

This object contains read-only information about the user's computer's ability to use a screen reader:

 Accessibility.isActive(); 

The above line of ActionScript returns a result of either true or false . If the result is true, the user's computer can employ a screen reader.

Array Object (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 instance:

 cakeType = new Array();  cakeType[0] = "Chocolate";  cakeType[1] = "Angel Food";  cakeType[2] = "Baked Alaska"; 

The first line of script above creates a new instance of the Array object called cakeType using the Array constructor. The next few lines place data inside that array.

The Array object contains many useful methods that will help you add, remove, and sort array items.

NOTE

For more on arrays, see Lesson 7, Working with Dynamic Data.


Boolean Object (Instances)

The Boolean object stores 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 instance:

 toggle = new Boolean(false); 

and

 toggle = false; 

create identical objects.

Button Object (Instances)

When you place a button on the stage, you create an instance of the Button object. Only the MovieClip and TextField objects are created in similar fashion that is, by placing actual instances on the stage. The Button object contains properties and methods that allow you to control its appearance, tab order, functionality and more.

Capabilities Object (Global)

This object contains information about the user's computer, such as screen resolution and whether it can play sounds. The following line of ActionScript places the horizontal resolution of the user's computer into myVariable:

 myVariable = System.capabilities.screenResolutionX; 

TIP

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


Color Object (Instances)

You use this object to change a movie clip's color dynamically. When you create a Color object, you point it at a particular movie clip. Then, using the Color object's methods, you can alter your movie clip's color. You create a Color object using the Color object constructor method, as follows:

 myColor = new Color(pathToTimeline); 

Later in this lesson you'll complete an exercise using the Color object.

Date Object (Instances)

With this object, 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 of the Date object, you would use the Date object constructor method. The following example demonstrates one use of the Date object:

 now = new Date();  largeNumber = now.getTime(); 

This creates a variable called largeNumber whose value is the number of milliseconds since midnight January 1, 1970.

NOTE

We will use the Date object in Lesson 15, Time- and Frame-Based Dynamism.


Key Object (Global)

You use the Key object to determine the state of the keys on the keyboard for example, whether caps lock is toggled on, which key was pressed last, and which key or keys are currently pressed.

You will complete an exercise using this object later in this lesson.

LoadVars Object (Instances)

Flash allows you to load data (for example, variables) into a movie from an external source. Using the LoadVars object, Flash can load in variables from a specified URL (which can be a static text file). For example:

 myOb = new LoadVars();  myOb.load("http://www.mysite.com/myFiles/file.txt"); 

In the above example, all of the loaded variables become properties of myOb .

Math Object (Global)

With the Math object, you can perform many useful calculations and have the result returned. Here's one use of the Math object:

 positiveNumber = Math.abs(-6); 

The above script uses the absolute value method of the Math object to convert 6 to a positive number.

Mouse Object (Global)

The Mouse object controls cursor visibility, as well as allows you to set up listeners to track mouse activity. Here is an example use:

 Mouse.hide(); 

The above line of script hides the mouse from view. The mouse is still active, just not visible.

MovieClip Object (Instances)

You create instances of this most familiar object either in the authoring environment (by placing them on the stage) or with ActionScript actions such as createEmptyMovieClip() and duplicateMovieClip() not by using the constructor method. Movie clips have many properties and methods that are used frequently in an interactive project. Here's an example use:

 myClip.gotoAndStop("Menu"); 

With this script, a movie clip with an instance name of myClip will be sent to the frame labeled Menu.

Number Object (Global)

You can create a Number object instance by using its constructor method or by assigning a number as the value of a variable. For instance:

 age = new Number(26); 

is equivalent to:

 age = 26; 

The new Number() constructor method is rarely used, however, because creating a new number without the constructor is shorter and achieves the same result.

Object Object (Instances)

No, it's not a typo: There is an Object object! You can use this generic object which is also known as ActionScript's root object (meaning it's the highest object in the class hierarchy) in various ways: By employing the properties and methods available to it, you can affect and modify other object classes (such as those listed in this section). It also comes in handy for creating objects that hold information about the current user or that track chunks of related data (to name just a couple of uses).

The following is the syntax for creating a generic object:

 names = new Object();  names.cat = "Hayes"; 

The first line of script creates a new object called names . The second line adds a variable to the object called cat . The variable is considered a property of this object.

In Lesson 6, Customizing Objects, we'll show you how to create your own custom objects (better than generic objects!) as well as how to create properties and methods for your custom object. Once you know how to do this, you can create objects that do precisely what you want.

Selection Object (Global)

You use the Selection object to retrieve information or set characteristics relating to selected items in your movies, especially text in text fields. When the cursor is in an area of a text field, that field is said to be "in focus." You can employ the Selection object to set the focus to a specific text field, to find out which text field is currently in focus, or even to programmatically select specific chunks of text in a field so that it can be manipulated in some way. The following is an example of one use of the Selection object:

 Selection.setFocus("firstName"); 

This script sets the input text field with the instance name of firstName into focus.

You'll complete an exercise using this object later in this lesson.

Sound Object (Instances)

You use the Sound object to control sounds for example, setting volume and adjusting left and right speaker pan settings. To learn more about this object, see Lesson 16, Scripting for Sound.

String Object (Instances)

You use the String object to manipulate and get information about strings. You can create a new string by using the String object constructor method or by putting quotes around a value when setting a variable. For instance:

 bird = new String("Robin"); 

is the same as:

 bird = "Robin" 

You'll use this object to complete an exercise later in this lesson.

Stage Object (Global)

With the Stage object, you can control and get information about characteristics of the stage, such as alignment. For instance:

 Stage.height 

The above line of ActionScript returns the height of the stage in pixels.

System Object (Global)

This object contains information about your user's computer's system, such as the operating system, the language being used, and all of the properties of the Capabilities object. One of the System object's properties is a string, called serverString, which contains a list of the system capabilities (concatenated into one string). You can then send this list to the server so that you can store or use the information it contains. To access the string, you would use the following:

 System.capabilities.serverString 

TextField Object (Instances)

Using this object, you can dynamically create a new text field and control most of its characteristics for example setting the format of the text field or the scrolling of text. Instances of this object are created when a text field is placed on the stage or created dynamically using createTextField() . We'll use this object later in this lesson.

TextFormat Object (Instances)

TextFormat objects are used to change the format of text displayed in text fields. Once created, you apply the TextFormat object to a text field using the setTextFormat() or setNewTextFormat() method of the TextField object:

 nameOfTextField.setTextFormat(nameOfFormatObject); 

Xml Object (Instances)

XML is one of the most popular standards for formatting data no surprise when you consider that XML-formatted data lets all kinds of applications transfer information seamlessly. Using Flash, you can create an XML object to store an XML-formatted document (which can then be sent from or loaded into XML objects). Here's one use of the XML object:

 myXML = new XML();  myXML.load("myFile.xml"); 

The above script creates a new XML object and loads an XML-formatted file into that object.

XMLSocket Object (Instances)

Flash also allows you to set up a persistent connection with a socket server an application that runs on a Web server. The socket server waits for users to connect to it. Once connected, the socket server can transfer information between all connected users at very fast speeds which is how most chat systems and multiplayer games are created. An XML socket is called such because it uses the XML format as the standard for transferred information. You can create an XMLSocket object instance by using its constructor method. The following is an example of one use of this type of object:

 mySocket = new XMLSocket();  mySocket.connect("http://www.electrotank.com", 8080); 

This script creates a new XMLSocket object and then opens up a connection with a socket server. See Lesson 12, Using XML with Flash, for a detailed description of socket servers and the XMLSocket object as well as an exercise in creating your own chat application.

It's beyond the scope of this book to cover every object in detail. However, during the course of this book, we'll use many of these objects in various ways, and provide detailed instructions about how and why we're using them. The following exercises will concentrate on just a few of these objects to give you a general idea of how you can use them.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net