4 Replies Latest reply on Jun 7, 2007 12:52 PM by crazyjoemilan

    scoping, myClass, and contextMenu

    crazyjoemilan Level 2
      I have a class where I'm defining a context menu. The odd thing is, when I call the function, I can't call any variables from outside the function. The context menu calls the function correctly, but the scoping is all wonkey. Is there a way around this?

      function checkIt() {
      TEST = "test"
      trace(TEST);
      }
      //////////// traces undefined

      function checkIt() {
      var TEST = "test"
      trace(TEST);
      }
      //////////// traces "test"

      I've tried referencing all readily available variables, but due to the way its formatted - will I be forced to use _root variables? Or is there a better way I'm not seeing?
        • 1. Re: scoping, myClass, and contextMenu
          Level 7
          Mr Helpy mcHelpson,

          > I have a class where I'm defining a context menu. The odd
          > thing is, when I call the function, I can't call any variables from
          > outside the function.

          Hrrm. Yeah, that seems wonky to me, too.

          > function checkIt() {
          > TEST = "test"
          > trace(TEST);
          > }
          > //////////// traces undefined

          Is this in your class file? Can you put the head-scratcher function in
          context of your class?

          > I've tried referencing all readily available variables, but due to
          > the way its formatted - will I be forced to use _root variables?
          > Or is there a better way I'm not seeing?

          I can't imagine having to use _root in a class. Let's see a bit more on
          how this code is collaborating with your context menu.


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


          • 2. Re: scoping, myClass, and contextMenu
            crazyjoemilan Level 2
            Hey David, thanks for taking the time.

            I think I cleaned up the code to show only whats important. I instantiated 200-1000+ of these "Checkb" objects (check boxes) depending on what the user is looking at.

            HmcH
            • 3. Re: scoping, myClass, and contextMenu
              Level 7
              Helpy,

              > Hey David, thanks for taking the time.

              Sure thing! :)

              > I think I cleaned up the code to show only whats important. I
              > instantiated 200-1000+ of these "Checkb" objects (check boxes)
              > depending on what the user is looking at.

              Okay.

              > class Checkb extends MovieClip {
              > // snip ...
              > test = new ContextMenuItem("THIS IS A TEST", checkIt);

              Here it is. The issue is, any variables or "this" references in the
              checkIt() function are scoped to checkIt(), so your TEST variable ... well,
              that's the weird part. If it doesn't already exist (in this scope anyway),
              it should be created right there, even without the var declaration. I'm
              actually not sure why TEST is undefined by the point it gets traced, but the
              whole thing is fixed with the Delegate class.

              import mx.utils.Delegate;
              class Checkb extends MovieClip {
              public var CM:ContextMenu;
              // etc.
              test = new ContextMenuItem("THIS IS A TEST", Delegate.create(this,
              checkIt));
              // etc.

              ... that scopes checkIt() to the class itself, and you'll see TEST correctly
              put the string "test" to the Output panel. This seems a bit wonky (I really
              can't think of a better word -- you nailed it!), but at least you can pass
              along the data you want.

              For grins, you may also want to make use of the two paramters send to
              your checkIt() function:

              function checkIt(obj:MovieClip, item:Object) {
              trace("obj: " + obj);
              trace("item: " + item);
              for (var prop:String in item) {
              trace(" " + prop + ": " + item[prop]);
              }
              TEST = "test";
              trace(TEST);
              }

              For me, that output the following:

              obj: _level0.instance1
              item: [object Object]
              visible: true
              enabled: true
              separatorBefore: false
              onSelect: [type Function]
              caption: THIS IS A TEST
              test

              ... where obj is clearly the movie clip itself and item is ContextMenuItem
              object.


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


              • 4. Re: scoping, myClass, and contextMenu
                crazyjoemilan Level 2
                Ahhhh fantastic. I'd always found ways to shy away from the Delegate class, but I now see it was tomfoolery. Thanks much for the help.