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?
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
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!');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With