5

Is there a line number constant or way to dynamically trace the line number in actionscript?

Does actionscript have the equivalent of

__LINE__

in PHP?

2 Answers 2

6

This is not a CONSTANT but this line of code will give you the line number:

trace(">",new Error().getStackTrace().match(/(?<=:)[0-9]*(?=])/g)[0]);

PS: this will only work if the swf is compiled in debug mode

2
  • Interesting approach. This has got me thinking about the problem in a new way. Thanks. What other useful information do you often pull out of the stack trace? Do you ever use this technique to create conditional breakpoints in the code? For example adding a break point when a very specific parameter has been passed to a function? I suppose in that case one could just put in conditional test for the parameter and wrap a break point inside it. Jul 28, 2009 at 18:58
  • hi ! i never use this trick... and i am not sure we can add dynamicly breakpoints in as3... but in order to debug your application, you can still throw an exception... hope that will help you... sorry i am french, my english is very poor... : (
    – OXMO456
    Jul 31, 2009 at 17:40
3

To use OXMO456's trick as a function, just use index 1 of the match result (rather than index 0). The code below does this and checks for debug capability:

import flash.system.Capabilities;

/**
 * Returns the positive line number from which the function is called, if
 * available, otherwise returns a negative number.
 */
function lineNumber():int {
  var ret:int = -1;
  if (Capabilities.isDebugger) {
    ret = new Error().getStackTrace().match(/(?<=:)[0-9]*(?=])/g)[1];
  }
  return ret;
}

Example:

trace('line ' + lineNumber() + ' reached!');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.