August 11th, 2007
Silent error using Return statment in ActionScript
A friend of mine was having problem with some ActionScript code on a frame and couldn't figure out why part of it was working and then it just stopped working. I took a quick look at this code and immediately saw a "return" statement where it shoudn't be. It was outside of a function and probably something that got copied-n-pasted from somewhere else.
So here is a puzzler, Flash didn't throw an error about this return being outside of a function and I have no idea where the flow of the code would return to. I did a quick test of it in a AS3 file and got the same behavior.
Is this something Flash should be throwing an error on? If not, why?
Here is a quick code snippet:
Actionscript:
-
trace ("fe fi fo");
-
return;
-
trace("fum");
-
// traces fe fi fo, but no fum

4 Responses to “Silent error using Return statment in ActionScript”
In AS3, if the return type is void, that is valid. If it is not void, you’ll get an error in AS3. If you are using this in the timeline in Flash CS3 in AS3, no idea. In AS2, the compiler isn’t that smart.
By JesterXL August 11th, 2007 at 7:41 pm
Yeah, but this return wasn’t in a function. It was “out in the wild” just sitting there on the timeline. I’m sure there wouldn’t be a way to do this in a Class file, but if this can happen on the main timeline and not throw an error in AS2 or AS3, then its a bit odd. And in the case of my buddy, it was causing errors without throwing an error. I would still like to know where Flash was trying to return to, since the return wasn’t called by any function.
By John O August 11th, 2007 at 8:16 pm
There is no “code on the timeline” in Flash when you compile as AS3 anymore. What happens in a nutshell is that it’s all wrapped in code generated AS3 classes. Some code is wrapped into functions that have a return type of void.
Imagine if you put code on frame 1, and then the compiler wrapped that code in a function, and then did an addFrameScript(1, gennedFunction).
By JesterXL August 12th, 2007 at 8:59 am
Thanks Jesse. That makes complete sense.
By John O August 12th, 2007 at 10:45 am