10 Replies Latest reply: Feb 5, 2010 8:17 AM by Webshark2000 RSS

    How do you run loop functions on specific frames of nested Movieclips?

    Webshark2000 Community Member

      I'm still pretty new to AS3 and I've run into a problem that I never had with AS2.  I have a project where the 1st frame of the main timeline is the loading screen and the second frame has buttons and a title bar that I want to be on every "page" of my project.  There are 7 buttons that take the user between 7 different "pages", which consist of 7 frames of a movieclip I have filling the content area on frame 2 of my main timeline.  I'll call this movieclip "pages_mc".

       

      The problem is that frame 6 of the pages_mc movieclip has the following code:

       

      var moving = 0;
      var xmoved;
      var xbegin = pano_cont.pano_image.x;


      parts_mc.addEventListener(Event.ENTER_FRAME, moveImage); // constantly moves the image 1 pixel left or right depending on the button pressed

      btn_left.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft); //changes the variable "moving" to 2 on mouse down
      btn_right.addEventListener(MouseEvent.MOUSE_DOWN, moveRight); //changes the variable "moving" to 1 on mouse down
      btn_left.addEventListener(MouseEvent.MOUSE_UP, stopMoveLeft); //changes the variable "moving" to 0 on mouse up
      btn_right.addEventListener(MouseEvent.MOUSE_UP, stopMoveRight); //changes the variable "moving" to 0 on mouse up


      function moveImage(e.Event):void{

      xmoved = xbegin - pano_cont.pano_image.x;
          if(moving == 1)
          {
              pano_cont.pano_image.x -= 1;
          }
          if(moving == 2)
          {
              pano_cont.pano_image.x += 1;
          }
          if(xmoved < -(pano_cont.pano_image.width/2)+180)
          {
              pano_cont.pano_image.x = -2538;
          }
          if(xmoved > (pano_cont.pano_image.width/2)- 180)
          {
              pano_cont.pano_image.x = -2178;
          }

      }

       

      function moveRight (e:MouseEvent):void
      {
          btn_right.gotoAndStop("down");
          moving = 1;
      }
      function moveLeft (e:MouseEvent):void
      {
          btn_left.gotoAndStop("down");
          moving = 2;
      }
      function stopMoveLeft (e:MouseEvent):void
      {
          moving = 0;
          btn_left.gotoAndStop("active");
      }
      function stopMoveRight (e:MouseEvent):void
      {
          moving = 0;
          btn_right.gotoAndStop("active");
      }

       

      This is all just some code to allow users to move a panaromic image around on the screen.  But when I navigate to a different page from of my pages_mc movieclip I get the following output message:

       

      TypeError: Error #1099: Cannot access a property or method of a null object reference.

           at SampleProject_fla::Content_MC_1/moveImage()

       

      I'm thinking this is happening because the "parts_mc" movieclip I referenced for the ENTER_FRAME event listener no longer exists (unless you go back to frame 6 of pages_mc).  I'm not sure how to remove this event listener when the user moves to another frame of pages_mc.

       

      Any help would be greatly appreciated.

        • 1. Re: How do you run loop functions on specific frames of nested Movieclips?
          kglad CommunityMVP

          use:

           

          parts_mc.removeEventListener(Event.ENTER_FRAME, moveImage);

           

          when navigating away from that frame.   you can type parts_mc on frame 1 of your swf and then check if it's not null before trying to reference it and remove its listener.

          • 2. Re: How do you run loop functions on specific frames of nested Movieclips?
            Webshark2000 Community Member

            kglad wrote:

            you can type parts_mc on frame 1 of your swf and then check if it's not null before trying to reference it and remove its listener.

            What script would I use to check if it is null or not? I'm not familar with that syntax.

            • 3. Re: How do you run loop functions on specific frames of nested Movieclips?
              kglad CommunityMVP

              if(parts_mc!=null){

              parts_mc.removeEventListener(Event.ENTER_FRAME, moveImage);

              }

              • 4. Re: How do you run loop functions on specific frames of nested Movieclips?
                Webshark2000 Community Member

                Sorry to be so ignorant about this, but when I try placing that code on frame 2 of the main timeline I get the following error:

                 

                1120: Access of undefined property moveImage.

                 

                which I know is because the moveImage function is on frame 6 of the pages_mc movieclip.  Should I be putting the null check code in the pages_mc movieclip or do I need to write the moveImage function so that it's all on the actions layer of frame 2 in the main timeline? And I wasn't completely clear as to what you meant when you said I should type parts_mc in frame 1 of the swf.

                • 5. Re: How do you run loop functions on specific frames of nested Movieclips?
                  kglad CommunityMVP

                  that conditional shouldn't resolve to true until moveImage is defined.  that's part of the reason you're checking if that movieclip is null.

                   

                  make sure you followed by instructions declaring that movieclip but not instantiating it:

                   

                  var parts_mc:MovieClip;  // should be in frame 1, NOT:

                   

                  var parts_mc:MovieClip=new MovieClip();  // or anything other than the above line.

                  • 6. Re: How do you run loop functions on specific frames of nested Movieclips?
                    Webshark2000 Community Member

                    kglad wrote:

                     

                    that conditional shouldn't resolve to true until moveImage is defined.  that's part of the reason you're checking if that movieclip is null.

                     

                    make sure you followed by instructions declaring that movieclip but not instantiating it:

                     

                    var parts_mc:MovieClip;  // should be in frame 1, NOT:

                     

                    var parts_mc:MovieClip=new MovieClip();  // or anything other than the above line.

                    I'm not sure what I'm doing wrong, but I'm still getting the error:

                     

                    1120: Access of undefined property moveImage.

                     

                    Here is the script I have on Frame 1 of the main timeline:

                     

                    var parts_mc:MovieClip;

                     

                    This is frame 2 of the main timeline:

                     

                    stop();

                    if(parts_mc!=null){

                    parts_mc.removeEventListener(Event.ENTER_FRAME, moveImage);

                    }

                     

                    and this is on frame 6 of the pages_mc movieclip:

                     

                    stop();

                     

                    var moving = 0;
                    var xmoved;
                    var xbegin = pano_cont.pano_image.x;


                    parts_mc.addEventListener(Event.ENTER_FRAME, moveImage); // constantly moves the image 1 pixel left or right depending on the button pressed

                    btn_left.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft); //changes the variable "moving" to 2 on mouse down
                    btn_right.addEventListener(MouseEvent.MOUSE_DOWN, moveRight); //changes the variable "moving" to 1 on mouse down
                    btn_left.addEventListener(MouseEvent.MOUSE_UP, stopMoveLeft); //changes the variable "moving" to 0 on mouse up
                    btn_right.addEventListener(MouseEvent.MOUSE_UP, stopMoveRight); //changes the variable "moving" to 0 on mouse up


                    function moveImage(e.Event):void{

                    xmoved = xbegin - pano_cont.pano_image.x;
                        if(moving == 1)
                        {
                            pano_cont.pano_image.x -= 1;
                        }
                        if(moving == 2)
                        {
                            pano_cont.pano_image.x += 1;
                        }
                        if(xmoved < -(pano_cont.pano_image.width/2)+180)
                        {
                            pano_cont.pano_image.x = -2538;
                        }
                        if(xmoved > (pano_cont.pano_image.width/2)- 180)
                        {
                            pano_cont.pano_image.x = -2178;
                        }

                    }

                     

                    function moveRight (e:MouseEvent):void
                    {
                        btn_right.gotoAndStop("down");
                        moving = 1;
                    }
                    function moveLeft (e:MouseEvent):void
                    {
                        btn_left.gotoAndStop("down");
                        moving = 2;
                    }
                    function stopMoveLeft (e:MouseEvent):void
                    {
                        moving = 0;
                        btn_left.gotoAndStop("active");
                    }
                    function stopMoveRight (e:MouseEvent):void
                    {
                        moving = 0;
                        btn_right.gotoAndStop("active");
                    }

                    • 7. Re: How do you run loop functions on specific frames of nested Movieclips?
                      kglad CommunityMVP

                      you're not doing anything wrong.  that's the compiler being less than clever.

                       

                      add the following to frame 1:

                       

                      var moveImage:Function;

                      • 8. Re: How do you run loop functions on specific frames of nested Movieclips?
                        Webshark2000 Community Member

                        Thanks for the help.  That did get rid of the "1120: Access of undefined property moveImage" error, but it still did't remove the ENTER_FRAME event listener from the parts_mc movieclip.

                         

                        I even tried writing the code for one of the other buttons, so it would execute on MOUSE_DOWN, hoping it would remove it before they even changed the page, but that didn't work.

                         

                        btn_prods.addEventListener(MouseEvent.MOUSE_DOWN, stopConstantLoop);


                        function stopConstantLoop(e:MouseEvent):void
                        {
                            if(pages_mc.parts_mc!=null){
                           
                            pages_mc.parts_mc.removeEventListener(Event.ENTER_FRAME, moveImage);
                            }
                        }

                         

                        I put a trace in the moveImage function and it just keeps going even after the pages_mc frame is changed.

                        • 10. Re: How do you run loop functions on specific frames of nested Movieclips?
                          Webshark2000 Community Member

                          kglad wrote:

                           

                          what's parts_mc.parts_mc???

                          Not sure where you see that.  I see pages_mc.parts_mc which is just because the parts_mc movieclip is nested on the 6th frame of the pages_mc movieclip.

                           

                          At any rate, I was able to get it working by moving all the code onto frame 2 of the main timline and declaring all of my vairables on frame 1 of the main timeline.  I then added an ENTER_FRAME event listener onto one of my movieclip buttons (main_btn) on the main timeline and had it check what frame the pages_mc movieclip was on.  If it was frame 6, I then executed the moveImage function and had it remove the event listener from the main_btn movieclip if the frame changed away from frame 6.

                           

                          Seems to be working so far.

                           

                          Here's the code:

                           

                          Frame 1

                          /* Declaring variables. */
                          var pagenumber:Number;
                          var moving:Number = 0;
                          var firstTime:Number = 0;
                          var xmoved:Number;
                          var xbegin:Number;

                           

                          Frame 2

                          ...

                          pages_mc.addEventListener(Event.ENTER_FRAME, pageNum);

                          pages_mc.addEventListener(Event.ENTER_FRAME, BtnSelParts);

                           

                           

                          /* Keeps track of current page. */
                          function pageNum(e:Event):void
                          {
                              pagenumber = pages_mc.currentFrame;
                              title_bar.gotoAndStop(pages_mc.currentFrame);
                          }

                           

                           

                          function BtnSelParts(e:Event):void
                          {
                              if(pagenumber == 6)
                              {
                                  btn_parts.gotoAndStop("current");
                                  if(firstTime == 0)
                                  {
                                      btn_main.addEventListener(Event.ENTER_FRAME, moveImage); //moves the panorama image left or right
                                  }
                              }
                              else
                              {
                                  if(btn_parts.currentFrame == 32)
                                  {
                                      btn_parts.gotoAndStop(1);
                                  }
                              }
                          }

                           

                           

                          function moveImage(e:Event):void
                          {
                              if(pagenumber==6)
                              {       
                                  if(firstTime == 0)
                                  {   
                                      pages_mc.btn_left.addEventListener(MouseEvent.MOUSE_OVER, overLeft); //animates left arrow button on rollover
                                      pages_mc.btn_right.addEventListener(MouseEvent.MOUSE_OVER, overRight); //animates right arrow button on rollover
                                      pages_mc.btn_left.addEventListener(MouseEvent.MOUSE_DOWN, moveLeft); //changes the variable "moving" to 2 on mouse down
                                      pages_mc.btn_right.addEventListener(MouseEvent.MOUSE_DOWN, moveRight); //changes the variable "moving" to 1 on mouse down
                                      pages_mc.btn_left.addEventListener(MouseEvent.MOUSE_UP, stopMoveLeft); //changes the variable "moving" to 0 on mouse up
                                      pages_mc.btn_right.addEventListener(MouseEvent.MOUSE_UP, stopMoveRight); //changes the variable "moving" to 0 on mouse up
                                      pages_mc.btn_left.addEventListener(MouseEvent.MOUSE_OUT, rollOutLeft); //animates left arrow button on rollout
                                      pages_mc.btn_right.addEventListener(MouseEvent.MOUSE_OUT, rollOutRight); //animates right arrow button on rollout
                                      xbegin = pages_mc.parts_mc.pano_image.x;
                                      firstTime = 1;
                                  }
                                  xmoved = xbegin - pages_mc.parts_mc.pano_image.x;
                                  if(moving == 1)
                                  {
                                      pages_mc.parts_mc.pano_image.x -= 1;
                                  }
                                  if(moving == 2)
                                  {
                                      pages_mc.parts_mc.pano_image.x += 1;
                                  }
                                  if(xmoved < -(pages_mc.parts_mc.pano_image.width/2)+180)
                                  {
                                      pages_mc.parts_mc.pano_image.x = -2538;
                                  }
                                  if(xmoved > (pages_mc.parts_mc.pano_image.width/2)- 180)
                                  {
                                      pages_mc.parts_mc.pano_image.x = -2178;
                                  }
                                  if(pages_mc.parts_mc.pano_image.pano1.currentFrame == 1)
                                  {
                                      pages_mc.btn_sel.gotoAndStop(1);
                                  }
                                  if(pages_mc.parts_mc.pano_image.pano1.currentFrame == 2)
                                  {
                                      pages_mc.btn_sel.gotoAndStop(2);
                                  }
                                  if(pages_mc.parts_mc.pano_image.pano1.currentFrame == 3)
                                  {
                                      pages_mc.btn_sel.gotoAndStop(3);
                                  }
                              }
                              else
                              {
                                  btn_main.removeEventListener(Event.ENTER_FRAME, moveImage);
                                  firstTime = 0;
                              }
                          } // controlls the panaromas

                           

                           

                          Let me know if you see any problems with this.

                           

                          Thanks again.