Posted on June 25, 2010 by Grant SkinnerLately I’ve been messing around with Flash on devices and in other “new frontiers”. It’s fun, but sometimes getting debugging working is hit or miss. To address this, I wrote an ultra simple class for tracing to a text field. It’s nothing fancy, but that’s the point – it’s simple (~15 lines of code), tiny (~0.5kb), handy (at least for me), and works well, so I thought I’d share. To use it, simply drop Out.as into your class directory. Set an output text field when your app starts up: Out.textField = outFld;You can also specify a maximum number of characters to retain, so that old traces will be deleted as new ones are added. Out.maxLength = 1000; From anywhere in your application you can call Out.trace(value). This will trace the value normally, append it to the end of your text field, and scroll the text field to the end so that the latest trace is always displayed. Out.trace("hello"); Out.trace("count: ",count); // supports multiple params Finally, if you want to do something more custom, you can override Out.handler to use your own function. function log(...rest:Array):Boolean { trace("logging: "+rest.join(",")); // ... etc. // only write to text field if first param (ex. priority) is > 1: return rest[0] > 1; // returning false prevents it being written to Out.textField. } Out.handler = log; You can download the Out class here. It is licensed under the MIT license.
Follow @gskinner on Twitter for more news and views on interactive media.
|
|
|
8 Comments
Nice class, i can see its potential for growth and extension. for less typing, i like Out.put("msg") instead of Out.trace("msg"), what do you think?
Posted by: ross sclafani on Jun 25, 2010 11:37am URL: http://ross.sclafani.net
talking about small AND useful, even copyright comment has more lines of code :D
Posted by: john on Jun 25, 2010 5:55pm URL: http://flash2nd.com
Nice, what I like about the way you designed this is that it links to an existing TextField you create however you want, rather than forcing on you some pre-built output display; and the custom handler goes well with this, too.
Posted by: Aaron on Jun 26, 2010 3:52pm URL: http://abeall.com
Hi grant.
I'm Korean developer.
How do you think global package method?
package
{
public function output( ...$rest: Array ): void
{
// do what you do
}
}
Like above.
If you use this, you dont need to import anything and you can call output at every scopes.
And code is shotter.
I using global method called by "echo"(you may guess what I used to)
Since I use global method, I dont feel need to anymore.
Posted by: Wooyaggo on Jun 29, 2010 6:55pm URL: http://as3.kr
Hi Can someone explain this to me:
public static function trace(...rest:Array):void {
"...rest" what does that signify, why the ...
Posted by: steve on Jun 30, 2010 6:55pm
@steve: it has been introduced in AS3, this is the help reference page that explains it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#..._%28rest%29_parameter
Posted by: Carlo on Jul 1, 2010 4:56am
It is well explained, short but concise. Gives me nice insights.
Posted by: Jeorge Peter on Aug 18, 2010 7:48pm URL: http://www.wickedinnovations.com
This is a very nice class. Thanks!
A nice enhancement anyone can throw on is to toggle the textfield's visibility with an obscure combination of key presses like o+u+t. might not be so cool on mobile though.
Posted by: carl on Aug 24, 2010 10:06pm URL: http://www.snorkl.tv