Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flash Developer Center /

Introducing the ActionScript 3.0 debugger

by Peter Elst

Peter Elst
  • peterelst.com

Created

16 April 2007

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript debugging Flash Professional OOP

Requirements

Prerequisite knowledge

This article assumes you have a basic understanding of ActionScript 3.0 syntax, using both frame-based and class-based code, as well as the Flash CS3 authoring environment.

User level

Intermediate

Required products

  • Flash Professional (Download trial)

Sample files

  • as3_debugger_source.zip (10 KB)

With the release of Adobe Flash CS3, the debugging capabilities of the Flash authoring environment have greatly improved.

This article give you a tour of the new interface and the different features of the new debugger. I also discuss how to use it when you're troubleshooting issues with ActionScript 3.0 classes and frame scripts in your FLA files.

Debugging basics

To start you out on the right track, let's first start with the basics of debugging. You'll learn all about breakpoints, how to use them in your ActionScript 3.0 code, and the role they play in debugging your Flash application.

Setting up breakpoints

Whenever you use a debugger, it is very important to be able to run through the script line by line in order to isolate the area of the code that is causing an error or a function that is returning an unexpected value. Rather than simply playing the script from beginning to end (as it would run in Flash Player), it is very helpful to add breakpoints to specific lines of code in your script. Breakpoints allow you to define a point at runtime where the SWF will pause when viewed in the debugger.

The ability to pause the code at a specified line makes it possible to investigate the progress of the script, so that you can closely examine what is happening—both visually and by researching the values assigned to variables (more about that later). Breakpoints work the same way in an ActionScript class and the ActionScript editor for frame scripts in the timeline of an FLA file.

Setting up breakpoints is easy. To add a breakpoint, simply click in the left margin of the line of you want to put a breakpoint. The breakpoint is displayed as a red dot next to the code (see Figure 1).

file
Figure 1. Adding a breakpoint by clicking once in the column to the left of the line numbers

To remove a single breakpoint, click the red dot to remove the breakpoint. To remove all breakpoints at once, select Debug > Remove All Breakpoints from the top menu.

Breakpoints are persistent. When you save a FLA file or an ActionScript class with breakpoints defined, they appear again the next time you open the file.

Running the debugger

To run your code through the debugger, simply select Debug > Debug Movie from the top menu. If you attempt to debug a FLA file that does not contain any code, a warning message will appear: "You cannot debug this SWF because it does not contain ActionScript" (see Figure 2).

file
Figure 2. Error when attempting to run a movie through the ActionScript 3.0 debugger that does not contain code

There are two ways to include code in your Flash project. You can either write code as frame scripts on a timeline of the FLA or you can reference an external ActionScript class from the FLA file.

First, let's examine a simple FLA file that contains a dynamic text field with an instance name artists_txt. For this example, imagine that the following snippet of ActionScript exists on Frame 1 of the main Timeline:

var artists:Array = ["John Lennon","Paul McCartney","George Harrison","Ringo Starr"]; var current_artist:String = ""; for(var i:uint=0; i<artists.length; i++) { current_artist = artists[i]; artist_txt.text = current_artist; }

This example is designed to show how useful it is to use breakpoints and evaluate the values assigned to variables when a FLA is run through the ActionScript 3.0 debugger. In the code snippet above, an array containing the names of the four members of the Beatles is defined. A variable named current_artist is declared and then a for loop is used to assign the value associated with the current index of the array. This text is displayed in the dynamic text field.

If you were to run this movie in Flash Player by selecting Control > Test Movie, the movie would play and the dynamic text field would display Ringo Starr.

Go back to the FLA and add a breakpoint next to line 6. This is the line of code where the artist_txt text field is assigned a value. By setting a breakpoint on line 6, you can pause the script at that specific point to see what happens. If you run the debugger now (Debug > Debug Movie) you'll see the FLA file compile to a SWF. The movie launches in a window running the debug version of Flash Player and the interface changes to reveal the debugging interface (see Figure 3).

file
Figure 3. ActionScript 3.0 debugger interface

