0 Replies Latest reply on May 17, 2007 10:15 AM by ChrisFlynn

    keyListener within a class

    ChrisFlynn Level 1
      Hi all

      I tried creating a class and within this class, I have a object that acts as a key listener. Looks like this:

      class Character extends MovieClip {
      var targetMC:MovieClip;
      function Character(targetMC:MovieClip) {
      this.targetMC = targetMC;
      createListener();
      }
      function createListener() {
      var keyListener:Object = new Object();
      keyListener.onKeyDown = function() {
      trace("DOWN -> Code: "+Key.getCode()+"\tACSII: "+Key.getAscii()+"\tKey: "+chr(Key.getAscii()));
      if (Key.isDown(68)) {
      //if d
      trace("d");
      moveForward();
      } else if (Key.isDown(65)) {
      //if a
      trace("a");
      moveBack();
      }
      };
      Key.addListener(keyListener);
      }
      function moveForward() {
      trace("moveForward, x = "+this.targetMC._x);
      this.targetMC._x += 5;
      }
      function moveBack() {
      trace("moveBack, x = "+this.targetMC._x);
      this.targetMC._x -= 5;
      }
      }

      Now what's happening is, when I press any key on the keyboard, the trace("DOWN -> Code: "+... fires successfully. And if I happen to press the "d" or the "a" keys, the trace("d") and trace("a") seem to work. But the functions within those if statements are never called.

      Is it because I'm calling them from within the keyListener Object? If so, how can I call those functions from there, if at all?

      Thanks
      Chris