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
provide unique instructions that run only when that instance's event occurs. The
and
actions don't override the event handler method; both events cause their scripts to run.
on()
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:
Place a movie clip symbol with the linkage ID
1
In the Actions panel (Window > Development Panels > Actions), use the
2
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
duplicateMovieClip()
and properties of the built-in MovieClip object, you'll need to make the new class inherit from
the MovieClip class.
Enter code like the following:
3
// inherit from MovieClip class
myClipClass.prototype = new MovieClip();
Now the class
Enter code like the following to define the event handler methods for the new class:
4
// define event handler methods for myClipClass class
myClipClass.prototype.onLoad = function() {trace ("movie clip loaded");}
myClipClass.prototype.onEnterFrame = function() {trace ("movie clip entered
frame");}
Select Window > Library to open the Library panel if it isn't already open.
5
Select the symbols that you want to associate with your new class, and select Linkage from the
6
pop-up menu in the upper right of the Library panel.
In the Linkage Properties dialog box, select Export for ActionScript.
7
Enter an identifier in the Identifier box.
8
The identifier must be the same for all symbols that you want to associate with the new class.
In the
myClipClass
Enter code like the following in the Script pane:
9
// register class
Object.registerClass("theID", myClipClass);
_root.attachMovie("theID","myName",1);
This registers the symbol whose linkage identifier is
instances of
myClipClass
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.
802
Appendix E: Object-Oriented Programming with ActionScript 1
or
onClipEvent()
method. If you want these movie clips to have access to the methods
inherits all the properties and methods of the MovieClip class.
myClipClass
example, the identifier is
have event handler methods that behave as you defined them in step 4.
event handler action to an individual instance to
on()
in the library.
theID
.
theID
with the class
theID
onClipEvent()
action to
function
or
attachMovie()
. All
myClipClass
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?