5 Replies Latest reply on Jul 4, 2006 11:51 AM by Peter Lorent

    Question about visibility

    sobaka11
      I have a class:

      class My {
      var keyListener:Object = new Object();
      function My() {
      keyListener.onKeyDown = function() {
      lala();
      };
      keyListener.onKeyUp = function() {
      };
      Key.addListener(keyListener);
      }
      function lala() {
      trace("my");
      }
      }

      I want that, when user will press a key the function lala() will be called. But the function doesn't run and I don't see message "my" in Output Window.
      I have heard that I must use function Function.call() to fix this problem.
      Can you advise me how to resolve this problem?
        • 1. Re: Question --Function.call--
          Level 7
          That's correct behaviour .. as written the function 'lala' needs to be a
          method of the keyListener object to be called like that. And its not .. its
          a member of your class 'My'.

          It has nothing to do with Function.call()

          Instead, your keyListenter need to have a reference to the My object that
          owns it so it can call the lalal() method of that My object.

          eg

          class My {
          var keyListener:Object = new Object();
          function My() {
          keyListener.my = this;
          keyListener.onKeyDown = function() {
          my.lala();
          };
          keyListener.onKeyUp = function() {
          };
          Key.addListener(keyListener);
          }
          function lala() {
          trace("my");
          }
          }
          --
          Jeckyl


          • 2. Re: Question --Function.call--
            Peter Lorent Level 2
            Why create a new object (keyListener) when you already have an object (My) that can listen for key events?

            class My {

            public function My() {
            Key.addListener(this);
            }

            private function onKeyDown():Void{
            lala();
            }

            private function lala():Void {
            trace("my");
            }
            }
            • 3. Re: Question --Function.call--
              sobaka11 Level 1
              Thanks!
              • 4. Question --Function.call--
                sobaka11 Level 1
                One more question.
                I have .fla file:
                clip=createEmptyMovieClip("newroot", _root.getNextHighestDepth());
                var a:loader=new loader(clip);

                And class:

                class loader {

                var xmlNode:XMLNode;
                var xmlData:XML;
                var n:Number, m:Number;
                var k:Object;
                var clipname:MovieClip;

                function loader(clip:MovieClip) {
                clipname = clip;
                trace(clipname); //here I see the name of the clip
                xmlData = new XML();
                xmlData.ignoreWhite = true;
                xmlData.onLoad = loadXML;
                xmlData.load("doc.xml");
                }

                function loadXML(loaded) {
                if (loaded) {
                trace(clipname); //here the name is 'undefined'
                k = this;
                xmlNode = k.firstChild;
                n = Number(xmlNode.childNodes[0].firstChild.nodeValue);
                m = Number(xmlNode.childNodes[1].firstChild.nodeValue);
                //var main_obj:main=new main(n,m,clipname);
                }
                }
                }

                When I load XML data I can't see the parameter from .fla file( clipname ). What should I do to see the parameter 'clipname' in function loadXML() ?
                • 5. Re:  Question --Function.call--
                  Peter Lorent Level 2
                  Inside callback handlers class members go out of scope.
                  //class file
                  import mx.utils.Delegate;

                  class loader {

                  var xmlNode:XMLNode;
                  var xmlData:XML;
                  var n:Number, m:Number;
                  var k:Object;
                  var clipname:MovieClip;

                  function loader(clip:MovieClip) {
                  clipname = clip;
                  trace(clipname); //here I see the name of the clip
                  xmlData = new XML();
                  xmlData.ignoreWhite = true;
                  xmlData.onLoad = Delegate.create(this,loadXML);
                  xmlData.load("doc.xml");
                  }

                  function loadXML(loaded) {
                  if (loaded) {
                  trace(clipname); //here the name is 'undefined'
                  k = this;
                  xmlNode = k.firstChild;
                  n = Number(xmlNode.childNodes[0].firstChild.nodeValue);
                  m = Number(xmlNode.childNodes[1].firstChild.nodeValue);
                  //var main_obj:main=new main(n,m,clipname);
                  }
                  }
                  }
                  //end class file