A Simple in-swf debugger
When developing a Flash app for the web sometimes it can be painful seeing what’s going on as you can’t trace things in the browser - not that I’m aware of anyway.
If you use FireBug in FireFox when you are developing you can set up a function in javascript that allows you to see what you a tracing using this same method but outputting it to the javascript debugger and/to FireBug using:
function debug(aMsg) {
setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}
I didn’t write this I can’t remember where I found it probably at on here somewhere. Also you don’t need to use FireBug but if makes it a lot easier.
So you call this function from flash using using either an External Interface call or using getURL for older versions of the Flash player. If you write the function right you can include a trace as well so you can follow the same process while working in Flash itself. If you want to be really clever you can output the things you are tracing to a TextArea within Flash as well. Here is idea of what the function might look like:
//str is what you want to trace
function debugging(str){
//trace the str in Flash
trace(str);
if (_root.debugger.showText == 1) {
//this is the putting str in the TextArea
_root.debugger.alert.text = str+'
‘+_root.debugger.alert.text;
}
//this calls the debug function in javascript
getURL(’javascript:debug(”‘+this.sender_name+’ : ‘+str+’”)’);
}
In some of the apps I have built using this you can also turn the debugger on and off in javascript this makes the whole thing quite complete and make the developing process a lot easier.
This only works in FireFox but you can always add a switch to get an alert in Explorer.
If you want something I but more sophisticated to your debugging you need to use Ash Atkins Debug Console class. I do use this but I find the first idea more useful generally.

Leave a Reply