Archives of September 2005

Rasterbator på svenska

Courtesy of Lennart Pettersson, I'm happy to announce the Swedish translation of The Rasterbator. Any speakers of Icelandic willing to complete the coverage for the Nordic countries?

Categories: Art, Creations, Programming
Posted by Matias at 21.09.2005 20.55 (9 years ago) | 0 comments

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) {
  s.PreviousFrame=s._currentframe;
  s.PreviousOnEnterFrame=s.onEnterFrame;
  for(m in s) if(s[m]._parent==s) SavePlayState(s[m]);
}

function DoFullStop(s) {
  s.Playing=s.PreviousFrame!=s._currentframe;
  if(s.Playing) {
    s.stop();
    s.onEnterFrame=null;
  }
  for(m in s) if(s[m]._parent==s) DoFullStop(s[m]);
}

function InitiateFullStop() {
  DoFullStop(_root);
  delete onEnterFrame;
}

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

function Freeze() {
  if(_global.FullStopState) return;
  SavePlayState(_root);
  onEnterFrame=InitiateFullStop;
  _global.FullStopState=true;
}

function Defreeze() {
  if(!_global.FullStopState) return;
  DoStart(_root);
  _global.FullStopState=false;
}

_global.FullStopState=false;
Categories: Programming, Web
Posted by Matias at 17.09.2005 22.50 (9 years ago) | 0 comments

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