在 IDE 之外显示捕获的错误 - 过度杀伤/批评?

发布于 2024-09-09 16:26:59 字数 2349 浏览 8 评论 0原文

我设计了以下方法来捕获整个 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;
    }
  }
}

这是矫枉过正还是我在这里错过了“最佳实践”?

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 runtime, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

电影里的梦 2024-09-16 16:27:00

您可以使用 FireBug 在浏览器中调试并输出 SWF。只要谷歌搜索“firebug as3”,你就会看到很多人都在这样做。

您还可以使用 De MonsterDebugger 之类的东西。它有很多很棒的功能。有关概述,请观看 来自 GoToAndLearn 的 Lee Brimlows De MonsterDebugger 视频

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文