Using button and movie clip event handlers 341
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");
}
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);
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 “Using event handler methods” on page 330 and
“Attaching code to objects” on page 746.