Table of Contents

12.4 Object Methods

Methods are functions that are associated with objects and are typically used to perform an object-related task or access an object's data (i.e., set and retrieve its properties). We invoke methods with the function call operator, ( ), using the dot operator to separate the method name from its object:

objectName.methodName( )

For example:

ball.getArea( );  // Call the getArea( ) method of ball

As with properties, methods are defined by a class and then used with individual object instances. For example, the MovieClip class defines a play( ) method that we use on a particular movie clip instance (such as the main timeline of a movie):

_root.play( );

Methods can take parameters, return values, and otherwise behave exactly like functions, with the added feature that they can access and operate on the object through which they are invoked. For example, here we invoke gotoAndPlay( ) as a method of the _root movie clip object, passing the destination frame number, 5, as an argument:

_root.gotoAndPlay(5);

And here we invoke the getDepth( ) method, which returns an integer depth level:

trace(_root.getDepth( ));

Now that you know how to use methods and properties with objects of existing classes, we'll show how to create properties and methods for our own custom classes.


Table of Contents