Posted By

baconandgames on 03/10/12


Tagged

output trace as3


Versions (?)

Debug Window


 / Published in: ActionScript 3
 

URL: www.baconandgames.com

This is a very simple class that adds an output window to any DisplayObject. You can use this instead of trace() if you'd like to debug in the browser wihtout having to install the debut player and deal with FlashTracer or any of that nonsense.

While I recommend removing all usage of this when your project is complete, you COULD simply set the instance of your DebugWindow enabled to false. This will hide it from view, unregister from all events and stop writing text to the output window. Removing it from the stage will also unregister the listener events by default

-- USAGE TIPS --

SHIFT + SPACE - Toggle visibility of DebugWindow CTRL + UP - Scroll contents of DebugWindow up (CMD on Mac) CTRL + DOWN - Scroll contents of DebugWindow down (CMD on Mac) Double Click - Hide DebugWindow Click & Drag - Move DebugWindow

There are plenty of other minor features I considered adding, but ultimately I wanted to keep it simple and lightweight.

  1. package com.baconandgames.utils{
  2.  
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.events.KeyboardEvent;
  6. import flash.events.MouseEvent;
  7. import flash.text.TextField;
  8. import flash.text.TextFormat;
  9. import flash.ui.Keyboard;
  10.  
  11. public class DebugWindow extends Sprite{
  12.  
  13. private static const BUFFER:int = 4;
  14.  
  15. private var _tf:TextField;
  16. private var _fmt:TextFormat;
  17. private var _enabled:Boolean;
  18. private var _addedToStage:Boolean = false;
  19.  
  20. /**
  21. * @param w Number Width of window
  22. * @param h Number Height of window
  23. * @optional textColor Number Color of text
  24. * @optional textSize int Size of text
  25. * @optional bkgdColor Number Background color of window
  26. * @optional borderColor Number Color of border
  27. * */
  28.  
  29. public function DebugWindow(w:Number,h:Number,textColor:Number=0x000000,textSize:int=10,bkgdColor:Number=0xffffff,borderColor:Number=0xff0000)
  30. {
  31. super();
  32.  
  33. this.graphics.lineStyle(1,borderColor);
  34. this.graphics.beginFill(bkgdColor);
  35. this.graphics.drawRect(0,0,w,h);
  36. this.graphics.endFill();
  37. this.visible = false;
  38. this.doubleClickEnabled = true;
  39. this.enabled = true;
  40.  
  41. _tf = new TextField();
  42. _tf.multiline = true;
  43. _tf.wordWrap = true;
  44. _tf.mouseEnabled = false;
  45. _tf.x = DebugWindow.BUFFER;
  46. _tf.width = w - DebugWindow.BUFFER*2;
  47. _tf.height = h;
  48.  
  49. addChild(_tf);
  50.  
  51. _fmt = new TextFormat;
  52. _fmt.color = textColor;
  53. _fmt.size = textSize;
  54. _fmt.font = "Verdana";
  55. _tf.setTextFormat(_fmt);
  56.  
  57. registerListeners();
  58.  
  59. }
  60.  
  61. /**
  62. * @param b Boolean
  63. * When set to true, listeners are enabled and text can be written to the output window.
  64. * When false, listeners are removed and text will NOT be written to the output window.
  65. **/
  66.  
  67. public function set enabled(b:Boolean):void{
  68. _enabled = b;
  69. if(b){
  70. this.visible = true;
  71. moveToTop();
  72. registerListeners();
  73. }else{
  74. this.visible = false;
  75. unregisterListeners();
  76. }
  77. }
  78.  
  79. /**
  80. * @return Boolean returns true if enabled
  81. * */
  82.  
  83. public function get enabled():Boolean{
  84. return _enabled;
  85. }
  86.  
  87. /**
  88. * Boolean returns true if enabled
  89. * @param val String or number you would like to output, each call will put text on new line
  90. *
  91. * */
  92.  
  93. public function output(val):void{
  94. if(this.enabled){
  95. _tf.appendText("\n"+val);
  96. _tf.scrollV = _tf.numLines;
  97. _tf.setTextFormat(_fmt);
  98. }
  99. }
  100.  
  101. /**
  102. * Works the same as output, but clears the output window before outputting content
  103. * @param val String or number you would like to output
  104. *
  105. * */
  106.  
  107. public function clearAndOutput(t):void{
  108. clear();
  109. output(t);
  110. }
  111.  
  112. /**
  113. * Clears the output window
  114. *
  115. * */
  116.  
  117. public function clear():void{
  118. output("");
  119. }
  120.  
  121. private function moveToTop():void{
  122. if(this._addedToStage) this.parent.setChildIndex(this,this.parent.numChildren-1);
  123. }
  124.  
  125. private function onAddedToStage(e:Event):void{
  126. _addedToStage = true;
  127. registerListeners();
  128. moveToTop();
  129. }
  130.  
  131. private function onRemovedFromStage(e:Event):void{
  132. unregisterListeners();
  133. }
  134.  
  135. private function handleMouse(e:MouseEvent):void{
  136. if(e.type == MouseEvent.MOUSE_DOWN){
  137. this.startDrag(false);
  138. }else if(e.type == MouseEvent.MOUSE_UP){
  139. this.stopDrag();
  140. }else if(e.type == MouseEvent.DOUBLE_CLICK){
  141. this.visible = false;
  142. }
  143. }
  144.  
  145. private function onKey(e:KeyboardEvent):void{
  146. if(e.shiftKey && e.keyCode == Keyboard.SPACE){
  147. this.visible = !this.visible;
  148. moveToTop();
  149. }else if(e.ctrlKey && e.keyCode == Keyboard.UP){
  150. if(this.visible) _tf.scrollV -= 1;
  151. }else if(e.ctrlKey && e.keyCode == Keyboard.DOWN){
  152. if(this.visible) _tf.scrollV += 1;
  153. }
  154. }
  155.  
  156. private function registerListeners():void{
  157. if(!this.hasEventListener(Event.ADDED_TO_STAGE)) addEventListener(Event.ADDED_TO_STAGE,onAddedToStage,false,0,true);
  158. if(!this.hasEventListener(Event.REMOVED_FROM_STAGE)) addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage,false,0,true);
  159. if(!this.hasEventListener(KeyboardEvent.KEY_DOWN) && this._addedToStage) this.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey,false,0,true);
  160. if(!this.hasEventListener(MouseEvent.MOUSE_UP)) addEventListener(MouseEvent.MOUSE_UP,handleMouse,false,0,true);
  161. if(!this.hasEventListener(MouseEvent.MOUSE_DOWN)) addEventListener(MouseEvent.MOUSE_DOWN,handleMouse,false,0,true);
  162. if(!this.hasEventListener(MouseEvent.DOUBLE_CLICK)) addEventListener(MouseEvent.DOUBLE_CLICK,handleMouse,false,0,true);
  163. }
  164.  
  165. private function unregisterListeners():void{
  166. if(this.hasEventListener(Event.ADDED_TO_STAGE)) removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
  167. if(this.hasEventListener(Event.REMOVED_FROM_STAGE)) removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);
  168. if(this.stage.hasEventListener(KeyboardEvent.KEY_DOWN)) this.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKey);
  169. if(this.hasEventListener(MouseEvent.MOUSE_UP)) removeEventListener(MouseEvent.MOUSE_UP,handleMouse);
  170. if(this.hasEventListener(MouseEvent.MOUSE_DOWN)) removeEventListener(MouseEvent.MOUSE_DOWN,handleMouse);
  171. if(this.hasEventListener(MouseEvent.DOUBLE_CLICK)) removeEventListener(MouseEvent.DOUBLE_CLICK,handleMouse);
  172. }
  173. }
  174. }

Report this snippet  

You need to login to post a comment.