Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Behaviors Conventions

Behaviors Conventions

Behaviors are prewritten code snippets that can be instantly added to parts of a FLA file. The introduction of behaviors has added to the complexity of determining best practices in Flash, because the way some behaviors are added does not follow typical and ideal workflows. Many developers usually enter ActionScript code either into one or several frames on the main Timeline or in external ActionScript files, which is a good practice to follow. 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 convenient, save substantial time, and can be useful for novice Flash and ActionScript users. Before you start using behaviors, take a close look at how you want to structure your FLA file:

  • What behaviors do you need for your project?
  • What code do the behaviors contain?
  • How are you are going to use and implement behaviors?
  • What other ActionScript do you need to add?

If you carefully plan a document that uses behaviors, you can avoid problems that could be created by decentralizing your ActionScript code.

Comparing Timeline Code with Object Code

Planning a project and organizing a document or application cannot be underestimated, particularly when you are creating large involved projects or working in teams. This is why the placement of ActionScript code—often what makes the project work—is important.

Many developers do not place ActionScript code on symbol instances, and instead place their code on the Timeline (timeline code) or in classes. Because Behaviors add code to many locations in a FLA file, it means that your ActionScript code is not centralized and can be difficult to locate. When code is not centralized, it is difficult to figure out interactions between the snippets of code, and it is impossible to write code in an elegant way. It can potentially lead to problems debugging code or editing files.

Flash has features that make it easy to work with behaviors in a document and with decentralized ActionScript code. If you use behaviors, try the following features when working on your project:

  • Script navigator: Makes your timeline code or code on individual objects easy to find and edit in the Actions panel.
  • Find and replace: Enables you to search for strings and replace them in a FLA document.
  • Script pinning: Enables you to pin multiple scripts from various objects and work with them simultaneously in the Actions panel. This works best with the Script navigator.
  • Movie Explorer: Enables you to view and organize the contents of a FLA file, and select elements (including scripts) for further modification.

When to Use Behaviors

Knowing when to use behaviors is the most important guideline. Carefully consider your project and whether behaviors are the best solution for you, which can be determined by answering the questions that follow. Consider different ways of structuring your projects, as well as the different options and features available in Flash.

If you have a FLA file with symbols, you can select one of the instances on the Stage, and then use the Add menu on the Behaviors panel to add a behavior to that instance. The behavior you select automatically adds code that attaches to the instance, using object code such as the on() handler. You can also select a frame on a timeline, or a slide or form in a screen-based FLA file, and add different behaviors to a frame or screen using the Behaviors panel.

You need to decide when you need to use behaviors instead of writing ActionScript. First, answer the questions in the introductory section “Behaviors Conventions“. Examine how and where you want to use behaviors and ActionScript in your FLA file. Then, consider the following questions:

  • Do you have to modify the behavior code? If so, by how much? If you want to modify the behavior code to any extent, do not use behaviors. Behaviors usually cannot be edited using the Behaviors panel if you make modifications to the ActionScript code. If you plan to significantly edit the behaviors in the Actions panel, it is usually easier to write all of the ActionScript yourself in a centralized location.
  • Does other ActionScript code have to interact with the behavior code? Debugging and modifications are easier to make from a central location than having code generated by behaviors placed in many areas around your FLA file. It is very difficult to handle ActionScript when code in different parts of your FLA file interacts with each other. Debugging and interaction can be inelegant or difficult with scattered code, and sometimes it is easier to write the ActionScript code yourself. For example, if code on a timeline interacts with behaviors placed on objects, you should avoid behaviors.
  • How many behaviors do you have to use, and where do you plan to put them in the FLA file? If your behaviors are all placed on a timeline, they might work well in your document. Or, your workflow might not be impacted if you only use a small number of behaviors. However, if you use many behaviors on a ton of object instances, it could be much more efficient to write your own code on the Timeline or in external AS files.

The main difference between a FLA file with behaviors and a FLA file without behaviors is the workflow you must use for editing the project. If you use behaviors, you must select each instance on the Stage, or select the Stage, and open the Actions or Behaviors panel to make modifications. If you write your own ActionScript and put all your code on the main Timeline, you only have to go to the Timeline to make your changes.

Using Behaviors Consistently

Use behaviors consistently throughout a document when they are your main or only source of ActionScript. It is best to use behaviors when you have little or no additional code in the FLA file, or have a consistent system in place for managing the behaviors that you use.

If you add ActionScript code to a FLA file, put code in the same locations where behaviors are added, and document how and where you add code.

Note: If you are using a screen-based FLA file, see “Screens Conventions” for more information on best practices and screens.

For example, if you place code on instances on the Stage (object code), on the main Timeline (frame scripts), and also in external AS files, you should examine your file structure. Your project will be difficult to manage if you have code in all of these places. However, if you logically use behaviors and structure your code to work in a particular way surrounding those behaviors (place everything on object instances), at least your workflow is consistent. The document will be easier to modify later.

Sharing Files That Use Behaviors

If you plan to share your FLA file with other users and you use ActionScript code placed on or inside objects (such as movie clips), it can be difficult for those users to find your code’s location, even when they use the Movie Explorer to search through the document.

If you are creating a FLA file that has spaghetti code (code placed in many locations throughout the document) and plan to share the file, you should notify other users where your ActionScript is located in the FLA file. This ensures that other users immediately understand the structure of the file. Leave a comment on Frame 1 on the main Timeline to tell users where to find the code and how the file is structured. The following example shows a comment (on Frame 1) that tells users the location of the ActionScript code:

/* ActionScript placed on component 
instances and inside movie clips using behaviors.
Use the Movie Explorer to locate ActionScript */

Note: It is not necessary to use this technique if your code is easy to find, the document is not shared, or all of your code is placed on frames of the main Timeline.

Document the use of behaviors if you are working with a complex document. If you keep track of where you use behaviors, you might have fewer headaches in the long run. Depending on the size of the application, you could create a flow chart, list, or use good documentation comments in a central location on the main Timeline.

Comments