views:

25

answers:

1

In Flash Actionscript 3, if a function is defined in Frame 1 of a flash animation and the animation loops, does it store another version of the function in memory upon each loop.

frame 1:

function blah() {
  var n = "yes";
  return n;
}

If so, is it a best practice to check to see if it's the first time the frame has been run, by setting a variable and checking it's existence, or is it a non-issue because the compiler checks to make sure that a function has not already been defined?

frame 1:

if (!status) {
  function blah() {
    var n = "yes";
    return n;
  }

  var status = "loaded";
}
+1  A: 

There is a fair chance that the flash player creates a "wrapper object" for each keyframe, and that when you play a certain keyframe, it just accesses that "instance" of the keyframe. I'm pretty sure the player doesn't store a new instance of that function in memory, it would be a waist of space, and thet fact you can call a function declared in frame one from frame 2 for example, shows that the functions from a frame are probably persistant. Plus, if you consider the fact you can't modify the code from a frame from another frame, when the timeline re-enters that frame, the content would be the same. From a little test I did, it seems as if the player just executes a frame's code everytime it enters that frame, and all the code on the frame is global to the current timeline. I didn't test much so I might be wrong, but I'm pretty sure you can remove the status variable, because it won't work since the status var would be reset to false when the frame script runs.

Hope this helps.

__dominic
thats basically right. Flash will compile the timeline to a class, complete with all the functions you have on the timeline.
Allan