我设计了以下方法来捕获整个AS3应用程序中的错误:
在Document类中,定义以下方法:
//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;
}
为了处理Document类中使用的类,我添加了以下内容,以便可以注册前面提到的函数:
protected var errorCatcher:Function;
protected var displayError:Function;
public function setErrorDisplayer(f:Function):void
{
displayError = f;
}
public function setErrorCatcher(f:Function):void
{
errorCatcher = f;
}
现在,当在浏览器中测试应用程序时,我可以在运行时在SWF中显示错误。
例如:
(我没有测试以下内容,只是一个示例)
//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;
}
}
}
这是矫kill过正,还是我错过了“最佳实践”?
最佳答案
您可以只使用FireBug在浏览器中调试和从SWF输出。只是Google的“firebug as3”,您就会看到大量的人正在这样做。
您也可以使用De MonsterDebugger之类的东西。它有很多很棒的功能。有关概述,请查看Lee Brimlows De MonsterDebugger video from GoToAndLearn。
关于flash - 在IDE之外显示捕获的错误-过度杀伤/批评?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3258226/