I have devised the following method for catching errors throughout my AS3 applications:
In the Document class, define the following methods:
//This is the handler for listening for errors
protected function catchError(event:ErrorEvent):void
{
displayError('Error caught: ' + event.text);
}
//Creates a MovieClip with a TextField as the child.
//Adds the MC to the stage
protected function displayError(msg:String):void
{
var errorMC:MovieClip = new MovieClip();
errorMC.graphics.beginFill(0xffffff);
errorMC.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
errorMC.graphics.endFill();
var errorTxt:TextField = new TextField();
errorTxt.multiline = true;
errorTxt.width = stage.width;
errorTxt.height = stage.height;
errorTxt.selectable = true;
addChild(errorMC);
addChild(errorTxt);
errorTxt.text = 'Error(s) Caught: \n' + msg;
}
To deal with classes that are used within the Document class I add the following so that I can register the previously mentioned functions:
protected var errorCatcher:Function;
protected var displayError:Function;
public function setErrorDisplayer(f:Function):void
{
displayError = f;
}
public function setErrorCatcher(f:Function):void
{
errorCatcher = f;
}
Now, I can display errors in the SWF at r开发者_开发技巧untime, when testing the application in the browser.
For example: (I didn't test the following it's just an example)
//Document class
package com
{
import flash.display.MovieClip;
import flash.event.ErrorEvent;
import flash.text.TextField;
import com.SomeClass;
public class Document extends MovieClip
{
protected var someClass:SomeClass = new SomeClass();
public function Document():void
{
someClass.setErrorCatcher(catchError);
someClass.setErrorDisplayer(displayError);
}
protected function catchError(event:ErrorEvent):void
{
displayError('Error caught: ' + event.text);
}
protected function displayError(msg:String):void
{
var errorMC:MovieClip = new MovieClip();
errorMC.graphics.beginFill(0xffffff);
errorMC.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
errorMC.graphics.endFill();
var errorTxt:TextField = new TextField();
errorTxt.multiline = true;
errorTxt.width = stage.width;
errorTxt.height = stage.height;
errorTxt.selectable = true;
addChild(errorMC);
addChild(errorTxt);
errorTxt.text = 'Error(s) Caught: \n' + msg;
}
}
}
Is this overkill or am I missing a "best practice" here?
You can just use FireBug to Debug and output from a SWF in the browser. Just Google for "firebug as3", and you will see a ton of people are doing this.
You can also use something like De MonsterDebugger. It has a lot of great features. For an overview, check out Lee Brimlows De MonsterDebugger video from GoToAndLearn.
精彩评论