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

Using the currentFrame and totalFrames properties

From: ActionScript 3.0 in Flash Professional CS5 Essential Training

Video: Using the currentFrame and totalFrames properties

Have you ever wanted to track the playback of a SWF file as a percentage, like a video? You can do that using simple math and some built-in properties. On the Stage, I have my main animation, which is main_mc, and then I have playPaused_mc that controls the playing and pausing of the animation. I am going to have prog_mc. That's a progress bar. By double-clicking, you can see that its Timeline is a 100-frame animation.

Using the currentFrame and totalFrames properties

Have you ever wanted to track the playback of a SWF file as a percentage, like a video? You can do that using simple math and some built-in properties. On the Stage, I have my main animation, which is main_mc, and then I have playPaused_mc that controls the playing and pausing of the animation. I am going to have prog_mc. That's a progress bar. By double-clicking, you can see that its Timeline is a 100-frame animation.

As a side note, you'll see that I use the classic tween instead of a motion tween; sometimes I just do that because it's faster for me, but there is no particular reason why you would need to use a classic tween here. On the first frame, I have a stop action, and that's all there is to that movie. What we are going to do is track the progress of the main animation as a percentage, and tell the prog movie clip to go to the frame that corresponds to the percent that's played in the movie.

So let's go to the first keyframe of the actions layer and open up the Actions panel. Inside of playClicked, below the last line of code that's in there, I am going to add an EventListener to main_mc. Type main_mc.addEventListener, capital E for Event, capital L for Listener; it should be blue after you type it. Then in the parentheses, I am going to type the event that I want to listen for.

Now this event is called ENTER_FRAME. It runs every time your movie enters a new frame. So it's executed with each frame. That's the perfect interval to track the playback progress for this animation. So the ENTER_FRAME event is Event with a capital E Event, .ENTER, in all caps, _FRAME, also in all caps. This should all be blue. So type a comma, and then after that, we'll type trackProgress.

So that will be the function that it will create. Now before we go on, I want to take this line of code, copy it with Command+C or Ctrl+C, and then paste it at the bottom of the pauseClicked function. Now the reason I want to do this is because I want to remove the EventListener. The ENTER_FRAME event runs very frequently and takes up memory in the Flash player. So when you don't need to run it, like when the movie is paused, it's best to not use it all.

So let's change add to remove in the pasted code, and that way we won't use memory when we don't need to. Below the pauseClicked function, let's define the trackProgress function. So type function trackProgress with a capital P, some parentheses, and inside of the parentheses we need to receive an event object, so type evt:Event with a capital E. After the close parenthesis, type colon, void, all lowercase, then type some curly braces.

So I have open and close curly braces, and I am indenting here. To track the progress, we are going to need two things: the current frame that the movie clip is on and the total number of frames the movie clip has. So in the trackProgress function, we are going to create a variable called percent. And this is going to be a Number, capital N. I am going to set it equal to the percentage of the movie that has been played.

To find the percentage, we are going to take the current frame that the movie is on and divide that number by the total number of frames in the movie. That will give us a number between 0 and 1 that will represent the percentage, and then we can multiply that number by 100 to get a whole number percentage. So let's see how that works. Type main_mc.currentFrame / main_mc.totalFrames.

Before we move on, let's just trace the value of percent to see what we get. Test the movie and then click the Play button and just look in the Output window. We'll close the movie, and we'll scroll down, and we get these values that start off at 0.01, 0.02, 0.03, and going on and on.

So what this represents is right now where I have highlighted 0.06 that is 6% of the movie that's played. So I ended at about 28% of the movie played. So if we multiply this value by 100, by moving that decimal point two places to the right, then we can get a whole number to use as a frame. So let's go back to our code, and we'll look at getting a whole number.

So the first I am going to do is wrap some of this code in parentheses, and parentheses in math, in ActionScript, work just select like parentheses in math class. They make sure that one process is done before moving onto another process. So they give this priority, this division right here. So I am going to take that, I am going to multiply that value by 100 to move the decimal point, and there we should have a number between zero and 100. The problem is it's still not going to be a whole number.

We need to round it off somehow. So I'll take that and wrap this whole thing in parentheses, so I'll have two open parenthesis at the beginning of this equation, and at the very beginning I am going to type math.round, and then I am going to delete that extra parentheses that Flash created for me. And what this is going to do is it's going to take this whole number and round it off. So instead of having 28.075, I am just going to have 28.

So I can use this percent to correspond to a frame in the progress bar movie clip. So we'll go to the next line and type prog_mc. That's the name of the progress bar .gotoAndStop; capital A, capital S, and in parentheses pass in the word 'percent.' Now let's test the movie, see what we get. Click the Play button and watch the playhead at the bottom of the screen track the progress of the animation.

So you see the bear walk in, eat the fish, and then walk out, and the whole time you can see the progress right at the bottom of the screen. Notice at the end, the progress bar goes all the way back to the beginning. So now I'll close the preview window. It looks like everything is working great. So by using the current frame and total frame properties, along with common math equations and rounding numbers, you can track the playback progress of an animation.

Show transcript

This video is part of

