11 Replies Latest reply on Aug 23, 2010 5:02 AM by toolmania1

    Multiple movie clips

    toolmania1

      I am making a web site in flash in which I have multiple movie clips.  I have mc_buttonMenu on the main timeline.  Inside of mc_buttonMenu, I have 3 buttons that are all button symbols.  From the main timeline, I have a movie clip called mc_allPages.  In this movie clip, I have a few layers.  I have the content layer.  This layer controls how my pages display.  I have another layer called labels.  I labeled frames 1 to 10 as 'home' and layers 11 to 20 as 'contactUs'.  I have another layer in mc_allPages called actions.  I put a stop(); on frame 10 and frame 20 on this layer. 

       

      I want to be able to control the movie clip mc_allPages from within the movie clip mc_buttonMenu.  I put some action script in various places, but it never worked.  I tried this action script:

       

      _root.btn_home.onPress()

      {

      Gotoandplay("home");

      }

       

      _root.btn_contactUs.onPress()

      {

      Gotoandplay("contactUs");

      }

       

      I tried placing that code on the main timeline, inside mc_allPages, inside mc_buttonMenu and split up into both buttons appropriately for btn_home and btn_contactUs. 

       

      Sometimes the animation would stop on frame 10 and sometimes it would just play through to frame 20 and ignore the stop(); I put on frame 10 inside the mc_allPages movie clip.  However, whenever I pressed the button ( btn_contactUs ), it never went to the frame labeled "contactUs" in the allPages movie clip.

       

      Any suggestions?

        • 1. Re: Multiple movie clips
          Ned Murphy Adobe Community Professional & MVP

          There are a couple of things off in the code you wrote.  How you target objects depends on where you place your code.  Assuming you have assigned the instance names as indicated (which are not the same as library names), you would use the following in AS2 to target things to happen if you place the code in the main timeline (I recommend using onRelease)...

           

          mc_buttonMenu.btn_home.onRelease = function()

          {

               mc_allPages.gotoAndPlay("home");

          }

           

          mc_buttonMenu.btn_contactUs.onRelease = function()

          {

               mc_allPages.gotoAndPlay("contactUs");

          }

          • 2. Re: Multiple movie clips
            toolmania1 Level 1

            So, I don't need to do anything with _root?

             

             

             

            Also, I can see the structure now of:

             

            movieClip.buttonInstanceName.functionYouWantToUse = function()

            • 3. Re: Multiple movie clips
              Rothrock Level 5

              Got two things for you. First I wouldn't it like this:

               

              instance.childInstance.methodOrPropertyOrEventYouWantToUse

               

              In your specific case it was a moveclip instance with a button instance inside it. But it could be a movie inside a movie or a FLVPlayback component inside a movie or...well you get the idea.

               

              Also in your case you are actually trying to get to an event (onPress, onRelease, etc.) But you might want to change the _x value or get some other thing.

               

              That all is just a subtle point of talking about it. Hopefully it helps make it more clear as opposed to less!

               

              Another little issue.

               

              The way you have listed there is called "anonymous function" because the function you are defining doesn't have a name. There is nothing wrong with it per se and I think most folks start out that way. But it is better to used named function. The way you have here means that if later you need to use that function again you will have to type it out again (or copy and paste). And then if you make a change to how it works you need to go and find all the places it is typed out and "fix" them.

               

              So a better way to do this might be:

               

              mc_buttonMenu.btn_home.onRelease = handleHome;

               

              function handleHome function()

              {

                   mc_allPages.gotoAndPlay("home");

              }

               

              Notice that in your assignment there are no parens after the handleHome. The parens say, in effect, execute this function now. In this case you don't want handleHome to happen when you assign the event handler, but rather when the event actually happens, that is when the button is released.

               

              1 person found this helpful
              • 4. Re: Multiple movie clips
                toolmania1 Level 1

                Rock,

                 

                Thanks.  Where would I put the named function?  Would I place this on a layer on the main timeline and call that layer 'functions'.  Then, I could just call that function by name everytime I wanted to use it.

                 

                I know c++, so I understand about classes if this is the direction you are headed.  :-)

                 

                Thanks,

                Tim

                • 5. Re: Multiple movie clips
                  Ned Murphy Adobe Community Professional & MVP

                  In general, you should avoid using _root references unnecessarily.  Some people use them as if everything needs them.  Most designs can avoid them entirely... though it is an occasionally convenient way to target something that can make anyone yield to the temptation..

                  1 person found this helpful
                  • 6. Re: Multiple movie clips
                    toolmania1 Level 1

                    Ya!

                     

                    Ned, I got it to work!.  I placed that code on the main timeline.  It did not work at first.  So, I looked at all of the instance names and everything was not named.  Once I supplied instance names for everything, that code worked.

                     

                    I made the same type of thing for contactUs and that worked also.

                     

                    I am pumped.  Thanks man!

                    • 7. Re: Multiple movie clips
                      toolmania1 Level 1

                      Rock,

                       

                      I tried this method also.  I placed the code on the main timeline.  However, I got some errors.  Am I supposed to put the function definition in the same location as the code that calls it?

                       

                      Thanks,

                      Tim

                      • 8. Re: Multiple movie clips
                        Rothrock Level 5

                        What errors are you seeing?

                        Yes it should be in the same scope as the code the calls it. So if your event handler assignments are on the main timeline then having the code on the main timeline is the place it aught to be. However if your assignment is inside a movieclip or something then you might want to move the assignments to the main timeline.

                         

                        While it is possible in AS2 to put code on timelines, within timelines, within timelines, and also directly onto clips, it isn't a great idea. It gets really difficult to find the code when it is spread out through all locations. It is generally better to have all the code on the main timeline so that it is easy to find.

                         

                        It isn't such a big deal if you just have a few lines and a few things going on. But it helps you structure your code better and leads to a different way of thinking about your code.

                         

                        It isn't strictly related to classes and oop, but it is a step in the direction of encapsulating and reuse.

                        • 9. Re: Multiple movie clips
                          Ned Murphy Adobe Community Professional & MVP

                          If you have experience with C++, and you are just starting off learning Flash, I encourage you to skip AS1/AS2 and start by learning AS3.  For some people AS3 can be a bit daunting to begin with, but the more experience you have with classes and OOP in general, the more likely you are to fair pretty well in picking things up easier with AS3.

                           

                          Another benefit of AS3 is the error reporting... AS2 can leave you staring at a design sitting at a standstill wondering why Flash doesn't work (it works quite welll BTW), but AS3 is very generous with error messages and can be undaunting when it comes to making you follow the rules.

                          • 10. Re: Multiple movie clips
                            Rothrock Level 5

                            Yeah I agree with Ned. If you don't have an overwhelming need to learn AS1/2 then starting with AS3 is a much better proposition. Even without the C++ experience you would probably be better served to go the AS3 route.

                             

                            At my work we have to publish to Flash 7 so we can't use it, but most places are going to be looking more toward the future...

                            • 11. Re: Multiple movie clips
                              toolmania1 Level 1

                              I was going to make a new posting, but this kinda falls under this title anyways.

                               

                              I added a preloader to this movie.  I had the movie working correctly and saved that version before I added this preloaded.  Pretty much, I pushed everything from the first frame to the second frame on the main timeline.  Then, I added a new layer called preloader.  I created two movie clips.  There is some action script also.  I am pretty sure all of that is working correctly.

                               

                              I had some flicker, but I fixed the errors with that.  The main movie does not flicker now.  However, I cannot get my buttons to go to frame "11" or to the label "contactUs".  I am pretty sure that the frame "11" is because I pushed the main timeline to frame number 2.  Does this have something to do with how all of the frames are totalled?  Therefore, by putting frame "11" in the action script, that would no longer work?

                               

                              So, if all of that is true, how do I tell a button to move to a label?

                               

                              I thought this was what I put:

                               

                              gotoAndPlay("contactUs");

                               

                              That does not seem to be working for me.

                               

                              However, maybe I have a separate issue.

                               

                              There is one error that is shown.  This error comes up after I Test Movie.  I do not have the file in front of me.  I can post that error later.  But, why would adding a preloader cause my buttons to not work?