Attaching code to objects

You must avoid attaching ActionScript code to objects (such as button or movie clip instances) in a FLA file, even in simple or prototype applications. Attaching code to an object means that you select a movie clip, component, or button instance, open the ActionScript editor (the Actions panel or Script window), and add ActionScript code by using the on() or onClipEvent() handler functions.

This practice is strongly discouraged for the following reasons:

Some Flash users might say it is easier to learn ActionScript by attaching code to an object. Some also say it might be easier to add simple code, or write about or teach ActionScript this way. However, the contrast between two styles of coding (code placed on objects, and frame scripts) can be confusing to developers who are learning ActionScript and should be avoided. Also, users who learn how to write code attached to objects often have to relearn how to place the equivalent code as a frame script at a later date. This is why consistency throughout the learning process, by learning how to write frame scripts, has advantages.

Attaching ActionScript code to a button called myBtn appears as follows. Avoid this method:

on (release) {
  // Do something.
}

However, placing the equivalent ActionScript code on a timeline appears as follows:

// good code
myBtn.onRelease = function() {
  // Do something.
};

For more information on ActionScript syntax, see Formatting ActionScript syntax.

NOTE

Using behaviors and screens sometimes involves attaching code to objects, so different practices apply when you use these features. For more information, see Using Flash.