1 Reply Latest reply on Jun 28, 2008 12:24 PM by clbeech

    trouble with key interactions within a class

    Level 7
      Hi, I've created a class that extends the movieclip class and in their I
      have a couple of listeners waiting for key strokes. I'm also setting up an
      array with 2 values in there.

      class DrawOnMe extends MovieClip {
      private var KeyDwnList:Object = new Object();
      private var KeyUpList:Object = new Object();
      private var VitEtch:Array = new Array();

      function DrawOnMe() {
      KeyDwnList.onKeyDown = KeyDownTrap;
      Key.addListener(KeyDwnList);
      KeyUpList.onKeyUp = KeyUpTrap;
      Key.addListener(KeyUpList);
      VitEtch["x"] = _width/2;
      VitEtch["y"] = _height/2;
      }


      private function KeyDownTrap():Void {
      switch (Key.getCode()) {
      case 37 :
      trace("x: "+VitEtch.x+" - y:"+VitEtch.y);
      break;
      default :
      break;
      }
      }

      private function KeyUpTrap():Void {
      trace("Stop it");
      }


      Ok, so press the left arrow key and it gets detected however the VitEtch
      array comes back undefined. I know this is some sort of scope issue, but for
      the life of me I can't figure out what to do about it. I thought that the
      listener would be apart of the movieclip that the class is assigned to and
      therefor that the property VitEtch should be readily accessible, am I wrong.
      Do listeners get created "somewhere" else when it comes to classes.

      Please help, this is driving me crazy.

      Thanks
      james


        • 1. Re: trouble with key interactions within a class
          clbeech Level 3
          the problem really isn't about scope here - it's about the syntax adding the element to the array: VitEtch["x"] - the [ ] are array access operators and you must specify an index by number used in this manner. it appears you are wanting to store the x/y values - you could also do this with an Object rather than an array. However, to store the two values use:
          VitEtch[0] = _width/2;
          VitEtch[1] = _height/2;

          and then refer to them by index number as in:
          trace("x: "+VitEtch[0]+" - y:"+VitEtch[1]);

          OR - use and Object reference instead of the array:
          private var VitEtch:Object = new Object();

          ..and then use:
          VitEtch.x = _width/2;
          VitEtch.y= _height/2;
          trace("x: "+VitEtch.x+" - y:"+VitEtch.y);