We launched a new IT training category! Check out the 140+ courses now.

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

Handling the enterFrame event

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

Video: Handling the enterFrame event

In Flash Professional, there were a couple of different ways to create animation. When you first learn how to use Flash, you typically use Timeline-based animation, including motion and shape tweens where you define a beginning state and an ending state and let the Flash authoring environment fill in the differences. And then there is the more time- intensive frame-by-frame animation where you create individual images in each frame of a Timeline and then as the Flash Player plays through the Timeline, the animation takes place. You can also create animation though in pure code using ActionScript 3.

Handling the enterFrame event

In Flash Professional, there were a couple of different ways to create animation. When you first learn how to use Flash, you typically use Timeline-based animation, including motion and shape tweens where you define a beginning state and an ending state and let the Flash authoring environment fill in the differences. And then there is the more time- intensive frame-by-frame animation where you create individual images in each frame of a Timeline and then as the Flash Player plays through the Timeline, the animation takes place. You can also create animation though in pure code using ActionScript 3.

You define the animation using ActionScript code depending on an event named ENTER_FRAME. To understand code-based animation, you need to know that the ENTER_FRAME event is dispatched by all movie clip objects, including the main document itself. You listen for the event which happens a certain number of times per second depending on the frame rate of the document. This is set by default to 24 frames per second, which means that the ENTER_FRAME event happens that often. You listen for the event and you call a function when the event occurs.

The code typically looks like this. You call the addEventListener method. You pass in the name of the event, ENTER_FRAME, and you name the function that you want to call on each frame and then within the function, you make something happen, causing an object to fade, move, rotate, or otherwise change its visual state. For this demonstration, I'll use a file from the Exercise Files area. I'll open the Ch02_Timeline folder and then open the file LogoWithEnterFrame.fla. In this presentation, I'm going to move a couple of movie clip symbols around the Stage.

I'll be move both this biker object and the sun, causing the biker to move from right to left and the sun to rise. I'll test the movie and show you its current state. The biker object is a movie clip symbol instance that has its own internal animation, causing the legs to move. But right now the biker itself isn't moving. Also, notice that the sun isn't moving yet. Then I'll go to the Code Snippets panel. Here are the two code snippets I'm going to use. I'll open the Animation category. The Animate Horizontally and Animate Vertically code snippets create ActionScript based animation using this ENTER_FRAME event.

I'll start with the biker object. Now with the layer unlocked, I'll select the biker object. Notice on the Properties panel that it already has an instance name of biker. Now, I'll go to the Code Snippets panel and I'm going to add to the code snippet Animate Horizontally by double-clicking it. Here is the code snippet in its initial state. As with many other code snippets, we're adding an event listener to the object and in this case, we're listening for the event ENTER_FRAME and calling this function, fl_AnimateHorizontally. As in many other cases, an arbitrary number is added at the end by Flash.

If you want to change the name of this function, you can, just making sure that the name here in the addEventListener method matches the name of the function in its definition. The body of the function is very simple. It simply decrements the value of the X property by 10, meaning that the object is moving from right to left. I'm going to close the ActionScript panel and the Code Snippets panel and then I'm going to adjust the beginning position of the biker. I'll click on the biker object, then hold down the Shift key and then click and drag the biker to the right. I'm holding down the Shift key to make it easier to control the vertical position of the object, then I'll let both of the Shift key and the mouse button go.

So now the biker object is going to start off the stage and then move across the stage. I'll test the movie and here's the result. I've now implemented simple ActionScript-based animation. Now you can make adjustments to this animation by adjusting its values. I'll go to the first frame of the Actions layer and then to slow down the animation, I'll just change the number of pixels that the biker is moving by in each frame from 10 to 5. I'll close the Actions panel and test the movie again and now the biker is moving across the screen much more slowly.

Now let's work with the sun object. I'll close the test movie. I'll go to the sun layer in the Timeline, right-click, and select Lock Others. Then in the Stage area, I'll click on the sun object. Now as with the biker object, I want to adjust the initial position of the sun. So I'll click on it once, then hold down the Shift key and move it down a bit and I'll make it so that it's just barely visible behind the hill. Now with the object selected, once again I'll go to the Code Snippets panel.

Go to the Animation category and I'll select and insert the code snippet, Animate Vertically. Just as with Animate Horizontally, Animate Vertically adjusts the position of the selected object, this time adjusting the Y property. I'll close the Actions panel and test the movie and you'll see that the sun flies by much too fast. I'll test it again and watch the bottom of the sun object. Notice that it's cut off a little bit and in fact because the graphic,that I have been provided doesn't have a complete sun, that is the bottom isn't complete, we're only going to cause the sun to rise a little bit.

So I want to slow down the animation a lot and I want to limit how far it travels. So let's go back to the Actions panel, clicking on frame 1 of the Actions layer and then opening the panel. First, I'm going to change the number of pixels that the sun travels in each frame from 10 to 1. I can either use this expression, sun.y -= 1, or I can simplify this now by simply saying sun.y --. Now let's check the animation. The sun is now moving really slowly.

I like that a lot better, but once again I have a problem of the object being cut off on the bottom, so I want to stop the animation at a certain point. Let's go back to the Actions panel. To make this happen, I'm first going to create a variable that tracks the initial vertical position of the sun object when the movie first starts up. I'll create a variable named sunOriginalY where its data type is number and I'll set it to a value of sun.y. Because this movie only has a single frame, this code will be executed once and so I'll be saving the original vertical position and I'll be able to compare that value to the current position as I do the animation.

Now I'll go to the function where I'm changing the vertical position. I'm going to add a conditional clause. It looks like this. If, then with two open parenthesis, sunOriginalY - sun.y, and we'll put that expression in its own set of parenthesis, is greater than equal to 50 and then we'll close the conditional expression. Then I'll add a pair of braces and then in order to cause the animation to stop, I'm going to remove the event listener. To make that happen, I'll select and copy the line that's adding the event listener in the first place.

I'll copy it to the clipboard and then I'll paste it within the if statement and then I'll change the name of the method that I'm calling from addEventListner to removeEventListener. So now I'm saying if the sun has traveled as I want it to travel, I'm going to stop the animation from happening anymore. I'll close the Actions panel and run the movie again and now the sun is rising in the background, but it only rises a certain amount. I see that that wasn't enough. So I'll go back and adjust the number again until I like the animation.

I'll change the number from 50 to 150. I'll run the movie and I see that that's a little bit too much. I can be a little more scientific about this by going to the Stage and doing some calculations or I can simply do this by trial and error and so now the sun is rising to a certain extent and stopping before I see the cutoff of the graphic. So this has been a look at how to create ActionScript-based animation, using two very simple code snippets, Animate Horizontally and Animate Vertically.

You can adjust the direction and speed of the animation by changing whether you're adding or removing pixels and by changing the number of pixels you're adjusting an object's position in, in each frame.

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.