Expand all | Collapse all
  1. 3m 22s
    1. Welcome
      1m 23s
    2. Using the exercise files
      1m 17s
    3. Using the function keys
      42s
  2. 23m 38s
    1. Adjusting preferences for ActionScript fonts, colors, and formatting
      3m 25s
    2. Changing Flash Player and ActionScript versions in the Publish settings
      1m 35s
    3. Reading and solving errors through the Compiler Errors window
      2m 49s
    4. Using the Actions panel buttons to add and remove comments
      2m 33s
    5. Using the Actions panel to format code
      1m 49s
    6. Using the Actions panel Toolbox
      2m 4s
    7. Seldom-used but helpful functions of the Actions panel
      3m 14s
    8. Understanding code hinting
      2m 3s
    9. Reviewing the Code Snippets panel
      3m 7s
    10. Using help
      59s
  3. 45m 50s
    1. Understanding how ActionScript 3.0 code is processed in the Flash Player
      3m 22s
    2. Understanding variables
      4m 56s
    3. Understanding functions
      9m 1s
    4. Understanding events and event listeners
      5m 47s
    5. Working with conditional statements
      9m 49s
    6. Creating arrays and vectors
      6m 50s
    7. Using the For command to create a loop
      6m 5s
  4. 49m 9s
    1. Reviewing the display list
      3m 0s
    2. Understanding instances referenced through ActionScript
      2m 27s
    3. Using dot syntax to modify properties in an instance
      3m 25s
    4. Placing objects at the center of the stage
      4m 2s
    5. Placing objects at the edges of the stage
      5m 53s
    6. Using the methods of an instance
      3m 44s
    7. Accessing the parents, children, and grandchildren of objects
      5m 50s
    8. Creating instances from Library movie clips using ActionScript
      4m 23s
    9. Adding objects to the stage and changing parents with the addChild method
      5m 45s
    10. Removing objects from the stage with the removeChild method
      4m 17s
    11. Using the numChildren property to loop through a container's child objects
      3m 17s
    12. Using the getChildByName method
      3m 6s
  5. 56m 20s
    1. Understanding timelines
      4m 15s
    2. Using common timeline navigation methods
      5m 34s
    3. Using the currentFrame and totalFrames properties
      8m 2s
    4. Controlling the timeline of an instance
      6m 41s
    5. Detecting and navigating frame labels with ActionScript
      7m 57s
    6. Solving problems when timelines and ActionScript animation collide
      4m 16s
    7. Condensing a multi-frame timeline into one frame
      8m 33s
    8. Creating a simple slide presentation app in the timeline
      7m 51s
    9. Using one event handler with multiple buttons
      3m 11s
  6. 36m 5s
    1. Creating a class using Flash templates
      4m 43s
    2. Setting a document class
      6m 51s
    3. Preparing a class to be connected to a symbol
      4m 31s
    4. Using the Symbol Properties menu to connect a symbol to a class
      4m 55s
    5. Resolving problems with instances in a linked class
      7m 53s
    6. Understanding packages
      3m 17s
    7. Working with ActionScript source paths
      3m 55s
  7. 44m 32s
    1. Viewing the finished game
      51s
    2. Viewing the FLA file
      2m 9s
    3. Creating the DragDrop and Map classes
      2m 51s
    4. Linking the draggable class to Library symbols
      2m 47s
    5. Adding drag-and-drop functionality
      3m 38s
    6. Saving and resetting an object's position
      3m 33s
    7. Giving a target drop object to the draggable objects
      13m 16s
    8. Showing a Win screen
      7m 3s
    9. Resetting the game
      8m 24s
  8. 29m 6s
    1. Loading bitmap images from the Library
      4m 6s
    2. Loading bitmap images from external files
      5m 22s
    3. Adding mouse functionality to bitmap images
      3m 31s
    4. Using a loop to load multiple images
      6m 14s
    5. Creating a simple slideshow
      8m 37s
    6. Using Flash Player 10 color management
      1m 16s
  9. 27m 13s
    1. Loading an external SWF
      4m 14s
    2. Running ActionScript code in an external SWF from its parent
      5m 30s
    3. Running parent code in a child SWF
      5m 7s
    4. Creating a timeline-based preloader to load an external SWF file
      5m 3s
    5. Displaying playback progress of a loaded SWF file
      7m 19s
  10. 40m 10s
    1. Creating plain text files
      2m 8s
    2. Loading text from an external text file
      6m 26s
    3. Loading multiple text files
      6m 43s
    4. Rendering simple HTML in a text field
      5m 51s
    5. Creating a scroll bar for a text field
      5m 29s
    6. Scrolling a text field
      4m 59s
    7. Scrolling movie clips and other objects using masks
      5m 42s
    8. Modifying TLF text properties through ActionScript
      2m 52s
  11. 23m 40s
    1. Reviewing XML and E4X syntax
      3m 29s
    2. Loading an XML file
      3m 26s
    3. Using dot syntax to access XML data
      4m 2s
    4. Using XML data to populate a DataGrid component
      7m 4s
    5. Using XML data to load image files
      5m 39s
  12. 23m 33s
    1. Loading audio from the Library
      1m 41s
    2. Loading audio from external files
      3m 41s
    3. Playing, pausing, and stopping sounds
      5m 39s
    4. Muting all audio with the SoundMixer.stopAll method
      1m 28s
    5. Tracking load progress
      2m 38s
    6. Displaying sound position
      5m 5s
    7. Adjusting volume
      3m 21s
  13. 19m 54s
    1. Touring the FLA file
      5m 57s
    2. Controlling video playing and pausing with ActionScript
      1m 56s
    3. Working with ActionScript cue points to add closed captioning
      3m 35s
    4. Displaying video playback position
      3m 44s
    5. Adjusting video volume
      4m 42s
  14. 5m 32s
    1. Using new code snippets for AIR and mobile
      1m 13s
    2. Viewing the new code snippets HUD
      1m 17s
    3. Loading assets with the new ProLoader class
      1m 8s
    4. Understanding Flash Player premium features
      1m 54s
  15. 16s
    1. Goodbye
      16s

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.

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 ActionScript 3.0 in Flash Professional CS5 Essential Training.

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.