66
Chapter 3: Writing Scripts in Director
•
class A generic term for a superclass or subclass; a parent or child class.
•
instance or object instance A single object that has been created from a superclass.
Custom classes
One of the major advantages of object-oriented programming is the ability to create your own
custom classes that enable you to add custom functionality to your scripts. The predefined classes
provided by JavaScript syntax, such as Object, String, Math, and so on are useful in some cases,
but they may not provide the functionality you require to accomplish your task. For example,
suppose you want some objects in your movie to represent types of transportation, such as cars,
boats, planes, and so on, and that you want each category to exhibit unique characteristics and
functionality. Neither the predefined JavaScript syntax classes nor the predefined Director objects
may directly provide the functionality that you need. Therefore, you may want to create a new
class for each type of transportation so you can define unique characteristics for each type.
Keep in mind that when you create custom JavaScript syntax classes, you still have access to all the
features and functionality of the predefined Director objects. This means that although the
predefined Director objects may not directly provide the functionality that you need, you can still
use them in your custom classes to access their values and predefined functionality.
Constructor functions
In JavaScript syntax, a constructor function represents the class that contains the template from
which new object instances are created. Constructor functions create and initialize (set the default
state of ) properties in the new objects.
Constructor functions are essentially identical in format to regular JavaScript syntax method
functions. The difference between a constructor function and a method function is that a
constructor function uses the special
this
keyword to represent a reference to the new object that
is being initialized. A method function typically only performs some action on a given set of an
object’s data.
The following example illustrates one way to create a Rectangle constructor function that could
be used to initialize the height and width of new Rectangle objects.
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
You can also create a constructor function by using "function literal" syntax. Function literal
syntax provides the same functionality as the syntax used previously, and is merely an alternative
way to write the constructor. The following example illustrates using function literal syntax to
create a Rectangle constructor function similar to the one illustrated previously.
Rectangle = function(w, h) {
this.width = w;
this.height = h;
}
Note:
When defining constructor functions that apply to a movie, be sure to place them in a movie
script so they are available globally.
It is considered good scripting practice to give constructor functions names that map to their
functionality, and to use initial capitalization in their names, such as
Rectangle
or
Circle
.
Summary of Contents for DIRECTOR MX 2004-DIRECTOR SCRIPTING
Page 1: ...DIRECTOR MX 2004 Director Scripting Reference...
Page 48: ...48 Chapter 2 Director Scripting Essentials...
Page 100: ...100 Chapter 4 Debugging Scripts in Director...
Page 118: ...118 Chapter 5 Director Core Objects...
Page 594: ...594 Chapter 12 Methods...
Page 684: ...684 Chapter 14 Properties See also DVD...
Page 702: ...702 Chapter 14 Properties See also face vertices vertices flat...
Page 856: ...856 Chapter 14 Properties JavaScript syntax sprite 15 member member 3 4...
Page 1102: ...1102 Chapter 14 Properties...