22 March 2010
A basic knowledge of creating Flex 4 applications is helpful. For more details on creating and customizing Flex 4 applications, see the Flex 4 documentation, the Flex 4 in a week video series and exercises, and the Flex Developer Center.
Beginning
Debugging is the process of finding and fixing bugs, or problems, in your application. Debugging is often a difficult and frustrating task but it can be greatly facilitated with the use of a debugger, a software tool that enables you to monitor and control the execution of the program, step through your code, and monitor and change the values of variables.
Flash Builder 4 has a built-in debugger and debugging perspective that you will learn to use in this three-part tutorial series.
In Part 1, you will:
trace() to output debugging information in the Console view.In Part 2, you will:
In Part 3, you will:
In this section, you import into Flash Builder two Flex projects for an application that draws various shapes as you click and move your mouse. The starting project application does not currently work, the ending project application does. The ending application is for reference. You will debug the starting application in this series of tutorials.
Note: You will only see the pop-up error message shown in Figure 4 if you have the debugger version of Flash Player. (Typically only Flash and Flex developers have this version). Users with the normal Flash Player will not see the message; their applications will just not work.
The application works by listening for and responding to mouseDown, mouseMove, and mouseUp events. When the user presses the mouse button, a listener is added for moving the mouse. This onMouseMove() listener creates an instance of a shape class (a Circle, Square, or Star from the com.adobe.samples.shapes package), which uses the graphics API to draw a shape at the user's mouse position. When the user releases the mouse button, the onMouseMove() listener is removed so no more shapes are drawn. The application has controls in the upper-right corner to allow the user to select a different shape, change the color, and clear the drawing.
Note: This application was refactored from having all of its code in one file in the Refactoring Flex 4 Applications tutorial.
To debug an application in Flash Builder, you need two things: the debugger version of Flash Player and a debug version of your SWF.
When you install Flash Builder, the debugger version of Flash Player is installed for the different browsers you have installed on your system. You may need to manually install a debugger version of Flash Player, however, if you install a more recent non-debugger version of Flash Player in your browser after installing Flash Builder or if you just want in install a later version than the one that shipped with Flash Builder. You can tell if you need to install the debugger version of Flash Player if you get an error message when you try to debug the application in Flash Builder. You can also check by right-clicking on a SWF in a browser window and looking at its context menu (you will see Debugger in the menu) or by using the flash.system.Capabilities.isDebugger() method in your code. To install the latest debugger version of Flash Player, go to the Flash Player Downloads page, and run the uninstaller and then the appropriate installer for your browser.
When you compile an application in Flash Builder by running, debugging, or explicitly building an application, Flash Builder generates a debug version of the application's SWF file and saves it in the project's bin-debug folder. This SWF is larger than the non-debug version of the application because it includes additional code and metadata that the debugger uses. These are the SWFs you will use when debugging the application. When you are done debugging and ready to deploy your application, remember to create a smaller, non-debug version of the SWF by selecting Project > Export Release Build.
To debug an application, you use the Debug command instead of the Run command by clicking the Debug button in the main toolbar or selecting Run > Debug. The application launches in a browser window just as when you run it, but now if Flash Player encounters any errors or warnings, they are displayed in the Console view of Flash Builder.
A browser window will open and then you should be returned to Flash Builder where you may get a dialog box asking you if you want to switch to the debugging perspective (see Figure 6). If you are not immediately returned to Flash Builder, select its task in your taskbar or dock.
Note: Your panels may be arranged differently than shown here. You can rearrange panels by dragging and dropping them in different locations. You can return to the default layout by selecting Window > Perspective > Reset Perspective.
shapeColor a value of the selectedColor property of a ColorPicker.
colorSelector is the name of the ColorPicker object.The problem on line 18 is that it is executed before the ColorPicker has been instantiated, so colorSelector does not exist and you cannot reference its properties yet.
shapeColor an initial value of black:private var shapeColor:uint=0x000000;
No shapes are drawn. You fixed one problem, but there are more to find and fix.
Next, you need to figure out if the rest of the code is being executed properly. One way to determine if code inside a function is being executed and to examine variable and expression values is to use the trace() method – which you will do here. In the old days of Flash Platform application development before there was an IDE (Integrated Development Environment) with a debugger, this was the only way to debug applications. Now that there is a debugger, you can also check to see if the function is invoked and view variable values at that point by placing a breakpoint inside the function and debugging the application instead. You will use breakpoints in the next part of the series.
To use the trace() method, you pass to it a string, a variable, or an expression to be evaluated. The result is displayed in the Console view when you debug the application.
init() function, trace the string "in init", by inserting the code in bold below:private function init():void{
trace("in init");
addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
...
init() function is not being executed.
init() function is not being called.init() to be the event handler for the Application's creationComplete event:<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()"
>
init() function is now being called successfully.
In this first tutorial, you learned the basics of debugging Flex 4 applications with Flash Builder 4. You used the debugger, the debugging perspective, the Console view, and the trace() method.
In Part 2, you’ll learn how to halt code execution using breakpoints and how to control code execution using Step Into, Step Over, Step Return, and Resume commands.
In Part 3, you'll use watchpoints, the Variables view to examine and change the values of variables, and the Expressions view to watch specific variables and Expressions.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License