You may set as many breakpoints as you'd like, so that you can evaluate each line of code in the movie as desired. The interface provides valuable feedback about the variables, and the values it returns help you troubleshoot issues within the code. If Flash Player were used to run the movie, the code would execute so quickly that it would be difficult to tell exactly where the code was misbehaving.

It's time now to add breakpoints to an ActionScript class. The example below is a simple StringUtils class that includes a method to reverse a given string:

package { public class StringUtils { public function StringUtils() {} public static function reverse(s:String):String { var tmp_array:Array = s.split(""); tmp_array.reverse(); var tmp_string:String = tmp_array.join(""); return tmp_string; } } }

To evaluate what the code is doing, add a breakpoint on line 11. This is the section of the script where the tmp_string variable is returned by the reverse method of the class (see Figure 4).

file
Figure 4. Adding a breakpoint to the line of code that returns the value of a variable in order to ensure the value is correct

Now that you've added the breakpoint, create a new FLA file that uses the StringUtils class. This is necessary because the FLA file is the movie that runs through the debugger. On Frame 1 of the main Timeline, add the following line of code:

StringUtils.reverse("Flash CS3 makes debugging fun!");

If you run this Flash project using the regular Flash Player (Control > Test Movie), you won't see anything. This is because the return value from reverse method of the StringUtils class was not coded to display or do anything. However, if you run the FLA file through the debugger (Debug > Debug Movie), you'll see the debugger interface reveal a great deal of information that describes what is happening in the compiled SWF behind the scenes (see Figure 5).

file
Figure 5. Feedback from the debugger regarding the information Flash Player accesses as the movie runs

Exploring the debugger interface

Taking a closer look at the debugger interface will help you see what you can do when evaluating ActionScript code.

The two main panels of the debugger interface display the variables and call stack in the FLA. They sit alongside the Output panel, which may be familiar to you if you have used it before to trace statements in your code.

Controlling playback

Before we examine these two panels in more detail, here's a quick note about how to control the playback of the movie as you debug it.

When you choose Debug > Debug Movie, the SWF automatically pauses at the first breakpoint it encounters so that you can take your time and read the information provided in the panels. When you wish to resume playback, click the green play icon in the Debug Console panel (see Figure 6). This causes the SWF to resume playing exactly where it stopped until it encounters the next breakpoint. Clicking the red X icon in the Debug Console panel ends your debug session.

file
Figure 6. Debug Console panel

The Debug Console panel also contains buttons that allow you to Step Over, Step In, and Step Out:

Step Over makes the debugger move to the very next line of code and pause there, as though it has encountered a breakpoint. You can click this button repeatedly to slowly go line by line through your code. This is an ideal approach for finding the exact line of code in a method that is causing you problems.

Step In is very similar to Step Over, except instead of moving ahead to the very next line of code in the script, it jumps to the definition of any function referenced on that breakpoint and then allows you to step through the code of the function line by line.

Step Out moves the debugger ahead to the point in time where the current function returns a value, whether a breakpoint exists there or not. This allows you to check the values quickly to make sure they are correct.

Variables panel

The Variables panel is your best friend whenever you run a movie in debug mode. As you step through your code, the Variables panel shows you the exact value of every variable in your SWF at that point in time.

As you step through the breakpoints, you can see how the values change, which makes it possible to tell whether your code is behaving properly.

Even more exciting, the Variables panel doesn't only allow you to see the values at runtime. It also allows you to change the values currently assigned to each variable.

The ability to change values of variables at runtime is a tremendously powerful feature. This enables you to see how your application behaves under different conditions by simply changing some of the values.

Let's go back to the first example in this article, where an array contains the names of the four members of the Beatles. This time, run the FLA through the debugger and examine the code that loops through the array. As you step through the breakpoints, notice that the value assigned to the variable current_artist changes (see Figure 7).

file
Figure 7. Variables panel displaying the values assigned to each variable in the FLA

Rename the value Ringo Starr to Ozzy Osbourne and step through the code line by line until the end of the movie. You'll see that you've just changed your data at runtime. Perfect!

