Skip to main content
Participating Frequently
February 3, 2011
Answered

Targeting the main timeline from within a MC

  • February 3, 2011
  • 1 reply
  • 1927 views

Hello all, I have this project that will basically work like a slideshow. I am trying to do things the right way and avoid scenes so I am building each slide as a separate mc and then placing them on the main timeline. What I'm trying to do is get the playback head to advance to the next frame on the main timeline once the mc reaches its last frame. I have found code that works but it seems pretty ghetto:

var _root:MovieClip = MovieClip(root)

stage.addEventListener(Event.ENTER_FRAME, text2);
function text2 (myevent:Event):void {
_root.gotoAndStop("slide2");
}

I guess I have two basic questions:

1. What is the proper way to target a frame on the main timeline from within a mc in as3?

2. Does the way I'm building this make sense or is there a better solution?

Thanks for reading!

This topic has been closed for replies.
Correct answer Ned Murphy

That code is an accident waiting to happen.  An ENTER_FRAME event fires repeatedly, at the frame rate of the file.  It does not mean what it sounds like...such as..."when something enters a frame"  All you would need in place of that code is to put the following line in the last frame of the mc...

MovieClip(root).gotoAndStop("slide2");

Since you asked about "proper"... children should not talk back to their parent and tell them what to do.  The good news is they should be independent and only worry about doing what they do. It's up to the parent to keep an eye on the children if they are so inclined.  The only thing the child should do is let the world know when they are done doing it.  This is done by dispatching an event.

So what you do is set up the child to dispatch an event to indicate they are done doing what they do (in the last frame in your case).

     dispatchEvent(new Event("imDone"));

and set up a listener for that child object in the parent timeline for the parent to listen for that event...

     childName.addEventListener("imDone", eventHandler);

     function eventHandler(event:Event):void {
         gotoAndStop("slide2");
     }

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 3, 2011

That code is an accident waiting to happen.  An ENTER_FRAME event fires repeatedly, at the frame rate of the file.  It does not mean what it sounds like...such as..."when something enters a frame"  All you would need in place of that code is to put the following line in the last frame of the mc...

MovieClip(root).gotoAndStop("slide2");

Since you asked about "proper"... children should not talk back to their parent and tell them what to do.  The good news is they should be independent and only worry about doing what they do. It's up to the parent to keep an eye on the children if they are so inclined.  The only thing the child should do is let the world know when they are done doing it.  This is done by dispatching an event.

So what you do is set up the child to dispatch an event to indicate they are done doing what they do (in the last frame in your case).

     dispatchEvent(new Event("imDone"));

and set up a listener for that child object in the parent timeline for the parent to listen for that event...

     childName.addEventListener("imDone", eventHandler);

     function eventHandler(event:Event):void {
         gotoAndStop("slide2");
     }

tim1124Author
Participating Frequently
February 3, 2011

Ned, you're a genious!

I learned more about flash from your post than I did in four hours digging through forums. I owe ya a steak dinner.

Ned Murphy
Legend
February 3, 2011

Medium rare works well for me.