Loading ...
Sorry, an error occurred while loading the content.

calling function problem

Expand Messages
  • Chris Browne
    Hi, I am trying to call a function everything else works but the function (dispatch()) isn t getting called. Think it could be to do with it being called from
    Message 1 of 2 , May 31, 2007
    View Source
    • 0 Attachment


      Hi,

      I am trying to call a function everything else works but the function (dispatch()) isn't getting called. Think it could be to do with it being called from within the onKeyDown function but i'm not sure of how to address it.. delegate maybe?

      any ideas would be great

      Cheers

      Chris :)


      ----------------------------------------------------------------------------------------------------------------
      import mx.events.EventDispatcher;
      import mx.utils.Delegate;

      class But extends MovieClip {

      public var addEventListener:Function;
      public var removeEventListener:Function;
      public var dispatchEvent:Function;
      public var keyListener:Object = new Object();


      private function But() {
      trace("class int");
      EventDispatcher.initialize(this);
      key();
      }
      public function dispatch(passed) {
      trace("dispatch has been called");
      var eventObj = new Object();
      eventObj.type = "onPress2";
      eventObj.target = this;
      eventObj.key = passed;
      dispatchEvent(eventObj);
      }


      public function key() {
      Key.addListener(keyListener);
      keyListener.onKeyDown = function() {
      PROBLEM->  dispatch(Key.getCode())  <-THIS LINE DOESN'T WORK :(
      trace(Key.getCode());
      };
      }

      public function onPress() {
      dispatch(this);
      }

      }//end of class
      ----------------------------------------------------------------------------------------------------------------

      Chris Browne  chris.browne@...
      45 Fouberts Place
      London W1F 7QH

      T +44 (0)20 7025 6000



    • Hyeongjik Song
      hi, chris. use like this. public function key() { var self = this; //
      Message 2 of 2 , Jun 1, 2007
      View Source
      • 0 Attachment
        hi, chris.
        use like this.


        public function key() {
        var self = this; // << add
        Key.addListener(keyListener);
        keyListener.onKeyDown = function() {
        self.dispatch(Key.getCode()); // << add
        trace(Key.getCode());
        };
        }


        or

        public function key() {
        Key.addListener(keyListener);
        keyListener.onKeyDown = Delegate.create(this, function() {
        dispatch(Key.getCode());
        trace(Key.getCode());
        });
        }
      Your message has been successfully submitted and would be delivered to recipients shortly.