10 Replies Latest reply on Mar 26, 2010 7:45 AM by celebritymusic

    Removing movieclips question (as3)

    celebritymusic Level 1

      Hi

       

      I have a .fla that contains five movieclips, and have a couple of questions regarding removing movieclips and swf size.

       

      If all the movieclips are displayed, my resulting swf is about 50k.

       

      If, in frame 1, I code conditionals that decide which of the movieclips will be displayed, will the resulting swf size become smaller if some of the clips aren't displayed?

       

      If so, what is the correct way to totally remove a movieclip from memory? Say I have a main movieclip that contains 5 other mcs, and the as code is within the main movieclip.

       

      If not, is there a way to make the swf size smaller if less than all clips are displayed (using conditionals).

       

      Thanks for your time and help.

       

      Shaun

        • 1. Re: Removing movieclips question (as3)
          Ned Murphy Adobe Community Professional & MVP

          File size is determined by the amount of content the swf file holds, not necessarily what it displays.  If your file holds 5 movieclips, the weight of those 5 movieclips is included in the file weight.  To permanently remove things from the file size they must be physically removed from the fla and then the file saved using the Save and Compact option before you publish the swf.

          • 2. Re: Removing movieclips question (as3)
            celebritymusic Level 1

            Ahh - thanks Ned

             

            Where then could I find some information about creating movieclips on the fly with code - ie if a condition is met, then create 5 movieclip buttons that are the same apart from one variable.  Is this sort of thing possible?

             

            Cheers

             

            Shaun

            • 3. Re: Removing movieclips question (as3)
              Ned Murphy Adobe Community Professional & MVP

              I will chance saying that you can create alot of things dynamically using just code, though when you go completely with code you may end up with less visually satisfying--but it depends on what level of visual impact you intend.  For instance, the following will create a black square as a movieclip dynamically...

               

              var mc1:MovieClip = new MovieClip();
              mc1.graphics.beginFill(0x000000);
              mc1.graphics.drawRect(0, 0, 100, 100);

              mc1.mcVariable = "something";
              addChild(mc1);

               

              But you can create a symbol that you store in the library and then call it into use dynamically as well.  In AS3 you right click whatever movieclip symbol that is in the library and select Linkage.  You set it to Export for Actionscript and assign it a Class name.  So if you gave it a Class name of "MC", then in your code you can create an instance of it using...

               

              var newMC:MC = new MC();

              newMC.mcVariable = "something";

              addChild(newMC);

               

              The weight of the one symbol is carried in the file, but it can be used multiple times.  And since it is a MovieClip symbol in this case, you can dynamically assign properties to it.

              1 person found this helpful
              • 4. Re: Removing movieclips question (as3)
                celebritymusic Level 1

                OK - that's making things clearer.

                 

                Is this possible...

                 

                Once you've dynamically created an mc, can you dynamically add actionscript to the timeline within the mc?  For example, I currently have 5 buttons that I would prefer to create dynamically.  They all share exactly the same code (some of it in the button timeline, some in an external as function which the button code calls).  The only difference for each button is the text it displays - each button's text comes from a unique global var.  This all works fine.

                 

                My aim, if possible, is to have a conditional that says 'if this condition is met, draw button 1, insert this common code in it, and then this unique code for it's text content'...and then the same for each consecutive button.

                 

                Is that possible?

                 

                Cheers

                 

                Shaun

                • 5. Re: Removing movieclips question (as3)
                  Ned Murphy Adobe Community Professional & MVP

                  If you create an object entirely from code, such as my first example, it won't have a timeline you can fiddle with.  If you instead create it from a symbol in the library, then it will have whatever timeline that you created for that symbol.  It could have a textfield that you assign when the object is instantiated and you could assign event listeners and various other things to it as well.

                  1 person found this helpful
                  • 6. Re: Removing movieclips question (as3)
                    celebritymusic Level 1

                    Ned - you've been great.  Last question today I promise...

                     

                    Ok - so if I have a movieclip button symbol that I add to my project using code like you explain.

                     

                    (In the current versions of the buttons I have, I use a variable to add text - but1txtfield.text = globals.data.but1text  - variable)

                     

                    Once I've added the symbol with code, and perhaps I've added 4 buttons using that symbol (using four different conditionals to decide if each button should be there), is there a way to add into the conditionals instructions to insert globals.data.but1text into button 1's code, globals.data.but2text into button 2...?

                     

                    ps currently I have the function code for the text fields in a global.as, and each button has the following code:

                     

                    this.buttonMode = true;

                    this.mouseChildren = false;

                     

                    globals.buttonfunc(txt);

                     

                    txt.text = globals.data.but1txt //(or but2txt etc)

                     

                    stop();

                     

                     

                    Cheers

                     

                    Shaun

                    • 7. Re: Removing movieclips question (as3)
                      Ned Murphy Adobe Community Professional & MVP

                      If I'm getting it, then one way yoiu could manage this would be to assign the text when you create the instance, rather than having code inside the button looking for it at the moment its brought into being.  So as you add button 1, you have that number available (somehow) to work into the code...

                       

                      for(var i:uint=1; i<6; i++){

                           var btn:Btn = new Btn();

                           btn.name = "btn"+string(theNumber);

                           btn.txt.text = globals.data[btn.name+"txt"];

                      }

                       

                      Or if you keep that code inside the button (and it works to grab the globals), borrowing a tad from the code above in naming the button when you create it, you could use something like...

                       

                       

                      txt.text = globals.data[this.name+"txt"];

                      • 8. Re: Removing movieclips question (as3)
                        celebritymusic Level 1

                        Hmm - yes - that's got me thinking now.

                         

                        I had a play around this arvo before getting your message tonight, and I noticed that when I tried to add text from outside of the button, it displayed it with the correct filters (determined in the external as), but ignored color and font size info in the text format section (also found in the external as).  I'm not sure why.

                         

                        So I think I will go with keeping the variable in the button.

                         

                        One thing I'm unsure of is what the "txt" represents in the this.name+"txt" ?

                         

                        Nonetheless, using your method of getting the button name and adding it to the variable will work nicely, even if I just used this.name in the variable, and assigned text the the variable elsewhere in the project.

                         

                        I also had another idea today.  My main objective is to cut down on the size of my swf - it has a lot of button symbols.  That was why I wanted to know how to create the buttons with code as they are needed.  But if I can add your new text global variable into each button, the code for each button becomes identical - in other words, I could use just one movie clip symbol from the library for ALL the buttons.

                         

                        So my question (finally), is, will using one button symbol from the libray to replace all the other button symbols (delete them from the library) make my compiled swf smaller?

                         

                        If not, I will have to create the buttons on the fly with code.

                         

                        Cheers Ned

                         

                        ps - it's 1am here - that's why I'm permitting myself a new question - cheeky, I know

                        • 9. Re: Removing movieclips question (as3)
                          Ned Murphy Adobe Community Professional & MVP

                          Anything you can remove from use in the file will reduce its size... the swf file carries the weight of whatever is used in it.  The fla carries the weight of everything in it whether its used or not, and if you never use Save and Compact, the fla can carry alot of memories of how it came to be what it is and sometimes pass some of that into the swf (it would seem at times, but shouldn't be).

                           

                          Try permitting yourself a new posting instead of building continuously onto the same one with new questions.  It helps to keep different questions separate for those who search for help, and it also helps you get more noticed if there's a 0 sitting in the Replies column.

                          • 10. Re: Removing movieclips question (as3)
                            celebritymusic Level 1

                            Thanks Ned - I had a play today and managed to strip 20% off my project size by replicating one symbol with one set of code bringing in a variable based on each instance name.  Very satisfying day of coding.  Cheers for your help - I appreciate your time.

                             

                            Shaun