7 Replies Latest reply on Jan 4, 2007 7:20 AM by Newsgroup_User

    targeting dynamic instance

    MikeLB
      Hope this is a simple one - Im attaching new mc via
      _root.attachMovie("mymc", "mymc_ins"+step, _root.getNextHighestDep...
      each mc is added with mousedown and each instance name becomes mymc_ins1, mymc_ins2 as I increment step. Now I would like to scale the instantly generated mc while the mouse is down and moving - this is fine if I specify mymc_ins1 but I need to do this as soon as the mc is added - how can I refer to a dynamically assigned instance - I know its wrong but sort of like this
      "mymc_ins"+step._width

      Out of me depth as ever

      Thanks in advance
      MikeB
        • 1. Re: targeting dynamic instance
          Level 7
          MikeB,

          > how can I refer to a dynamically assigned instance - I
          > know its wrong but sort of like this "mymc_ins"+step._width

          The key here is to make sure you're providing an actual object reference
          as your parameter, rather than a string. So rather than
          "mymc_ins"+step._width (which concatenates to a string), either use the
          eval() function or the array access operator, as described here ...

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

          Better yet -- this is the way I'd go in this situation -- use the value
          returned by the MovieClip.attachMovie(), which is precisely the object
          reference you need. Just store the value in a variable, which will become
          your stand-in movie clip.

          var mc:MovieClip = _root.attachMovie("mymc", "mymc_ins"+step, //etc.
          mc._xscale = ???
          mc._yscale = ???


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


          • 2. Re: targeting dynamic instance
            Peter Lorent Level 2
            David hi, best wishes and a very busy new year...
            • 3. Re: targeting dynamic instance
              MikeLB Level 1
              Thanks ever so much David - thats just what I needed, tried both ways and all fine - just one other little question if you don't mind, I'm creating the mc on a mousedown/event/function and then scaling the mc on a mousemove as follows
              mouseListener.onMouseDown = function() {
              var mc:MovieClip = _root.attachMovie("my etc...
              isdrawing = true;
              }
              and then:-
              mouseListener.onMouseMove = function() {
              if (isDrawing) {
              mc._xscale = etc
              mc._yscale = etc
              }
              this only seems to work if I place the mousemove/scale function within the mousedown, why can't the mousemove function pick up mc? as is structured above

              Thanks again for your answer - as you can tell we're just starting with AS.
              • 4. Re: targeting dynamic instance
                Level 7
                LuigiL,

                > David hi, best wishes and a very busy new year...

                Hi, LuigiL! Good to hear from you. :) Thanks for the greetings! Best
                to you, too. Hope you had a good New Year celebration. 2007 is looking
                good already.


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


                • 5. Re: targeting dynamic instance
                  Level 7
                  MikeLB,

                  > I'm creating the mc on a mousedown/event/function and
                  > then scaling the mc on a mousemove as follows
                  > mouseListener.onMouseDown = function() {
                  > var mc:MovieClip = _root.attachMovie("my etc...
                  > isdrawing = true;
                  > }

                  Aha.

                  > and then:-
                  > mouseListener.onMouseMove = function() {
                  > if (isDrawing) {
                  > mc._xscale = etc

                  Okay, this is a scope issue. In your onMouseDown event, you've assigned
                  a function literal that declares a variable mc. That variable is scoped to
                  the function only, as is the case any time you declare a variable inside a
                  function. As soon as the function completes execution, the variable is
                  gone. That's why the onMouseMove-assigned function isn't able to see it.

                  Declare the variable outside the scope of either function first, then it
                  will be available to both.

                  var mc:MovieClip;
                  var isDrawing:Boolean;
                  mouseListener.onMouseDown = function() {
                  mc = _root.attachMovie(//etc.);
                  isDrawing = true;
                  }
                  mouseListener.onMouseMove = function() {
                  if (isDrawing) { // etc.
                  }


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


                  • 6. Re: targeting dynamic instance
                    MikeLB Level 1
                    Thanks once again David - thats made it perfectly clear, better locate one of those 'for dummies' books... unfortunatley our projects are always a baptism of fire.

                    Kind Regards
                    MikeB
                    • 7. Re: targeting dynamic instance
                      Level 7
                      MikeB,

                      > Thanks once again David - thats made it perfectly clear, better
                      > locate one of those 'for dummies' books...
                      > unfortunatley our projects are always a baptism of fire.

                      You betcha! Good luck with it. :)


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