Code Snippets in Flash CS5 - Page 1
       by kirupa  |  19 July 2010

While you can do a lot using the timeline and drawing tools in Flash, adding interactivity almost always involves writing code. To help those of us who know a little bit of ActionScript 3 but could use a leg up to get running, you have some help via the Code Snippets feature introduced in Flash CS5.

What are Code Snippets?
Code snippets are essentially generic pieces of code that demonstrate how to do something. For example, let�s say that you want to do something when a button is pressed. This involves two things - creating an event handler and wiring up an event to call that event handler when fired.

The code for doing that would look as follows:

myButton.addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler (event:MouseEvent):void
trace("Mouse clicked");

Notice that no assumption about your particular project or preferences is made. I am using a generic instance name for both the button instance as well as the event handler associated with the Click event. To make it more applicable to your application, it is up to you to modify and revise it.

Despite the code being very generic, it does help you recall how to associate an event with an even handler. In general, code snippets are not meant to be a replacement for getting away with adding interactivity without writing code.Code snippets are really just shortcuts to help you recall how to do something that you simply forgot the syntax for. You can argue that programming is really about taking your ideas and remembering the appropriate syntax to use to bring your ideas to life.

The Code Snippets Panel
Similar to how behind every awesome man is an even more awesome woman, behind every awesome feature is an awesome panel that takes up space in the UI. The Code Snippets feature is no different.

The Code Snippets panel can be accessed by clicking the Code Snippets button that by default is docked to the left of your Properties panel:

[ the Code Snippets button brings up the Code Snippets panel ]

If you don�t see the Code Snippets button, go to Window | Code Snippets to display the panel. Regardless of how you get the panel up, the Code Snippets panel will look like the following image:

[ behold - the Code Snippets panel in all its magnificence! ]

This panel displays a categorized list of code snippets that you can browse and add. In the next page, let's look at how to actually take some of these code snippets and use them!

Onwards to the next page!

1 | 2

SUPPORTERS:'s fast and reliable hosting provided by Media Temple.