Using the Built-In Classes in Flash


To continue the blueprint metaphor, a class in and of itself isn't a very useful thing until you begin creating object instances of that class. There are several ways to create an object instance of a particular class. For example, instances of the built-in MovieClip class can be created at author-time if a shape is converted into a movie clip symbol on the stage. They can also be dynamically created during run-time. There are methods designed specifically for this purpose.

As mentioned, you can create a new instance of a MovieClip object either manually (on the stage or in the library) or programmatically. You can use the following MovieClip methods to create a MovieClip instance with ActionScript:

  • this.attachMovie()Grabs a movie clip from the Library.

  • this.createEmptyMovieClip()Creates a new blank movie clip.

  • this.duplicateMovieClip()Duplicates a movie clip instance already on Stage.

There might be times when you want to access a class property or run a method without going through an instance of its class. Examples of built-in classes that you might want to access in this way include the Math, Date, and Mouse classes. You're not creating multiple instances of Mouse, but directly communicating with a single Mouse object.

For example, imagine you want to position a movie clip named dot_mc at a specific distance from the center of a circle. In the following code, the cos and sin Math methods are used with the PI Math property in a formula to determine the position of the dot:

dot_mc._x = 100 * Math.cos(45 * Math.PI/180) + 200; dot_mc._y = 100 * Math.sin(45 * Math.PI/180) + 200;


The Math object has many other useful methods that you can access directly, without having to create an instance of the Math class. These include Math.random(), Math.cos(), and Math.pow().

The Mouse object also has methods that you can access directly without creating an instance. These deal with making the mouse pointer visible in the Flash movie. By default, the mouse pointer is always visible in a Flash movie. To hide the mouse pointer, call

Mouse.hide();


To show the pointer again, call

Mouse.show();


Most of the time, you use a constructor function to create a new instance of a built-in class. To instantiate these classes, use a constructor function that consists of the instance name, the new keyword, and the name of the class from which you are creating an object. In the following syntax statement, Datatype is the same as the Class.

var instanceName:Datatype = new Class();


This should become clearer as you look at the following examples:

// create an instance of the Array object var myList:Array = new Array(); //create an instance of the  loadVars object var partyData:LoadVars = new LoadVars(); // create an instance of the  Number object var oneHundred:Number = new Number(100);


Now that you have an idea of how the constructor function works you can explore an example more fully. Imagine you're building an application that helps people organize parties. One of the many details the host needs to keep track of is the guest list. Each guest has individual information. You can create an instance of the Object datatype to hold that information.

var guest:Object = new Object();


After you create your instance, you can then use the dot syntax to start adding properties with values to be stored by the object instance.

var guest:Object = new Object(); guest.name = "Julie Smith"; guest.address = "123 Anystreet"; guest.age = 6; guest.favoriteFood = "pasta"; guest.likesMushrooms = False;


You can also create object instances for Array and Object objects by using a shortcut called literals. In the following examples, you can see that an Array object uses square brackets to enclose its elements. The Object object uses the names of the properties, followed by a colon, to organize its data.

var my_menu:Array = new Array(); //uses the constructor function var my_menu:Array = ["eggs", "ham", "beans"]; //use the array literal syntax var guest:Object = {         name:"Julie Smith",         address:"123 Any Street" }//object literal


Learning how to use the built-in classes that ship with ActionScript can help you understand classes in Flash, but you're not limited to them. You can also create your own custom classes.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

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