2 Replies Latest reply on Dec 23, 2008 8:29 AM by SLMHILL

    TARGETTING GLOBAL TEXTFIELDS

    SLMHILL
      I'm just teaching myself the ins and outs of targetting movieclips with script.

      Supposing you have set up several movieclips, each containing a dynamic textfield, with the var name "mytext"

      Exactly how do you refererence them all using _global from the main timeline so that all textfields contain the same text?

      I am just trying to figure out what the _global term does.

      Thanks & Merry Xmas!
        • 1. Re: TARGETTING GLOBAL TEXTFIELDS
          Level 7
          SLMHILL,

          > I'm just teaching myself the ins and outs of targetting
          > movieclips with script.

          Fortunately (in this regard, at least), movie clips act like any other
          object, so once you understand the concept, you'll be able to target movie
          clips, text fields, and anything else that can contain (or be contained by)
          other objects.

          > Supposing you have set up several movieclips, each
          > containing a dynamic textfield, with the var name "mytext"

          There are at least two ways to do this: a) the old school approach, in
          which you associate each text field with a variable by way of its Var (or
          Variable) field in the Property inspector, or b) the newer say, which
          requires that you give each relevant object (movie clip and text field) an
          instance name.

          Let's look at the old school approach first:

          > Exactly how do you refererence them all using _global from
          > the main timeline so that all textfields contain the same text?

          This gets a bit dicey, because based on a few tests I ran just now, it
          looks like the Var (or Variable) field doesn't like object paths. In other
          words, if I happen to have a variable named myText defined *in the same
          timeline* as the text field, everything works without a hitch. I simply put
          myText in the Var (or Variable) field of the Property inspector, and the
          string value appears in the text field when I test the SWF.

          What doesn't seem to work is a variable reference that exists in another
          timeline (that is, in another scope). For example, if my text field is
          inside a movie clip, yet the main timeline has the variable, the following
          expression should work, but doesn't:

          Var (or Variable) field: this._parent._parent.myText

          The reason it should work is because the hierarchy makes sense. The
          main timeline defines the variable myText. This timeline has a targettable
          child (the movie clip). The movie clip has a targettable child (the text
          field). Therefore, this object's parent's parent should lead to the
          variable.

          By the same token, I'm not turning up useful results with _global.myText
          in the Var (or Variable) field. So ...

          This workaround does it. In frame 1 of your movie clip, use the Actions
          panel to type this:

          var myText = _global.myText;

          ... then simpy put myText in the Var (or Variable) field. Finally, put
          _global.myText = "marzipan"; in frame 1 of the main timeline, and you should
          find that all the batons get passed. In other words, "marzipan" should
          appear in the text field, no matter how many times you copy that same movie
          clip on the main timeline.

          How is this working? The text fields Var (or Variable) field targets a
          variable named myText in the timeline in which it appears (namely, its movie
          clip parent). The movie clip parent *declares* a "local copy" of that
          variable and sets its value to whatever _global.myText is. That last bit --
          _global.myText -- could be set anywhere, but it happens to be set in frame 1
          of the main timeline.

          This approach is a bit contrived, but it works.

          Here's the newer approach (which, by the way, is still pretty old --
          several versions of Flash ago). Start a new FLA, use the Text tool to
          create a text field. Make it dynamic, as before, and instead of giving it a
          Var (or Variable) value, use the Property inspector to give it an instance
          name (yup, just like a movie clip instancen name). This instance name lets
          you target this particular field with code. Let's say you give it the
          instance name myTextField.

          Now select that text field and convert it to a movie clip. Use the
          Property inspector to give that movie clip an instance name (say,
          myMovieClip). Now drag a couple additional copies of that movie clip to the
          Stage. Give the other two unique instance names, such as myMovieClip2 and
          myMovieClip3.

          In frame 1 of the main timeline, use this:

          myMovieClip.myTextField.text = "marzipan";
          myMovieClip2.myTextField.text = "marzipan";
          myMovieClip3.myTextField.text = "marzipan";

          Test your SWF, and the word marzipan will appear in all three. Is this
          easier? Meh, six of one, half a dozen of the other. To me, this approach
          is a lot more straightforward. Each object is targettable via its own
          unique "address," and the text content of each text field is under your
          explicit control.

          You could use a for() loop to cycle through each of these nested text
          fields and update their text content any time you please. It's not as
          "automatic" as using the _global object and the slightly complex
          baton-passing as before, but at least you know what's available to you.

          > I am just trying to figure out what the _global term does.

          This is nothing more than a special object in ActionScript 1.0 and 2.0
          (not 3.0!) that stands outside the normal hierarchy of your objects. Any
          scope can "see" this object simply by referencing _global. This object is
          dynamic, which means you can add properties to it at will (properties are
          essentially variables).

          Let me know if that does it for you.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: TARGETTING GLOBAL TEXTFIELDS
            SLMHILL Level 1
            Thanks for all your wonderful info. I will definately be able to follow this, but need time to test it out.

            I can program buttons, so I have figured out how to construct target paths to get one movieclip to communicate with another one, but my remaining quandaries in this area were just how to use the dynamic textfields with the _global command.

            Flash can be a difficult program to teach yourself, but very interesting too!

            Happy Christmas!