For this code bits article, I’m going to be explaining how to control the timeline. Included code bits will be stop(); play(), and gotoAndPlay();. While there are other ways to control the timeline, I find these to be the most useful. Now to explain each one.

The stop(); command will stop the timeline on a specific frame. All you have to do is place stop(); into the frame’s code area, and the timeline will stop on that frame. This code is useful for stopping at a play screen or a menu screen, and can be used for many more purposes as well.

Now for the play(); command. This command is fairly useful and it is important to know what it does. The play(); command will play the timeline. It is essentially the opposite of the stop(); command. It could be called when you click a button or complete a specific goal.

I find the gotoAndPlay(); and gotoAndStop(); commands to be the most useful. The first command will go to a specific frame and play, while the second one will go to a specific frame and stop. They are useful for games where there is more than just a couple frame, and it ensures that even if you move frames around, you will still go to the same frame. These are a little bit harder than the other two, so I’m going to give you an example of each of the in use.

gotoAndPlay('frameName');//go to the frame called frameName and play.
gotoAndStop(7);//go to frame 7 and stop.

As you can see, the second time I used a number instead of a frame label. Either way works, but I prefer to label each frame, that way I can move frames around without worrying about changing the code.

Related posts:

  1. Code Bits: if statements
  2. Code Bits: Variables
  3. Code Bits: Set Interval