9 Replies Latest reply on Aug 21, 2006 10:49 AM by Cdndesign

    variables in a function

    Cdndesign Level 1
      I want to enhance a script I found as a tutorial on http://www.lukamaras.com/tutorials/actionscript/load-jpg/loading-external-jpg-image.html

      essentially, it's comprised of two parts. The first part which can hopefully remain the same is
      function startLoading(whichImage) {
      loadMovie(whichImage, "imageLoader");
      _root.onEnterFrame = function() {
      infoLoaded = imageLoader.getBytesLoaded();
      infoTotal = imageLoader.getBytesTotal();
      percentage = Math.floor(infoLoaded/infoTotal*100);
      infoField.text = percentage+"%";
      if (percentage>=100) {
      delete this.onEnterFrame;
      infoField._visible = false;
      }
      };
      }

      The second part consists of code specifying the button and the action assigning the picture to be loaded

      _root.menuclip.imgbtn1.onRelease = function() {
      infoField._visible = true;
      startLoading("picture1.jpg");
      };
      _root.menuclip.imgbtn2.onRelease = function() {
      infoField._visible = true;
      startLoading("picture2.jpg");
      };
      _root.menuclip.imgbtn3.onRelease = function() {
      infoField._visible = true;
      startLoading("picture3.jpg");
      };

      While I really love this tutorial, the flash file I am working with has a minimum of 150 buttons to it, each loading its own picture.
      Technically, I could copy and paste the code 150 times I was wondering if there was an alternate solution saving on code using variables. Something like:
      _root.menuclip.VARbuttonname.onRelease = function() {
      infoField._visible = true;
      startLoading(VARpicturename);
      };
      and just have each button assigned to set the VARbuttonname and VARpicturename onRollover.

      I have made attempts at it but I am very new to actionscripting and still trying to get the syntax down.

      Can anyone help me acheive this by this method or any other alternative?

      Please let me know

      Thanks

      Dustin
        • 1. Re: variables in a function
          Nixy Level 1
          I suggest that you use MC instead of buttons. This way you can add variable like an id to represent the button number. There is many way to do that. In the following code, you need to have your "imgbtn" as MovieClip. The variable numOfBtn is the number of MC (buttons), you have. The first one must start at 1. For exemple imgbtn1, imgbtn2, imgbtn3...

          function setBtnEvents():Void {
          var numOfBtn:Number = 1;

          for(var i:Number = 1; i <= numOfBtn; i++) {
          var tempBtn:MovieClip = _root.menuclip["imgbtn" + i];
          tempBtn.id = i;
          tempBtn.onRelease = function():Void {
          infoField._visible = true;
          startLoading("picture" + this.id + ".jpg");
          }
          }
          }

          hope that can help!

          • 2. Re: variables in a function
            Cdndesign Level 1
            Thanks. But here's where I reveal how noob I am. Where do I put the actionscripting? On a frame in the root? On the MC called menuclip? On root I have an MC called menuclip and inside it is three other mcs named imgbtn1 imgbtn2 imgbtn 3.
            Also on root I have an MC called imageLoader.

            Please let me know where to put the code. I am attempting this on a simple demo before trying to implement it in the main fla

            Thanks
            • 3. Re: variables in a function
              Nixy Level 1
              This functions is supposed to be placed on a exactly at the same place where you have the function startLoading.
              This is used to replace the code
              _root.menuclip.imgbtn1.onRelease = function() {
              infoField._visible = true;
              startLoading("picture1.jpg");
              };

              Do one thing. Before usign my function. Try by testing you movie with one or two image. Use your original code. If all works, that you can click on your button and load images, then remove all the _root.menuclip.imgbtn1.onRelease... you have and place my function there. This function is the samething, but for the number of MovieClip (MC instead of Button), you want. Again test it with one or 2 buttons. If that works great you can now use it with number of MC you want. Just change the numOfBtn value to the number of MC you have.

              If you have problem, share you .fla. I'll take a look at it. But before just try it.
              • 4. Re: variables in a function
                Cdndesign Level 1
                Sorry about my delay. I was discussing dns issues with my web host.

                Thanks again for your advice. I have created a quick demo which contains the code I use to load the pictures and your code as well. My code is commented out at the moment but when uncommented it works well. I have uploaded a zip file that includes the .fla and a folder with 3 images that I am using to test the code.
                http://216.8.139.182/~dustin/picturetest.zip
                Hopefully you can help me in figuring out what I am doing wrong.

                Thanks
                • 5. Re: variables in a function
                  Nixy Level 1
                  Hi
                  Sorry I wasn't at the office yesterday. So here it is.
                  In your code all was great. Just one thing. You forgot to call the function. The code was there at the right place, but just need to call it to execute. You can download the new .fla at this adresse:
                  http://www.crimulti.com/picturesTest.zip

                  I have 2 things about the .fla. Nothing verry important, but just great way to work.

                  1- Always keep the script layer at the top of the other layers. The reason is simple. When Flash compile documents, you can set the it to compile layers from the bottom to the top or from the top to the buttom. By default, the value is bottom to top. I explain it just for one reason. A script that execute before the object (MC) exist, will not affect your object. In your case, the function would execute, and after your button would appear. So the function would not affect your MC.

                  2- If you have a script on the root that needs to affect content inside an MC (that is your case), call the script on the second frame. Don't forget to put a stop also. The reason, again, is simple. When Flash compile if all your movie is on one frame, it compress all layer, your code execute after the clip exist, cause the script layer is on the top, right, but the content of the clip may not all still exist. So if you call the function on the second frame. All the layers at the first frame are compiled, it means that all including Clip Content exist. Son when the code execute on the second frame, you are shure that it will affect all inside your MC. This is not really important with small content. But when you have many Mc inside Mc and so on... This is a good way to work.

                  All that is done in the new FLA
                  Have a good day.
                  • 6. Re: variables in a function
                    Cdndesign Level 1
                    You are an absolute genius!!!!

                    Thank you sooo much. It works perfectly. I still have quite a bit to learn to understand how to create a function and how to call one, but it's help like this that expands my understanding and advances me further. Thank you very much for your time, energy, and efforts toward my dilemma. I really appreciate it. I checked out your site and your portfolio work.....it proves yet again, that you are a genius! :)
                    • 7. Re: variables in a function
                      Nixy Level 1
                      You're welcome
                      Tanx for your comments!
                      • 8. Re: variables in a function
                        Cdndesign Level 1
                        This follow-up post is for anyone looking for the answer to this problem as solved by Nixy (thanks again, Nixy...you are a true genius.) In the event that the links posted during our discussion don't work, here is the code that Nixy combined with what I found in a tutorial to make 150 buttons load 150 seperate external pictures into a flash file without repetition of code.

                        In my example, I have an empty MC called imageLoader and a dynamic text field called infoField. I also have an MC called menuclip. Inside MC menuclip are additional MC's named imgbtn1, imgbtn2, imgbtn3 ... etc.

                        On the root directory, placed on a frame in a layer of it's own..the following code makes the magic happen:

                        function setBtnEvents():Void {
                        var numOfBtn:Number = 150;

                        for(var i:Number = 1; i <= numOfBtn; i++) {
                        var tempBtn:MovieClip = _root.menuclip["imgbtn" + i];
                        tempBtn.id = i;
                        tempBtn.onRelease = function():Void {
                        infoField._visible = true;
                        startLoading("images/picture" + this.id + ".jpg");
                        }
                        }
                        }


                        // function to loads Career title picture
                        function startLoading(whichImage) {
                        loadMovie(whichImage, "imageLoader");
                        _root.onEnterFrame = function() {
                        infoLoaded = imageLoader.getBytesLoaded();
                        infoTotal = imageLoader.getBytesTotal();
                        percentage = Math.floor(infoLoaded/infoTotal*100);
                        infoField.text = percentage+"%";
                        if (percentage>=100) {
                        delete this.onEnterFrame;
                        infoField._visible = false;
                        }
                        };
                        }

                        // Init functions call
                        setBtnEvents();



                        If you found this useful, be sure to thank Nixy....he's the hero of the hour.
                        • 9. variables in a function
                          Cdndesign Level 1
                          Ok. It turns out that my situation just got a tad more complicated and now I need help revising what was previously built. Please Nixy or anyone who would have the answer...please help.

                          The code listed in the above post works beautifully if you have one MC (called menuclip) that holds your menu buttons but through some intense revisions my menu is laid out kind of like this

                          Root:

                          Menu1
                          Submenu1
                          button1
                          button2
                          button3
                          button4
                          button5
                          button6
                          button7
                          button8
                          button9
                          button10

                          Submenu2
                          button1
                          button2
                          button3
                          button4
                          button5
                          button6
                          button7
                          button8
                          button9
                          button10


                          Menu2

                          Submenu1
                          button1
                          button2
                          button3
                          button4
                          button5
                          button6
                          button7
                          button8
                          button9
                          button10

                          Submenu2
                          button1
                          button2
                          button3
                          button4
                          button5
                          button6
                          button7
                          button8
                          button9
                          button10

                          and so on...
                          In my scenerio I have on the root menu:
                          3 menus
                          each have 5 submenus
                          each submenu has 10 buttons

                          Is there a way of revising the code above to load a picture from any of the 10 buttons on each of the 5 submenus under each of the 3 menus? (total of 150 buttons)

                          Please help if you can. Thank you