Call stack

The call stack is another powerful feature available as you debug your Flash project. The call stack displays references to the methods, in the order that they were called so that you can make sure they happen in the correct sequence. You can access the call stack through the Debug Console panel.

Let's return to the second example in this article. If you run the StringUtils example through the debugger, notice that the call stack is populated in the Debug Console panel. As you click the references in the panel, the ActionScript editor jumps to the corresponding method definition in the file, timeline, or frame where it is located (see Figure 8).

file
Figure 8. Using the call stack to jump to a method within a FLA or external file

As you click back through the call stack, you can actually follow along in the Variables panel to evaluate the values at that point in the movie. While you can't change the values of any of these variables at an earlier point in time, this is a great way to monitor what is happening in your Flash application. This strategy allows you to pinpoint any bugs in your code accurately.

Remote debugging

Everything I've covered so far is related to debugging your Flash project locally. You may be wondering if it is possible to perform remote debugging sessions—that is, debug code in a SWF that is running on a server.

To enable the ability to remotely debug your SWF, check the Permit Debugging option in the Publish Settings dialog box (File > Publish Settings > Flash) to allow this (see Figure 9).

file
Figure 9. Permitting remote debugging in the Publish Settings dialog box

When the Permit Debugging option is checked, breakpoints and other debugging information is saved into the compiled SWF. This results in a slightly higher file size. With that in mind, depending on your situation, you may decide to deploy your final SWF without debugging enabled.

To start a remote debug session, make sure you have Flash CS3 Professional open and from the main menu choose Debug > Begin Remote Debug Session > ActionScript 3.0.

The interface changes to the debug mode as described in this article. A message in the Output panel will appear: "Waiting for Player to connect...".

Then if you launch the SWF in a browser or in the stand-alone debug Flash Player, a dialog box asks you to specify where the debugger is running (see Figure 10).

file
Figure 10. Selecting the location of the remote SWF file to debug

For the purposes of this article, select the Localhost option because you have Flash open on your own machine. However, it is important to note that you could connect to the IP of another machine that has the ActionScript 3.0 debugger open.

Remote debugging can be very useful in situations when you want to test your Flash content running in a test environment on an actual server.

Where to go from here

The Flash engineering team has done a tremendous job improving the debugging capabilities in Flash CS3 Professional and bringing it in line with the functionality found in Flex Builder 2.

After exploring the powerful features in the ActionScript 3.0 debugger, you may find that you no longer need to use trace statements as often—because the Variables panel, in combination with the call stack, provides you with a much better alternative.

Give it a try and see how much easier it is to debug your Flash content using the ActionScript 3.0 debugger and get immediate feedback that will help you troubleshoot issues in your SWF files.

For more information about ActionScript 3.0, check out the resources available in the ActionScript Technology Center as well as the ActionScript-related Quick Starts in the Flash Developer Center.

More Like This

  • Creating ActionScript 3.0 components in Flash – Part 8: Keyboard support
  • Controlling the appearance of text elements with the Text Layout Framework
  • Deconstructing the ActionScript 3 Flash video gallery application
  • Examining the Puzzle Game sample application
  • Flash CS4 Missing Manual excerpts: Video, testing and debugging, optimization, and sound
  • Authoring mobile Flash content for multiple screen sizes
  • Augmented reality using a webcam and Flash
  • Filtering XML data in Flash applications using ECMAScript for XML
  • Formatting text for localized Flash projects
  • Skinning the ActionScript 3 FLVPlayback component

Flash User Forum

More
04/23/2012 Auto-Save and Auto-Recovery
04/23/2012 Open hyperlinks in new window/tab/pop-up ?
04/21/2012 PNG transparencies glitched
04/01/2010 Workaround for JSFL shape selection bug?

Flash Cookbooks

More
02/13/2012 Randomize an array
02/11/2012 How to create a Facebook fan page with Flash
02/08/2012 Digital Clock
01/18/2012 Recording webcam video & audio in a flv file on local drive

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement