![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.
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.
theID
in the library. 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.
// inherit from MovieClip class myClipClass.prototype = new MovieClip();
Now the class myClipClass
inherits all the properties and methods of the MovieClip 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");}
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
.
// 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.
function myClipClass(){} myClipClass.prototype = new MovieClip(); myClipClass.prototype.onLoad = function(){ trace("movie clip loaded"); } myClipClass.prototype.onPress = function(){ trace("pressed"); } myClipClass.prototype.onEnterFrame = function(){ trace("movie clip entered frame"); } myClipClass.prototype.myfunction = function(){ trace("myfunction called"); } Object.registerClass("myclipID",myClipClass); _root.attachMovie("myclipID","ablue2",3);
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |