8 Replies Latest reply on Oct 11, 2006 5:54 AM by DazFaz

    Cannot call function inside a movieclip from the stage

    Lelikg
      Hi
      I created a movieclip, which has a function declared in its first frame

      function tr()
      {
      trace("hello world");
      }

      i dragged the mc to the stage and gave it a name "my_mc"
      from the stage , first frame, i call the function
      my_mc.tr();

      nothing happens :(
      I tried declaring the function using tr = function() and nothing
      i tried calling this.my_mc.tr();
      I also tried calling the function from the movieclip onClipEvent(load) { this.tr() } and still nothing

      am i missing something?

      Thanks
      Leon.
        • 1. Cannot call function inside a movieclip from the stage
          DazFaz Level 1
          Ah the ever faithfull "trying to call a function before it even exists" problem, lol

          Try:

          myMovie.tr = function() {
          trace("Hello world");
          };
          myMovie.tr();//Calls the function after its been created
          myMovie.onRelease = function(){//Just to make sure the function is inside the "myMovie"
          this.tr();
          }

          //Oh by the way this function goes on the very top timeline. Always remember, Flash is liking opening boxes, and it thinks that way too. If you try and call a function from the first box (main time line), that you have created deep within another box (movieClip)then flash will not find it until its created. I think that is what your problem is.
          • 2. Re: Cannot call function inside a movieclip from the stage
            shikozel
            personally, I dont write functions inside movie clips. I found that it is only complex things. you can put the function in the root and call it from the root
            • 3. Re: Cannot call function inside a movieclip from the stage
              Lelikg Level 1
              It is a function that is supposed to create a text label, and stretch the background to the length of the text.
              This movieclip will have many instances and it would be much more comfortable to have the function inside it, in order to make it a component of sorts.
              • 4. Re: Cannot call function inside a movieclip from the stage
                DazFaz Level 1
                Neither do I shikozel but Flash is like baptism by fire, your lateral thinking grows with experience and until Lelikg can get their head around this, putting functions within MovieClips is a good place to start.
                • 5. Re: Cannot call function inside a movieclip from the stage
                  DazFaz Level 1
                  Lelikg, if you are doing a generic repetative action, then you might think about using prototypes. Let me know and I will explain a little more about them.
                  • 6. Re: Cannot call function inside a movieclip from the stage
                    Lelikg Level 1
                    Please do explain, this is very interesting.
                    Apart from having the function inside a movieclip, I am intrigued as to why I cannot access that function.
                    I seem to be in the right scope. I've been trying to make it work for a few hours now and I'm simply frustrated.
                    Even if I end up not using functions inside movieclips, i still am dying to know why this does not work and what I did wrong.

                    Thanks.
                    Leon.
                    • 7. Re: Cannot call function inside a movieclip from the stage
                      AnandMX Level 1
                      well u didnt done wrong, it wasnt loaded at that time thats it.
                      either refer it after a while,
                      or just put a cal inside the move itself, u ll find its working
                      • 8. Re: Cannot call function inside a movieclip from the stage
                        DazFaz Level 1
                        Lelikg, functions, methods and so on are great things really. Getting your head around the whole process is the hard part, well it was for me.  Once you are able to think on multiple levels and from the main timeline then that’s even better as your whole flash programming experience will leap forward with great bounds.

                        Ok then. As I’ve illustrated before, flash uses a principal very much like a series of boxes. The main time line is the BIGGEST box with everything else being put inside thereof. (Russian doll’s).

                        And it loads that way too. Unless you have predetermined a certain loading sequence (another way for another day maybe). I don’t know if you have noticed the “loading order” option in the publish settings (Flash Tab), but if not then I will just explain a little more. You can change the loading order of your swf. “Bottom Up” and “Top Down”. This is more relevant if you have multiple layers within your project. If you want the very top layer to load first, including all functions hoots and whistles on that layer, then you would set this option (load order) , within the publish settings, to “Top down”.

                        Baring in mind though this only relates to the layer loading sequence and not the code within the layers. For example, if you load “Bottom Up” the very bottom layer will load first but flash will still only load from “top down” within that layer. So if you have code right at the bottom of that layer, then this will be the very last thing to be recognised in the loading sequence.

                        So, if you try and call a function before it’s declared then it just will not happen because in “Flash’s” eyes it does not exist yet.