ActionScript Live Debug Class
I wanted to have a way of tracking object attributes in real time within the Flash movie. As a test case, I wanted to track the position of the mouse using a dynamic text box. Here is the result:
Edit: This has been updated to include stage dimensions and to update on stage resize.
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="30")]
public class DebugData extends Sprite {
// Instantiate stage variables
public var sw:Number = stage.stageWidth;
public var sh:Number = stage.stageHeight;
// Instantiate text field variables
public var txtFld_mouseX:TextField = new TextField();
public var txtFld_mouseY:TextField = new TextField();
public var txtFld_stageW:TextField = new TextField();
public var txtFld_stageH:TextField = new TextField();
// Instantiate text string variables
public var txtStr_mouseX:String;
public var txtStr_mouseY:String;
public var txtStr_stageW:String;
public var txtStr_stageH:String;
public function DebugData() {
// Turn scaling off and set alignment to top left
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// Create text fields
createTextField(txtFld_mouseX, "Mouse X: ");
createTextField(txtFld_mouseY, "Mouse Y: ", 20, 50);
createTextField(txtFld_stageW, "Stage Width: ", 20, 80);
createTextField(txtFld_stageH, "Stage Height: ", 20, 110);
// Add text fields to display list
addChild(txtFld_mouseX);
addChild(txtFld_mouseY);
addChild(txtFld_stageW);
addChild(txtFld_stageH);
// Add an event listener to update debug text
debug();
}
private function createTextField(txtFld:TextField, txtStr:String, txtFldX:int = 20, txtFldY:int = 20):void {
var txtFmt:TextFormat = new TextFormat();
// Format text
txtFmt.font = "Helvetica";
txtFmt.color = 0x333333;
txtFmt.size = 14;
txtFmt.leading = 6;
txtFmt.leftMargin = txtFmt.rightMargin = 4;
// Text field properties
txtFld.x = txtFldX
txtFld.y = txtFldY;
txtFld.width = 200;
txtFld.height = 20;
txtFld.border = txtFld.background = true;
txtFld.borderColor = 0xCCCCCC;
txtFld.background = true;
txtFld.backgroundColor = 0xEEEEEE;
txtFld.defaultTextFormat = txtFmt;
txtFld.text = txtStr;
}
private function debug():void {
var timer:Timer = new Timer(1000/30);
timer.addEventListener(TimerEvent.TIMER, traceOutput);
timer.start();
}
private function traceOutput(e:TimerEvent):void {
// Debug text
txtStr_mouseX = "Mouse X: " + mouseX;
txtStr_mouseY = "Mouse Y: " + mouseY;
txtStr_stageW = "Stage Width: " + stage.stageWidth;
txtStr_stageH = "Stage Height: " + stage.stageHeight;
// Update text fields with debug text
txtFld_mouseX.text = txtStr_mouseX;
txtFld_mouseY.text = txtStr_mouseY;
txtFld_stageW.text = txtStr_stageW;
txtFld_stageH.text = txtStr_stageH;
}
}
}
About this entry
You’re currently reading “ActionScript Live Debug Class,” an entry on Bauhouse
- Published:
- January 22, 2009 / 12:10 am
- Category:
- ActionScript
- Tags:
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]