beautypg.com

Attaching code to objects, Behaviors conventions, About behaviors conventions – Adobe Flash Professional CS3 User Manual

Page 474

background image

FLASH CS3

User Guide

468

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.

See also

“Comparing timeline code with object code” on page 469

Behaviors conventions

About behaviors conventions

Behaviors are prewritten ActionScript 2.0 code snippets that you can add to parts of a FLA file. Many developers
enter ActionScript code either into one or several frames on the main Timeline or in external ActionScript files.
However, when you use behaviors, sometimes code is placed directly on symbol instances (such as buttons, movie
clips, or components) instead of being placed on the timeline.

Behaviors are not supported by ActionScript 3.0.