views:

37

answers:

4

I'm writing some code that (I think) requires sequentially playing MovieClip objects. I would like to be able to leave this at a single frame and using only external source files.

The way I wish to use this is to switch between sections of my program.

remove current child
construct movieclip
add new child
wait for completion of new child and goto next step

Is this posisble?

Edit: Other than the ENTER_FRAME method. Running a handler every frame seems wasteful when a signal surely could be sent when the clip reaches its final frame.

+1  A: 

Look for currentframe and totalframes MovieClip properties on livedocs, use them to check on an ENTER_FRAME event wether the active movieclip is still playing or has ended.

@user486493 Probably should have been more specific. I discovered this method between posting the question and reading the answer, but I'm not happy with it. Polling each frame seems stupid and inefficient. If there's not a better way, I'll accept this, but I'd like to see one if there is one.
Ben
+1  A: 

You could dispatch a complete event when a MovieClip reaches its last frame , but I'm not sure that's a better solution , because you would need to implement the event dispatching on each MovieClip you need to play.

It seems easier and more scalable to implement a method which would work with whatever MovieClip is loaded. In this example, you would have set an allMovies variable as an Array that would contain all your MovieClips and a movieIndex integer .

 function loadMovieClip(mc:MovieClip):void
 {
     mc.addEventListener(Event.ENTER_FRAME , enterFrameListener );
     addChild( mc);
 }

 function enterFrameListener(event:Event):void
 {
      var mc:MovieClip = event.currentTarget as MovieClip;

      if( mc.currentFrame == mc.totalFrames )
      {
           mc.removeEventListener(Event.ENTER_FRAME , enterFrameListener );
           removeChild(mc);

           ++movieIndex;

           if( movieIndex < allMovies.length )
             loadMovieClip(allMovies[movieIndex] );
      }
  }
PatrickS
@PatrickS This is indeed more along the lines of what I was chasing for a specific example. Sadly it seems polling is the best option so far.
Ben
Just read cmann answer, if the addFrameScript method can be called from a MovieClip instance , this may be the solution you're looking for as you could dispatch an event without having to hardcode the method in every single MovieClip you want to display.
PatrickS
@PatrickS His method seems more efficient and concise for the usage I need. For queueing movieclips yours would be good. I'm running a menu system, so his is ideal.
Ben
+4  A: 

You could use the addFrameScript method.

http://www.kirupa.com/forum/showpost.php?p=2098268&amp;postcount=318

MovieClip.addFrameScript(frame:int, method:Function, [frame:int, method:Function...]):void;

Something like this :

// This will be called on the last frame.
function myfunc():void{
    // Do stuff
}
// ** The frame number is zero-based.
myclip.addFrameScript(myclip.totalFrames-1, myfunc);
cmann
@cmann I think this is the most elegant solution to the problem so far. I'll probably use this or a variant of it. Interesting post too. Thanks.
Ben
Implemented this answer successfully so I'm accepting it.
Ben
@cmann Of note is this method requires clearing the framescript in the situation I was in, otherwise the method gets repeatedly called. To clear a frame script use `myclip.addFrameScript(myclip.totalFrames-1, null)`
Ben
A: 

or you can dispatch some event on the needed frame like this:

// In (frame 15 of 250):
super.dispatchEvent(new Event("CUSTOM_EVENT_TYPE", true));

//In as file:
_myClip.addEventListener("CUSTOM_EVENT_TYPE", onCustomListener); 
... 
private function onCustomListener(event : Event) : void {
    event.stopImmediatePropagation(); 
    //your staff 
}
dimpiax
I requested to keep this in external source files. Though this (presumably) works, it remains framescript.
Ben