Domanda

Ho ideato il seguente metodo per catturare errori durante le mie applicazioni AS3:

Nella classe di documenti, definire i seguenti metodi:

//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;
}
.

Per affrontare le classi che vengono utilizzate all'interno della classe di documenti, aggiungo quanto segue, in modo che possa registrare le funzioni menzionate in precedenza:

protected var errorCatcher:Function;
protected var displayError:Function;

public function setErrorDisplayer(f:Function):void
{
  displayError = f;
}

public function setErrorCatcher(f:Function):void
{
  errorCatcher = f;
}
.

Ora, posso visualizzare errori nel SWF in runtime, quando si verifica l'applicazione nel browser.

Ad esempio: (Non ho testato quanto segue è solo un esempio)

//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;
    }
  }
}
.

è questo overkill o mi manca una "migliore pratica" qui?

È stato utile?

Soluzione

È possibile utilizzare Firebug in debug e uscita da un SWF nel browser.Just Google per "Firebug AS3", e vedrai un sacco di persone che stanno facendo questo.

Puoi anche usare qualcosa come de monsterdebuger .Ha un sacco di grandi caratteristiche.Per una panoramica, check out Lee Brimlows de monsterdebugger video da gotoandlearn .

.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top