11 Replies Latest reply on Mar 27, 2008 9:56 AM by Greg Dove

    Calling a function in an attached MovieClip

    brian914 Level 1
      I am trying the following, but it seems like the function is not getting called. If I trace it, it returns an undefined. What am I doing wrong?

      Thanks a lot for any help.

      this.attachMovie("team_" + whichProfile, "team_" + whichProfile, this.getNextHighestDepth());
      var teamProfile = ["team_" + whichProfile];
      this[teamProfile].fadeInPhotos();
        • 1. Re: Calling a function in an attached MovieClip
          Neatgadgets Level 1
          Not exactly sure what you are trying to do but there is a problem in the line

          this.getNextHighestDepth());

          there are two closing brackets. But this might just be a typo.
          • 2. Re: Calling a function in an attached MovieClip
            Brian Forstat
            Sorry I should have explained that better. I am attaching a movieclip called "team_" + whichProfile, with whichProfile being a number. Inside of that MC, I have a function called fadeInPhotos. All I am trying to do is call that function. I can't seem to figure out how to correctly call that function.

            Thanks for the help!
            Brian

            • 3. Re: Calling a function in an attached MovieClip
              Level 7
              Hello brian914,

              > this.attachMovie("team_" + whichProfile, "team_" + whichProfile,
              > this.getNextHighestDepth());
              > var teamProfile = ["team_" + whichProfile];
              > this[teamProfile].fadeInPhotos();


              this.attachMovie("team_" + whichProfile, "team_" + whichProfile, this.getNextHighestDepth());
              this["team_" + whichProfile].fadeInPhotos();


              Also, that seems weird for me that you are using a dynamic variable for your
              linked object. You have several similar objects in your library?
              Maybe, you'd better have only one and refer to it as:

              this.attachMovie("team_mc", "team_" + whichProfile, this.getNextHighestDepth());

              Or smth. like that.


              • 4. Calling a function in an attached MovieClip
                Greg Dove Level 4
                var teamProfile = ["team_" + whichProfile];
                this[teamProfile].fadeInPhotos();

                For those two lines above, you are doing this:

                1.creating an array which contains a single element which is a string
                2.trying to use the array as a string inside the array access operator for the current timeline
                what you are trying to do is this:
                this["team_" + whichProfile].fadeInPhotos();

                But unfortunately that won't work.
                The code in the parent timeline's frame completes before the code in the newly attached movie's frame. So the fadeInPhotos function is not yet defined when it is being called.

                You can either a) code fadeInPhotos to run on the timeline of the attached clip...so it will run when its turn is up. or b) wait to call it from the parent timeline.
                Perhaps there are other ways too. Here is one. This is a very 'hacky' approach, I wouldn't actually use it myself ( would prefer that the MovieClip.onLoad handler could be assigned... but it cannot in this instance, its only for classes):


                • 5. Re: Calling a function in an attached MovieClip
                  brian914 Level 1
                  Something about the path is not right. I have tried to call the fadeInPhotos function at a later time via one of my buttons and it still does not work, when I know for sure that clip has been successfully attached.

                  When I trace the following, it returns an undefined.
                  trace("this[teamProfile].fadeInPhotos() is: " + this[teamProfile].fadeInPhotos());

                  trace("this['team_' + whichProfile].fadeInPhotos() is: " + this["team_" + whichProfile].fadeInPhotos());

                  I even tried hardwiring this, like so:

                  trace("this.team_0.fadeInPhotos() is: " + this.team_0.fadeInPhotos());

                  So what am I doing wrong? This seems pretty straight forward. I am attaching an MC and then trying to call a function inside that MC. Why is this not working?

                  Thank you so much for the help!

                  • 6. Re: Calling a function in an attached MovieClip
                    Greg Dove Level 4
                    try some cascading traces to see what you can be sure of.

                    //have I got the name right?
                    trace("teamProfile: " + teamProfile);
                    //is the name a string
                    trace("teamProfile is a string: " + ((typeof teamProfile=="string")? "yes": "no"));
                    //is the movieclip correctly pathed
                    trace("this[teamProfile] is: " + this[teamProfile]);
                    //is the function defined
                    trace("this[teamProfile].fadeInPhotos is: " + this[teamProfile].fadeInPhotos);
                    //does the function return a value
                    trace("this[teamProfile].fadeInPhotos() is: " + this[teamProfile].fadeInPhotos());
                    • 7. Re: Calling a function in an attached MovieClip
                      brian914 Level 1
                      Wow, I like that...
                      This is what I got.

                      teamProfile: team_0
                      teamProfile is a string: no
                      this[teamProfile] is: _level0.team_0
                      this[teamProfile].fadeInPhotos is: undefined
                      this[teamProfile].fadeInPhotos() is: undefined

                      Does this mean, it just can't see the function? Why could that be?
                      If I go into team_0 and trace fadeInPhotos, it tells me it is a function.

                      What does this mean?

                      Thank you so much!
                      • 8. Re: Calling a function in an attached MovieClip
                        brian914 Level 1
                        Wow, I like that...
                        This is what I got.

                        teamProfile: team_0
                        teamProfile is a string: no
                        this[teamProfile] is: _level0.team_0
                        this[teamProfile].fadeInPhotos is: undefined
                        this[teamProfile].fadeInPhotos() is: undefined

                        Does this mean, it just can't see the function? Why could that be?
                        If I go into team_0 and trace fadeInPhotos, it tells me it is a function.

                        What does this mean?

                        Thank you so much!
                        • 9. Re: Calling a function in an attached MovieClip
                          Greg Dove Level 4
                          In which case, what I said earlier is happening, you need to delay the call.
                          (I also find it unusual that the teamProfile is working as an array, because its not a string ...but I assume its because the toString method is being called in that case)

                          try this:
                          var targ=this[teamProfile];
                          setTimeout(function(){targ.fadeInPhotos.call(targ)},40); //set this number at a delay that is longer than your framerate
                          • 10. Re: Calling a function in an attached MovieClip
                            brian914 Level 1
                            wow... that is working. This is really weird to me though for a couple of reasons. One, I could not call it from a button later on. Second, I actually had this on a Fuse call back, which means, it was not getting called until the MC that gets attached was faded in.
                            So those two things make no sense to me. But thank you so much for helping me through this. I think I will try to move this forward from here.

                            Thank you!!!
                            • 11. Re: Calling a function in an attached MovieClip
                              Greg Dove Level 4
                              you're welcome. Good luck with it