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

Adding a complex code snippet

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

Video: Adding a complex code snippet

Simple snippets can be as easy as a stop command, or as complex as creating ActionScript objects using complex features of the language such as constructor methods, and calling methods and setting properties of those objects. I'll demonstrate this using a code snippet that creates a countdown timer. That is, an object that waits for a particular amount of time to go by and then calls a particular function in your code. To demonstrate this, I'll use an existing movie called CountDownTimer.fla.

Adding a complex code snippet

Simple snippets can be as easy as a stop command, or as complex as creating ActionScript objects using complex features of the language such as constructor methods, and calling methods and setting properties of those objects. I'll demonstrate this using a code snippet that creates a countdown timer. That is, an object that waits for a particular amount of time to go by and then calls a particular function in your code. To demonstrate this, I'll use an existing movie called CountDownTimer.fla.

You'll find this file in the Ch01_ SimpleSnippets subfolder of the Exercise Files. In this movie, there is already an Actions layer. Within the Actions layer in frame number 1, there is a stop command that's been inserted with an existing code snippet. In the Library, the mcBiker movie clip symbol, which the biker is an instance of, also has a stop command. So now I'll go back to Scene 1 and I'll test the movie. You'll see that the biker is stationary. Because of another movie clip symbol shape tween, the hair is appearing to wave in the wind.

But otherwise the biker is stationary. Now to prepare for adding the code snippet, I'll go to the bike layer, right-click on it, and select Lock Others. Then I'm going to move the biker off the screen to the right. I'll hold down the Shift key and then click and drag the biker, making sure that I keep her at the same vertical position. Then I'll release the mouse button and the Shift key. That will be her new starting position. I want to wait for three seconds, and then cause the biker to go across the screen.

I'll make sure that the playhead is in frame number 1 again. Then I'll go to the menu and open the Code Snippets panel. Then I'll go to the Actions category. Down toward the bottom of the Actions category, you'll find a code snippet named Simple Timer. I'll double-click to add that code snippet to the first frame of the Timeline. Here is the snippet. In the first line of code, a variable is created. The name of the variable in this case is fl_TimerInstance_3.

The name may differ, depending on how many times you've executed code snippets in a particular movie. In particular, a numeric value might be added to the end of the variable name. The variable name doesn't matter at all as long as it's consistent. You'll see that it's referred to on the first line that declares the variable, and then on other lines that add bits of code. This first line of code declares the variable and sets its datatype as an instance of the Timer class. Then this code calls something called the constructor method of the class.

A constructor method is a way of creating a class and sometimes setting its initial values. The two initial values that are set when you create a Timer object are the number of milliseconds that you want to wait before something happens and then the number of times you want to execute whatever functionality you're making happen. In this bit of code, we're saying fire the Timer every second, that's 1000 milliseconds, and do it 30 times. I'm going to change that to 3000 or 3 seconds, and 3 times.

The next bit of code adds something called an event listener. Now if you're new to ActionScript, you may not recognize this. An event listener is a way of calling a particular function when an event occurs. An event is simply a way of telling the development environment that something has happened. The particular kind of event that the Timer dispatches or fires is called simply a TimerEvent. This bit of code means listen for that TimerEvent. Then call a function called fl_TimerHandler_3.

Then finally, this bit of code calls the start method or function of the Timer object. From the moment that start method is called, the Timer object will now wait for 3000 milliseconds or 3 seconds. Then it will call the function that you requested. The function is down here. Again, it's called fl_TimerHandler_3. Right now, all it does is execute a trace command, which causes a little bit of debug output when you're testing the movie in Flash Pro. Then the next bit of code increments, or adds a value of one, to an existing variable, fl_SecondsElapsed_3.

I'm going to test the code right now so we can see the debug output. I'll close the Actions layer and test the movie. After about three seconds, the Output panel appears in the background and shows the number of seconds elapsed. Now what it's really telling us is how many times the TimerEvent has executed. Now I'll do a little bit of modification to the code snippet. I'll go back to the Actions panel. I'll make the following changes.

First of all, I don't need this variable named fl_SecondsElapsed_3 to make the biker move across the screen. So I'll add a comment, starting with two slashes. That stops that code from executing. I'll do the same thing for the trace command and the increment command within the function. Within the function, I'll add a simple bit of code, the play command. I'll type in the word play followed by an opening and a closing parenthesis and a semicolon. This means start the Timeline.

Now, again this function will be called automatically when the TimerEvent is dispatched. I'll test the movie. You'll see when it first starts up, nothing happens. But after three seconds, there goes the biker. After another three seconds, there she goes again. And after another three seconds, there she goes again. So the Timer object is causing that to happen. Now you may have noticed that when the biker went across the screen that the legs weren't moving. That's because there is a stop command in the timeline of the movie clip symbol itself.

I'm going to remove that stop command by going to the Library, locating and double-clicking on mcBiker. Then I'll go to the Timeline, and look at the Actions panel. There is a single frame with an ActionScript command there. I'll open the Actions panel, and simply strip out all of the code. Alternatively, you can simply delete the Actions layer and that would have the same effect. I'll go back to Scene 1. Once again, test the movie. Now after three seconds, the biker goes across the screen.

After another three seconds, she passes by again and again. Now you may notice if you wait a few moments that this will happen again, and again, and again. That's simply because the code was placed into the first frame of the Timeline. Each time the playhead hits frame number 1, you'll execute all of this code again, firing off the start method of the Timer object. There are ways of nesting some of this code in conditional logic. So you can make sure it only executes once if that's what you want.

I'll describe some of these strategies in later videos in the series. But here I've shown you how to use a fairly complex bit of code that creates an instance of an existing ActionScript class, the Timer class, sets its parameters to the length of time you want to wait, and how many times you want to execute some code, indicates which function you want to call when the TimerEvent is dispatched, and then starts the Timer object. In reaction to the event, you've controlled when the Timeline executes. That is, when the movie clips animation begins.

The result? A simple movie that controls when animation happens, instead of allowing the animation to happen automatically whenever the movie is loaded into Flash Player.

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.

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.