Stopping a Flash movie completely

As far as I know, there is no direct way to easily stop everything that is going on in a Flash movie, that is, stopping every movieclip. It is a complex task, since the movieclips can be playing or be still or there may be ActionScript events that go on and cause things to move. Even more difficult is to resume the action where it was left so that only those movieclips that were playing will continue and ActionScript events will continue going on.

How to use

Here is some program code that will perform those tasks. However, it is not completely fool proof. It does take into account the following:

  • Play and stop states of different movieclips
  • OnEnterFrame events

But it does not work correctly with the following:

  • Events that are assigned in the first and only frame of a movieclip (they are re-assigned at every frame, this can be resolved by adding a second frame)
  • Timers

The code is easy to use, just copy and paste the code below to the first frame of the movie root and invoke Freeze(); or Defreeze(); when needed. Of course, adding new method to the MovieClip prototype would be nicer, but, alas, this was written for the need to stop everything in the movie.

How does it work

Since there is no easy way to determine whether a movieclip is playing or not, the _currentframe property of every movieclip is saved. Then, on the next onEnterFrame event the _currentframe property is determined to the saved result and if these differ, the movieclip is presumed to be playing. The play state is saved and only those which were playing are defrozen. This means the freeze does not happen immediately but only at the next frame.

The onEnterFrame events, if any, are saved to a temporary variable at freeze. Defreezing reassigns them.

There are recursive functions and infinite loops are bound to occur if there are references from the movieclips to others. These are avoided by checking whether a nested movieclip's _parent is the nestee.

The Code

function SavePlayState(s) {
  for(m in s) if(s[m]._parent==s) SavePlayState(s[m]);

function DoFullStop(s) {
  if(s.Playing) {
  for(m in s) if(s[m]._parent==s) DoFullStop(s[m]);

function InitiateFullStop() {
  delete onEnterFrame;

function DoStart(s) {
  if(s.Playing) {;
  for(m in s) if(s[m]._parent==s) DoStart(s[m]);

function Freeze() {
  if(_global.FullStopState) return;

function Defreeze() {
  if(!_global.FullStopState) return;

Categories: Programming, Web
Posted by Matias at 17.09.2005 22.50 (12 years ago) | 603 comments


Post a new comment

Will be displayed within an image file, hopefully undecipherable to address harvesters.

content licensed under Creative Commons BY-NC-SA - Valid HTML 5