Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line number constant in Actionscript 3.0?

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?

like image 653
Gordon Potter Avatar asked Jul 28 '09 00:07

Gordon Potter


2 Answers

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

like image 120
OXMO456 Avatar answered Nov 15 '22 05:11

OXMO456


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!');
like image 34
jwfearn Avatar answered Nov 15 '22 04:11

jwfearn