Skip to main content
Participant
March 19, 2014
Question

Buttons won't work when entering frame second time

  • March 19, 2014
  • 1 reply
  • 2118 views

I've set up three buttons in the first frame which are supposed to switch frames. When the program first runs, there are no errors, and i can click any of the buttons and end up where I want. However, when going back to the first frame, the buttons no longer work. The stage listener still works though. I added the "Clicked" output to check if the function was called, which it wasn't. I know I disable the buttons in the code, but only after that button is clicked, and the task is done. I should mention I don't have the code on the timeline, but on a separate document. Here is my code:

<code>

package

{

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    import flash.events.Event;

    import flash.display.SimpleButton;

  

   public class DocumentClass extends MovieClip

    {

        public var matteOppgave:Boolean = false;

                      public var engelskOppgave:Boolean = false;

                    public var flaggOppgave:Boolean = false;

 

                    public function DocumentClass()

        {

                              stop();

 

 

                              btnBok.addEventListener(MouseEvent.CLICK, matte);

                              btnFlagg.addEventListener(MouseEvent.CLICK, flagg);

                              btnPc.addEventListener(MouseEvent.CLICK, engelsk);

                              stage.addEventListener(Event.ENTER_FRAME, sjekk);

 

 

                              btnBok.enabled = true;

                              btnPc.enabled = true;

                              btnFlagg.enabled = true;

 

                              function matte(evt:MouseEvent)

                              {

                                        gotoAndStop(2);

                                        frame2();

                                        trace("Clicked");

                              }

                              function engelsk(evt:MouseEvent)

                              {

                                        gotoAndStop(3);

                                        frame3();

                                        trace("Clicked");

                              }

                              function flagg(evt:MouseEvent)

                              {

                                        gotoAndStop(4);

                                        frame4();

                                        trace("Clicked");

                              }

                              function sjekk(evt:Event)

                              {

                                        if(matteOppgave == true && engelskOppgave == true && flaggOppgave == true)

                                        {

                                                  gotoAndStop(5);

                                        }

 

                                        if(matteOppgave == true)

                                        {

                                                  btnBok.alpha = 0.5;

                                                  btnBok.enabled = false;

                                                  låsEin.alpha = 0;

                                        }

                                        if(engelskOppgave == true)

                                        {

                                                  btnPc.alpha = 0.5;

                                                  btnPc.enabled = false;

                                                  låsTo.alpha = 0;

                                        }

                                        if(flaggOppgave == true)

                                        {

                                                  btnFlagg.alpha = 0.5;

                                                  btnFlagg.enabled = false;

                                                  låsTre.alpha = 0;

                                        }

                              }

            

        }

</code>

This topic has been closed for replies.

1 reply

Amy Blankenship
Legend
March 19, 2014

You constructor runs once, when the stage is drawn the first time on frame 1. When you navigate away from frame 1, if those instances are not on stage at the destination frame (and continuously in between, the original instances will be thrown away. When you navigate back to frame 1, new instances are constructed and placed into the variables. Hence, the listeners are listening to objects that no longer exist (technically those objects hold a reference to the callback function and those objects have probably been garbage collected, so in reality there's no listening going on).

Check out my presentation "Solving the frame 2 problem."

JonOlnAuthor
Participant
March 19, 2014

Thanks for answering!

I didn't get too much out of the presentation since I'm pretty much a beginner in coding. I think I understand the problem, but how do I fix it?

Amy Blankenship
Legend
March 19, 2014

You have several choices.

  1. Do everything through code, including creating instances, positioning them, and adding them to the stage. I find this tedious, but it is how most developers who think of themselves more as coders than designers work.
  2. Use timeline scripts, eschewing Classes. This is how most people who consider themselves designers work.
  3. Learn to think holistically about how the timeline and the code coexist. I used to have an article on an O'Reilley site about this that goes into more depth than the preso, but it seems O'Reilley no longer finds it worthwhile to host content that's primarily about working in the Flash Platform. To summarize, you can
    1. use getters and setters instead of letting Flash declare your stage instances for you, and when the setter triggers that's when you should add your listener or
    2. listen to the stage in capture phase and grab instances you care about when they're added, then do things like adding listeners to them there.

There are plenty of tutorials out there that tell you how to do 1 and 2. To the best of my knowledge, the only person who can and will help you with #3 is me. So you're better off probably picking one of the others if you want to have access to a huge body of material by different people on the approach you want to follow.