Attaching code to objects

Avoid attaching ActionScript to objects in a FLA file, even in simple SWF files. (Only ActionScript 1.0 and 2.0. can be attached to objects; ActionScript 3.0 cannot.) Attaching code to an object means that you select a movie clip, component, or button instance; open the Actions panel; and add ActionScript using the on() or onClipEvent() handler functions.

Attaching ActionScript code to objects is strongly discouraged for the following reasons:

  • It is difficult to locate, and the FLA files are difficult to edit.

  • It is difficult to debug.

  • ActionScript that is written on the timeline or in classes is more elegant and easier to build upon.

  • It encourages poor coding style.

  • The contrast between two styles of coding can be confusing to people learning ActionScript; it forces students and readers to learn different coding styles, additional syntax, and a poor and limited coding style.

    Avoid attaching ActionScript 2.0 to a button called myButton_btn, which looks like the following:

    on (release) {
    	//do something
    }

    However, placing ActionScript 2.0 with the same purpose on the timeline (which is encouraged), looks like the following code:

    myButton_btn.onRelease = function() {
    	//do something
    };
    Note: Different practices apply when using behaviors, which sometimes involves attaching code to objects.

  This page on the Web