802
Appendix E: Object-Oriented Programming with ActionScript 1
Defining event handler methods in ActionScript 1
You can create an ActionScript class for movie clips and define the event handler methods in the
prototype object of that new class. Defining the methods in the prototype object makes all the
instances of this symbol respond the same way to these events.
You can also add an
onClipEvent()
or
on()
event handler action to an individual instance to
provide unique instructions that run only when that instance’s event occurs. The
onClipEvent()
and
on()
actions don’t override the event handler method; both events cause their scripts to run.
However, if you define the event handler methods in the prototype object and also define
an event handler method for a specific instance, the instance definition overrides the
prototype definition.
To define an event handler method in an object’s prototype object:
1
Place a movie clip symbol with the linkage ID
theID
in the library.
2
In the Actions panel (Window > Development Panels > Actions), use the
function
action to
define a new class, as shown here:
// define a class
function myClipClass() {}
This new class will be assigned to all instances of the movie clip that are added to the
application by the Timeline, or that are added to the application with the
attachMovie()
or
duplicateMovieClip()
method. If you want these movie clips to have access to the methods
and properties of the built-in MovieClip object, you’ll need to make the new class inherit from
the MovieClip class.
3
Enter code like the following:
// inherit from MovieClip class
myClipClass.prototype = new MovieClip();
Now the class
myClipClass
inherits all the properties and methods of the MovieClip class.
4
Enter code like the following to define the event handler methods for the new class:
// define event handler methods for myClipClass class
myClipClass.prototype.onLoad = function() {trace ("movie clip loaded");}
myClipClass.prototype.onEnterFrame = function() {trace ("movie clip entered
frame");}
5
Select Window > Library to open the Library panel if it isn’t already open.
6
Select the symbols that you want to associate with your new class, and select Linkage from the
pop-up menu in the upper right of the Library panel.
7
In the Linkage Properties dialog box, select Export for ActionScript.
8
Enter an identifier in the Identifier box.
The identifier must be the same for all symbols that you want to associate with the new class.
In the
myClipClass
example, the identifier is
theID
.
9
Enter code like the following in the Script pane:
// register class
Object.registerClass("theID", myClipClass);
_root.attachMovie("theID","myName",1);
This registers the symbol whose linkage identifier is
theID
with the class
myClipClass
. All
instances of
myClipClass
have event handler methods that behave as you defined them in step 4.
They also behave like all instances of the class MovieClip, because you told the new class to
inherit from the class MovieClip in step 3.
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...