Oct 23

Debugging flash is tricky. Debugging it in a browser is even harder, since you can't trace things out. FlashTracer is great, but it only works after a moderately painful setup, you need the debug player installed, and it doesn't work for those times when you're using someone else's machine. Which is why we wrote DTDBug!

It's a simple way to see your Flash traces in a browser (as well as the regular IDE output pane). The only change to your workflow is an extra five characters! So instead of writing this:

trace("Hello world!");
you'd write this:
DTDBug.log("Hello world!");

Here's an example:

 

And here's the code it uses:

package { 
 	 
   import au.com.dtdigital.core.dtdbug.DTDBug;	 
   import flash.display.MovieClip; 
 	 
   public class DTDBugDemo extends MovieClip { 
   	 
     public function DTDBugDemo() { 
       // add DTDBug to the stage 
       this.addChild(DTDBug.getInstance()); 
       // allow usage on any URL 
       DTDBug.addAllowedURL("*"); 
       // log a test message 
       DTDBug.log("Hello world!"); 
     } 
   } 
 }

With addAllowedURL You can easily restrict which URLs it's enabled for (since there are probably times when you don't want people to be able to hit CTRL + SHIFT + SPACE and see your traces). We normally only use it on dev sites.

We hope you like it. If you find it useful, please let us know in the comments. A big thank you to Jarrod Cope for writing the original code!

Download DTDBug v1.0 (AS3 with example)

Filed under:

 
  • Print
  • Comments (3)
Jonathan Nicol
29 Jan 2010

This is a cool little class. A few suggestions:

I added the following to the show() method:

this.parent.setChildIndex(this,this.parent.numChildren-1);

Which ensures the debugger is always on top even when new children have been added to the display list.

Also you might want to mention in your example the ability to color log messages, and to add a label to messages:

DTDBug.logWithLabel("Label","Hello world!");
DTDBug.log("Hello world!", DTDBugColour.BLUE);

Color of course requires that the DTDBugColour class is imported, so instead of:

import au.com.dtdigital.core.dtdbug.DTDBug;

do:

import au.com.dtdigital.core.dtdbug.*;

Also, I noticed that the log method traces messages using the traditional trace() method, wheras the logWithLabel method doesn't.

Jonathan Nicol
29 Jan 2010

More suggestions!

It occured to me that it might be desirable to limit the length of the log, so it doesn't grow too enormous. In the update method I added:

if (this.traceField.length > 1000) {
this.traceField.text = this.traceField.text.slice(-1000);
}

Obviously 1000 could be changed to whatever is deemed to be an acceptable length for the log.

I also added a stage event listener to change the size of the textfield and background when the stage is resized. But that's might be overkill so I won't post code for that!

Brenton
DTDigital 2 Feb 2010

Hi Jonathan,

Thanks for your feedback. We’ve had a look at a few of your suggestions and have made the following amendments to DTDBug.

We’ve now added DTDBug directly to the stage. We’ve chosen this way since there would have possibly been a bug when loading into another swf. The preloading swf would have always been on top but by adding it to the stage it doesn’t matter where DTDBug is, it will always been on top.

We’ve also made DTDBug listen and readjust to stage resize events.

Lastly, we’ve added a trace method to the logWithLabel method.

Thanks again,
- Brenton

Return to our Labs blog