9 Replies Latest reply on May 19, 2006 5:49 AM by Newsgroup_User

    which is right? using createEmptyMovieClip

      var container:MovieClip = this.createEmptyMovieClip("container", 0);

      or

      this.createEmptyMovieClip("container", 0);

      i've seen the samples use both ways, what would be the best way or does one do something the other doesn't?

        • 1. Re: which is right? using createEmptyMovieClip
          Level 7
          flasheister,

          Subj: which is right? using createEmptyMovieClip

          > var container:MovieClip = this.createEmptyMovieClip("container", 0);
          >
          > or
          >
          > this.createEmptyMovieClip("container", 0);

          Those are almost the same thing. I'll explain.

          > i've seen the samples use both ways, what would
          > be the best way or does one do something the
          > other doesn't?

          In both cases, you're invoking the MovieClip.createEmptyMovieClip()
          method against a movie clip instance. The location of this code determines
          which instance that is. The global "this" property points to whatever
          timeline or object it's in. If this is written in the main timeline, for
          example, then "this" refers to the main timeline -- so that's your movie
          clip. Check out this method in the ActionScript 2.0 Language Reference ...
          you'll see that the method provides a return value. The return value is of
          type MovieClip, and it points to the movie clip created by the method.

          So in the first example (the one with var), the return value is stored
          in the variable container. The problem there is that you're creating a
          movie clip with the instance name container, and storing that reference in a
          variable named container -- so it's redundant. In the second example,
          you're not using the return value, which is fine. In any case, the newly
          created movie clip has the instance name container, so you can use that
          instance name and ... just use it.


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: which is right? using createEmptyMovieClip
            nicknameforme Level 1
            I just had this exact same question. David you cleared it up slightly, but what I don't understand (and maybe flasheister doesn't understand) is why would you use the var option then? Is there any advantage to it?
            • 3. Re: which is right? using createEmptyMovieClip
              Rothrock Level 5
              There is only an advantage to it if you ever need to refer to that movieclip again. Like this:

              myClip=this.createEmptyMovieClip("container",100);
              myClip._x=100;

              In this example to position the newly created clip's x property to 100. But you can use it in any place where you need to reference the clip – with the MovieClipLoader, the drawing API, etc.

              I don't think David has quite hit it exactly on the head. Where he is talking about the name being redundant. True, in this example, the new movieclip has the same name as the variable, but the createEmptyMovieClip method returns the full path to the clip, so there are times when the variable could be more useful. For example.

              holder = this["SomeStem"+counter].createEmptyMovieClip("holder",100);

              They are both called holder, but they will be in different places.

              Also, just keep in mind when reading the help files that anytime you see a description that includes "Returns" it is telling you what you can assign to a variable should you need to reference/use that info again.
              • 4. Re: which is right? using createEmptyMovieClip
                nicknameforme Level 1
                Aha. Now that makes sense.

                This leads me to another question though -- when you use this["SomeStem"+counter] why is there no "." between 'this' and the [.

                Is it because it's on the same level... where is this variable bracketing convention listed in the Help file. I'm trying to better understand it.
                • 5. Re: which is right? using createEmptyMovieClip
                  Level 7
                  The brackets - "array access operators" -- are used to resolve Strings
                  into MovieClips (turn strings into movieclips).

                  It is in the help files under "Operators > [] array access operator",
                  and this is #3 of the uses:

                  Usage 3: You can use the array access ([]) operator instead of the
                  eval() function to dynamically set and retrieve values for movie clip
                  names or any property of an object. The following line of code sends the
                  number 6 to the Output panel.

                  name["mc" + i] = "left_corner";

                  Also check out.

                  http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically

                  nicknameforme wrote:
                  > This leads me to another question though -- when you use
                  > this["SomeStem"+counter] why is there no "." between 'this' and the [.
                  >
                  > Is it because it's on the same level... where is this variable bracketing
                  > convention listed in the Help file. I'm trying to better understand it.
                  >
                  • 6. Re: which is right? using createEmptyMovieClip
                    Level 7
                    Sorry -- of course it won't always "send the number "6" to the output
                    panel... that was copied and pasted direct from Help, and was part of
                    the previous example.

                    ImagicDigital wrote:
                    > The following line of code sends the number 6 to the Output panel.
                    >
                    > name["mc" + i] = "left_corner";
                    • 7. Re: which is right? using createEmptyMovieClip
                      nicknameforme Level 1
                      Thanks for that clarification. Although the help page you mention is quite confusing. I don't see how the example name["mc"+i}.... has any relevance to anything on that page. It's an example with no precedent and no context.

                      What does the number 6 have to do with "left-corner". Just curious. I think there's something wrong with the docs, there.

                      Thankfully, despite the docs, you cleared it up.
                      • 8. Re: which is right? using createEmptyMovieClip
                        Rothrock Level 5
                        Here is how I think about the array access notation.

                        Flash keeps track of everything as an object and the object can be a container for more objects. Something like this:

                        myClip["Something"+var]._x

                        means to me: look inside the object called myClip, evaluate "Something" + var, and find the object with that name. Then look inside that object for _x.

                        There is no dot, because the dots represent branches on the tree. So when you are already looking inside (in effect to the next branch) you don't need the dot.

                        Don't know if this is entirely correct or even close, but this is how I tend to think about it and it has helped me.
                        • 9. Re: which is right? using createEmptyMovieClip
                          Level 7
                          Rothrock,

                          > I don't think David has quite hit it exactly on the head.
                          > Where he is talking about the name being redundant.
                          > True, in this example, the new movieclip has the same
                          > name as the variable, but the createEmptyMovieClip
                          > method returns the full path to the clip, so there are
                          > times when the variable could be more useful.

                          Ah, good. Yes, thanks for the elaboration, Rothrock! There was a
                          moment where I actually thought what you explained while typing my reply,
                          but the thought didn't make it to my fingers.

                          An object reference is useful any time the full path is a bit "wordy"
                          (long to remember, or long to type). The only potentially confusing thing
                          in the original example is that the reference and the movie clip have the
                          same name and are in the same location (same path).

                          I often use the return value of MovieClip.createEmptyMovieClip() -- in
                          other words, the object reference to the new clip -- in for() statements, if
                          I'm performing a mass event handler assignment.

                          var thisClip:MovieClip;
                          for (var n=0; n<10; n++) {
                          thisClip = this.someContainer.createEmptyMovieClip("sampleClip" + n, n);
                          thisClip.onRelease = function() {
                          // something here
                          }
                          }

                          The above is marginally more convenient to type than ...

                          var thisClip:MovieClip;
                          for (var n=0; n<10; n++) {
                          thisClip = this.someContainer.createEmptyMovieClip("sampleClip" + n, n);
                          this.someContainer["sampleClip" + n].onRelease = function() {
                          // something here
                          }
                          }


                          David
                          stiller (at) quip (dot) net
                          Dev essays: http://www.quip.net/blog/
                          "Luck is the residue of good design."