Using the Trace Function

 <  Day Day Up  >  

Using the Trace Function

The trace function is invaluable in debugging Flash applications, and it can be extremely useful for debugging applications that have a heavy server-side component. The trace function can display all types of data to the user .

When the trace statement is used with a variable or even with a simple Array, Flash will display the contents of the variable or the simple Array. Consider the following code, as displayed in Figure 17.2:

 var magTypesArray :Array = new Array(); magTypesArray[0] = "News" magTypesArray[1] = "Technology"; trace (magTypesArray); 
Figure 17.2. Using the trace statement with a simple Array.

graphics/17fig02.gif

This is not the most descriptive message we could receive, but it is usable. It would be nice if it told us at what index each item was or even just that this particular Object was an Array.

The trace statement will also display the path of a movie clip Object, or an Object that inherits from the movie clip class. Consider a combo box on the Stage with an instance name of myCombo . Figure 17.3 shows what happens when a trace function is performed on the instance name.

Figure 17.3. Using the trace function with a movie clip or component.

graphics/17fig03.gif

The trace function will also display all properties of Objects that are an instance of the LoadVars class. The trace function will even display the property names and the ampersands which is exactly how the information is stored in the LoadVars Object. The data needs to be retrieved from the server using ampersands. An example LoadVars Object is shown in the following code:

 var myVariables :LoadVars = new LoadVars(); myVariables.employeeName = "Jeff"; myVariables.employeeAddress = "NY"; myVariables.employeeID = 1; trace (myVariables); 

Figure 17.4 shows the result of the preceding code.

Figure 17.4. Using the trace Action with a loadVars Object.

graphics/17fig04.gif

Consider the following XML:

 <?xml version="1.0" encoding="UTF-8"?> <employees> <employee name="James"> <description>Author</description> </employee> <employee name="Robin"> <description>Author</description> </employee> <employee name="Jeff"> <description>Author</description> </employee> </employees> 

The trace function will also dump out the contents of any ActionScript XML Object once the XML has been loaded in. This allows us to immediately see the XML data structure, as shown in the following code:

 var myXml :XML = new XML(); myXml.load ("http://localhost/myTrips.xml"); myXml.onLoad = function () :Void { trace (myXML); } 

The preceding code results in the following in Figure 17.5.

Figure 17.5. Using the trace statement to display the contents of an XML Object.

graphics/17fig05.gif

One limitation of the trace function is that it does not display useful information when an Object from the Object class is traced. Consider the following code:

 var newsWeek :Object = new Object(); newsWeek.idNo =  1; newsWeek.productName = "Newsweek"; newsWeek.productType = "News"; newsWeek.productDescription = "America's source for current events and the latest in what's happening"; trace (newsWeek); 

Figure 17.6 shows what is displayed, which is not the most useful information.

Figure 17.6. [Object Object] appears whenever an Object is traced, which can making debugging data structures difficult.

graphics/17fig06.gif

In order to debug the application and be sure that all of the data is present, we need to see all the properties of the Object instance. The [Object Object] displayed in the figure is of very little use to us, and does not assist in debugging.

The [Object Object] output gets even worse when we deal with complex data structures. If we have many Objects inside an Array, we will simply get the [Object Object] output and wonder what properties are actually contained in the Object. Consider the following code:

 var newsWeek :Object = new Object(); newsWeek.idNo =  1; newsWeek.productName = "Newsweek"; newsWeek.productType = "News"; newsWeek.productDescription = "America's source for current events and the latest in what's happening"; var time :Object = new Object(); time.idNo =  2; time.productName = "Time"; time.productType = "News"; time.productDescription = "Simply the best on indepth analysis of today's current events"; var magArray :Array = new Array; magArray[0] = newsWeek; magArray[1] = time; trace(magArray) 

When we use the trace function with the Array of Objects, the following screen in Figure 17.7 is displayed, which is not too useful for the developer:

Figure 17.7. Using the trace command with an Array of Objects.

graphics/17fig07.gif

The trace command will display some data structures, such as Arrays, XML Objects, LoadVars Objects, and variables but it does not display the contents of Object instances. If we have an Array inside of an Object, or an XML Object inside of an Object, or any combination of factors, the trace command will simply display the ubiquitous [Object Object] , which is of very little use to developers. Therefore, to debug the application, we need to build a better way to dump complex data structures. In the next section, we will build a class that will be used strictly for debugging and that will dump out complex data structures.

 <  Day Day Up  >  


Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net