I was curious to how gotoAndStop really functioned, so I ran a few tests. Results are highlighted for easy skim reading.
On each frame, I placed a trace statement, saying which frame is currently displaying.
Then, I messed around putting different kinds of code on frame 1.
Example #1
stop();
gotoAndStop(5);
trace("I'm still on frame 1, dude!");
Output
I'm still on frame 1, dude!
Frame 5, and feeling jive!
Now, logically, you should jump to frame 5 before you enter frame 1, but alas, Flash is not as logical.
[SIZE=4]
First, Flash finishes whatever code it needs on frame 1 [SIZE=2](or the frame that called the gotoAndStop()[/SIZE]
Then, Flash jumps to whatever frame you told it to go to[/SIZE]
This got me thinking… What would Flash do if you told it to go to a certain frame, but since it will finish running all functions for that frame, what if in the rest of that code, you told it to go elsewhere?
Example #2
stop();
gotoAndStop(5);
gotoAndStop(3);
trace("I'm still on frame 1, dude!");
Output
I'm still on frame 1, dude!
Frame 3, baby yeah!
[SIZE=4]*If Flash gets more than one “go to frame xx” on each frame, it only jumps to the last frame it was instructed to go to.
*[/SIZE]
If anyone has ever decompiled a SWF (I haven’t yet, but it is covered in a very interesting gotoAndLearn tutorial ), you will notice that all the Flash compiler does is add functions to specific frames using the not so commontly known “addFrameScript” function. For example, the above code I wrote would automatically compile to (comments added by me):
package HelloWorld_fla
{
import flash.display.*;
public class MainTimeline extends MovieClip
{
public function MainTimeline()
{
addFrameScript(0, frame1); //Remember that really begins numbering at 0, so frame 1 is 0 etc.
addFrameScript(1, frame2);
addFrameScript(2, frame3);
addFrameScript(3, frame4);
addFrameScript(4, frame5);
}
function frame1()
{
stop();
gotoAndStop(5);
gotoAndStop(3);
trace("I'm still on frame 1, dude!");
}
function frame2()
{
trace("Terrible frame 2...");
}
function frame3()
{
trace("Frame 3, baby yeah!");
}
function frame4()
{
trace("Have I been on this frame b4?");
}
function frame5()
{
trace("Frame 5, and feeling jive!");
}
}
I hope this research has helped someone out there understand the illogical logic of AS3.
Does anyone else have any comments or thoughts they have noticed regarding the frame system of Flash?