Many actionscript 2 tutorials on the web today use what are called onClipEvent handlers, which are placed on a movie clip's personal actions panel. The problem with this is that if you want to edit some of your code from earlier, you would have to search through the individual movie clips to find what you are looking for. I prefer to use main stage events, which are placed where else but the main stage actions panel.
When creating initial variables, such as a player's health, on a movie clip, one would use the onClipEvent(load) function. Example (On a clip called hero_mc): onClipEvent(load){ this.health=100 }
However, when using main stage events no handler is required to perform this action. Example (On the main stage actions panel): hero_mc.health=100
Aside from being less code, the latter example provides the advantage of the ability to group code together, such as initializing enemy clips as well. Example: hero_mc.health=100 ememy1_mc.health=50 enemy2_mc.health=50
Another common event used is onClipEvent(enterFrame) This effect can also be duplicated using main stage events. For example, to move a character down when the user holds the down arrow key (on hero_mc): onClipEvent(enterFrame){ if(Key.isDown(Key.DOWN)){ this._y++ } }
But on the main stage: hero_mc.onEnterFrame = function(){ if(Key.isDown(Key.DOWN)){ this._y++ } }
This attaches a loop to the movie clip, even though the code is on the main stage. However, you can also have the loop attached to the main stage, while still manipulating the same movie clip: onEnterFrame=function(){ if(Key.isDown(Key.DOWN)){ hero_mc._y++ } }
Here's a list of commonly used events and their clip events and main stage events: onClipEvent(load)-----none(No handler needed) onClipEvent(enterFrame)-----onEnterFrame * onClipEvent(press)-----onPress ** onClipEvent(release)-----onRelease ** onClipEvent(releaseOutside)-----onReleaseOutside **
Keep in mind that only one event of each type can be attached to any clip, or the main stage, at a time.
* All main stage events require the addition of =function(){ after the code ** With the exception of onEnterFrame, all main stage functions must specify a symbol to use, or they will not function correctly. For example, onPress would not yield the same effect as hero_mc.onPress