Attaching or assigning multiple handlers to one object

You can also attach more than one handler to an object if you want different scripts to run when different events occur. For example, you could attach the following onClipEvent() handlers to the same movie clip instance. The first executes when the movie clip first loads (or appears on the Stage); the second executes when the movie clip is unloaded from the Stage.

on (press) {
    this.unloadMovie()
}
onClipEvent (load) {
    trace("I've loaded");
}
onClipEvent (unload) {
    trace("I've unloaded");
}

NOTE

Attaching onClipEvent() and on() handlers is not a recommended practice. Instead, you should put your code in frame scripts or in a class file, as demonstrated throughout this manual. For more information, see About ActionScript and events and Attaching code to objects.

To attach multiple handlers to one object using code that's placed on the timeline, see the following example. The code attaches the onPress and onRelease handlers to a movie clip instance.

To assign multiple handlers to an object:

  1. Create a new Flash document, and name it assignMulti.fla.
  2. Select Frame 1 of the Timeline, and add the following code in the Actions panel:
    this.createEmptyMovieClip("img_mc", 10); 
    var mclListener:Object = new Object(); 
    mclListener.onLoadInit = function(target_mc:MovieClip) { 
         target_mc.onPress = function() { 
              target_mc.startDrag(); 
         }; 
         target_mc.onRelease = function() { 
              target_mc.stopDrag(); 
         }; 
    } 
    mclListener.onLoadError = function(target_mc:MovieClip) { 
         trace("error downloading image"); 
    } 
    var img_mcl:MovieClipLoader = new MovieClipLoader(); 
    img_mcl.addListener(mclListener); 
    img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg", img_mc); 
    
  3. Select Control > Test Movie to test the document.

    The image loads into the img_mc instance, and the onPress() and onRelease() event handlers let you drag the image around the Stage.