Code Placement


In this lesson, you have learned about the Actions panel. You've seen that you can select a frame and add code to that frame by using the Actions panel. This section discusses other places where you can add ActionScript. We'll also discuss best practices for programming. Just because you can put code somewhere doesn't necessarily mean that you should!

Frames

You have already gained experience adding ActionScript to a frame. ActionScript can be added only to a key frame. If you select a frame in theTimeline that is not a key frame and enter ActionScript into the Actions panel, it will attach itself to the nearest key frame at a lower frame number.

Buttons

If a button is selected and the Actions panel is open, you can add code directly to the button. Because the purpose of a button is to capture specific events, such as release, rollOver, and rollOut, all actions added to a button must sit within code that captures a button event.

The syntax for capturing a button event is this:

   on(someEvent) {      //actions go here    }


Instead of the word someEvent, you enter the event you want to capture. For example:

   on(release) {      getURL("http://www.electrotank.com");    }


You can add ActionScript to a button to capture several events:

   on(rollOver) {      this._xscale = 200;    }    on(rollOut) {      this._xscale = 100;    }    on(release) {      getURL("http://www.electrotank.com");    }


If the preceding ActionScript is added to a button, it gets twice as wide when the mouse is over it, it returns to normal size when the mouse leaves it, and it launches a new web page if clicked.

Movie Clips

You can add code to frames. Movie clips contain frames. Adding ActionScript to frames within a movie clip is the same as adding it to the main Timeline. You select the frame and the type in the code.

With movie clips, however, there is another place where you can add ActionScript. From outside of the movie clip, you can select the movie clip, open the Actions panel, and then enter ActionScript to attach the code to the movie clip itself.

With buttons, you have to put the code within a button event handler code. For movie clips, you must put the code within a clip event handler. Some example clip events are load, enterFrame, mouseDown, and mouseUp. The syntax to attach ActionScript to a movie clip is this:

   onClipEvent(someEvent) {      //add actions here    }


Instead of the word someEvent, you type the name of the event you want to capture.

   onClipEvent(enterFrame) {      //add actions here    }


You can capture several clip events on a movie clip like this:

   onClipEvent(load) {      //add actions here    }    onClipEvent(mouseMove) {      //add actions here    }


Best Practices

A best practice is an idea or procedure that is generally accepted as a good way to approach a certain task. With ActionScript there are several best practices, two of which are discussed here.

Best Practice 1

You can add code to frames, directly to buttons, and directly onto a movie clip. One best practice in ActionScript, which is used in this book, is to ensure that no code should ever be placed directly onto a button or movie clip. If you want to capture button events or clip events, you can do that by adding code to a frame. For instance, if you want to launch a new web page when a user clicks a button, you can add this code to a frame:

   visitWebPage_btn.onRelease = function() {      getURL("http://www.electrotank.com");    }


The only extra thing that you have to do is to make sure that your button or movie clip has an instance name. Without an instance name, you can't talk to it using code.

This practice emerged as a best practice because many Flash applications were developed with code in dozens of locations all throughout the document. When you needed to edit the code, it proved to be a nightmare to even find the code, let alone edit it. With this best practice, you still might have code in a few locations throughout your application, but knowing where to look for it just got a lot simpler.

Best Practice 2

ActionScript should always be placed on its own layer with a name of actions or something similar. This layer should not contain any assets; it should be completely blank except for the code. The layer should be locked so that you do not accidentally add an asset to that layer. It is also common to make this layer your top layer or one of the top two layers.

This emerged as a best practice for two reasons:

  • It takes the first best practice a step further. Not only does someone looking in your file know that your code is on a frame somewhere, but this person knows it will be on a blank layer called actions (probably at the top of the layer stack).

  • Keeping the layer empty of assets helps keep you from accidentally adding ActionScript to a movie clip or button. Also, if you mix assets onto the actions layer and then want to delete all the assets on the layer, you might be tempted to delete the layer itself, which would remove the code!




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net