Search code examples
actionscript-3debuggingtrace

AS3 - Catch ANY Error thrown and keep track of them?


I would like to have many devices testing a game, and I find the best way to debug a game and solve specific code problems is to have the device connected and in debug mode in Adobe ANIMATE, that way I can catch any Errors in the Output window.

For Example, if I am debugging and connected to Animate, the output window will throw errors like :

ReferenceError: Error #1065: Variable bg_storage is not defined. at global/flash.utils::getDefinitionByName() at Game/stageAdd()[/Users/**/Game.as:360]

Now I know exactly what the problem is and where to find it. I love errors like this.

My question : If I didn't have a device connected to Animate in Debugging mode, is there a way to make the game detect any errors thrown and store them as a String, that way I can put up a big text block on the game of the error string and keep track.

or at least a way to log them some how?

Ex : If an error is thrown, have that error text set as a String variable, then have a text box write out that String variable.

I hope that isn't too confusing. If I am going about debugging in a poor way, I would love to know what you guys do to keep track of errors without being connected to debug mode.

EDIT I can see an approach is you to add an uncaughtErrorEvent event to each function to be able to catch these errors...

loadbar.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR ... )

I am trying to make it so any error thrown in any part of the game will trace that error somewhere to a String value that I can call, so that I can see any error thrown during a play test session without being connected to debug mode.

Thanks!


Solution

  • Sure. There's a class intended exactly for that: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/UncaughtErrorEvent.html See examples at the bottom of the page to listen to the right instances for that event.

    You are also free go grab my own class that does the thing you want: https://bitbucket.org/thydmitry/ru.delimiter/src/2756fadd741a6d44276fde1701470daf24cebfa8/classes/ru/delimiter/utils/Log.as?at=default&fileviewer=file-view-default

    You will need to add it to your project and then call in the main document class (in constructor, preferably):

    Log.create(this);
    Log.handleExceptions(this, true);