Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Applying code snippets to movie clip instances

From: Flash Professional CS5: Code Snippets and Templates in Depth

Video: Applying code snippets to movie clip instances

In previous videos, I have shown how to use simple snippets whose code is executed as the playhead enters a particular frame of the timeline. I am now going to show you how to use an object snippet, that is, a snippet whose code is executed when a particular event is dispatched by a particular object. For this demonstration I'll use an exiting file from the Exercise Files area. I'll choose File > Open and go to the Ch_01SimpleSnippets folder and from there, I'll locate and open a file called LogoWithButton.fla.

Applying code snippets to movie clip instances

In previous videos, I have shown how to use simple snippets whose code is executed as the playhead enters a particular frame of the timeline. I am now going to show you how to use an object snippet, that is, a snippet whose code is executed when a particular event is dispatched by a particular object. For this demonstration I'll use an exiting file from the Exercise Files area. I'll choose File > Open and go to the Ch_01SimpleSnippets folder and from there, I'll locate and open a file called LogoWithButton.fla.

In this version of the movie, I have a movie clip symbol that has its own animation. That is the biker's legs are moving, and the hair is waving in the wind. But when I test the movie you'll see nothing is happening and that's because there is a Stop command in the Actions layer of the main timeline. This movie, however, has something a little bit different. In the logo area at the bottom of the movie, the text Explore California has been turned into a Button object. A Button object in Flash is a very simple object that just has visual states.

There are four keyframes in a Button object, and these keyframes are causing the visual appearance that you are seeing. When the mouse button is down, the button appears to be pressed, and when the mouse button is up, it appears to be un-pressed. That visual appearance is defined in the button symbol, btnLogo, in the Library. I'll double-click that symbol to show you how this is being done. There are two keyframes, one in the up frame and one in the down frame and this is where the visual effect is being described.

It's the same graphical object but with a Bevel filter with reversed colors for the highlight and the shadow. Now, what matters is what happens when the user clicks that button. This is one of the most common things that Flash developers write ActionScript code for. Once you learned how to use a Stop command, typically the next thing you learn how to do is how to react to a button click. Flash will add all that code for you with a code snippet. Because this is an object snippet, you must have selected the object whose event you want to listen to.

I'll show you what happens first if you don't have anything selected. I'll click off the stage, and that causes everything to be deselected. Then I'll go to the menu and choose Window > Code Snippets. In the Code Snippets panel I'll open the Event Handlers category. Notice that there are five event handler snippets available. Three for mouse events for the Click Over and Out events, one for the Key Pressed event and one for the Enter Frame event. I am going to be using the Mouse Click event.

Watch what happens when I double-click the code snippet. I get the message, "This action requires an object to be selected on the Stage." So I'll click OK to clear that dialogue and then I'll select the button on the Stage. Notice that the button object already has a name in the Properties panel, logo_btn. If you haven't named the object, Flash will offer to name it for you when you add the code snippet. I'll use the name that's already set. I'll double-click and that adds the code snippet to the Actions panel in the frame where the playhead is currently located.

Now here is the code. I am adding the event listener to the button object. I am listening for this particular event whose name is MouseEvent.CLICK and I am reacting by calling this method or function named fl_MouseClickHandler_3. Here is the function. Flash Pro starts the code snippet by adding a trace command to help you debug and see when the button has been clicked. I'll close the Actions panel, and then I'll test the movie.

When I click the button, you'll see the Output panel appear in the background, and show the trace message "Mouse clicked." Each time I click the button, I get a new copy of the trace message. So now, let's customize the code. I'll go back to the Timeline and click on to frame one of the Actions layer and then I'll open the Actions panel. I want to react to the button click in this case by starting the Timeline and causing the biker object to travel from right to left across the Stage.

So I am going to comment out the Trace command, and then I'll place the cursor after all of the commented code, and I'll add a simple Play command, starting with the word play in all lowercase, then an opening and closing parenthesis, and a semi-colon, and that means start the animation. Now, let's test it again. I'll press Ctrl+Enter on Windows, or Command+Enter on Mac to test the movie. I'll click the button and there goes the movie clip symbol. the biker traveling across the Stage from right to left.

Each time I click, the animation happens again. So that's how you can very simply add an event listener to handle the Click event on any movie clip symbol. Now, the requirement is that you must be working with a movie clip symbol instance. The symbol instance must have a name, and again if you haven't already assigned a name to your movie clip symbol instance, Flash will offer to assign it for you, ad then you have to know what you want to do when the click event happens. In this case, I am doing something incredibly simple. calling the Play function, and starting the animation.

What you do is up to you and up to the requirements of your particular Flash movie.

Show transcript

This video is part of

Expand all | Collapse all
  1. 4m 40s
    1. Welcome
      1m 32s
    2. Understanding the prerequisites
      1m 42s
    3. Using the exercise files
      1m 26s
  2. 39m 39s
    1. Using code snippets
      4m 7s
    2. Touring the Code Snippets panel
      2m 51s
    3. Adding a simple code snippet
      3m 28s
    4. Adding a complex code snippet
      7m 45s
    5. Applying code snippets to movie clip instances
      5m 25s
    6. Showing and hiding movie clip instances
      3m 20s
    7. Handling common mouse events
      8m 1s
    8. Handling keyboard events
      4m 42s
  3. 36m 21s
    1. Adding Play and Stop actions
      5m 27s
    2. Handling the enterFrame event
      8m 5s
    3. Managing multi-scene presentations
      8m 14s
    4. Adding drag and drop
      6m 33s
    5. Rotating objects
      5m 5s
    6. Fading objects
      2m 57s
  4. 27m 14s
    1. Starting and stopping sounds
      7m 24s
    2. Showing video with FLV Player and ActionScript
      5m 15s
    3. Showing video with NetStream
      4m 19s
    4. Stopping, pausing, and resuming video
      5m 19s
    5. Seeking a cue point
      4m 57s
  5. 22m 49s
    1. Loading external SWF files
      4m 50s
    2. Adding library symbol instances to the Stage
      6m 40s
    3. Loading external images
      7m 26s
    4. Loading external text
      3m 53s
  6. 14m 36s
    1. Exploring the Code Snippets XML file
      4m 12s
    2. Modifying an existing code snippet
      3m 0s
    3. Adding a custom code snippet
      3m 41s
    4. Managing code snippet categories
      3m 43s
  7. 36m 5s
    1. Understanding template types and categories
      4m 33s
    2. Creating clickable advertising presentations
      6m 10s
    3. Reusing movie clip symbols from templates
      6m 10s
    4. Replacing content in a new file template
      6m 56s
    5. Randomizing movement in movie clip symbols
      6m 1s
    6. Scripting movement in animations
      6m 15s
  8. 18m 50s
    1. Creating a photo album
      4m 38s
    2. Creating a simple slideshow presentation
      4m 13s
    3. Creating an advanced slideshow presentation
      5m 35s
    4. Creating a desktop slideshow with Adobe AIR
      4m 24s
  9. 18m 45s
    1. Counting down dates
      3m 38s
    2. Panning a landscape
      5m 51s
    3. Revealing text with masks
      3m 48s
    4. Animating with inverse kinematics and bones
      5m 28s
  10. 42s
    1. Goodbye
      42s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Flash Professional CS5: Code Snippets and Templates in Depth.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.