2 Replies Latest reply on Nov 28, 2010 3:30 PM by Jared_Anderson

    Keyboard Event Isn't Working, and don't know why.

    Jared_Anderson

      I am creating a game where you have to use the up and down buttons on the keyboard to avoid movie clips. For some reason it won't register when i press the keys down.

       

      If anyone has any insight as to why it would be greatly appreciated.

       

      Here is my code:

       

      stop();
      var Santas:Number = 5;

       

      var keyDown1:Boolean = false;
      var keyUp1:Boolean = false;
      stage.focus = this;

       

      function randomValue()
      {
          return Math.round(Math.random() * 500);
      }

       

      stage.addEventListener(Event.ENTER_FRAME, loop);
      stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent );
      function keyDownEvent(myevent:KeyboardEvent)
      {
          if (myevent.keyCode == Keyboard.UP)
          {
              keyUp1 = true;
          }
          if (myevent.keyCode == Keyboard.DOWN)
          {
              keyDown1 = true;
          }
      }
      function keyUpEvent(myevent:KeyboardEvent)
      {
          if (myevent.keyCode == Keyboard.UP)
          {
              keyUp1 = false;
          }
          if (myevent.keyCode == Keyboard.DOWN)
          {
              keyDown1 = false;
          }
      }

       


      function loop(myevent:Event)
      {
          score_txt.text = "Santas Left: " + String(Santas);
          cane1_mc.x +=  4;
          if (cane1_mc.x > 550 && cane1_mc.y > 0)
          {
              cane1_mc.x = -10;
              cane1_mc.x = randomValue();
          }
          cane2_mc.x +=  3;
          if (cane2_mc.x > 550 && cane2_mc.y > 0)
          {
              cane2_mc.x = -10;
              cane2_mc.x = randomValue();
          }
          cane3_mc.x +=  3;
          if (cane3_mc.x > 550 && cane3_mc.y > 0)
          {
              cane3_mc.x = -10;
              cane3_mc.x = randomValue();
          }
          lake1_mc.x +=  4;
          if (lake1_mc.x > 550 && lake1_mc.y > 0)
          {
              lake1_mc.x = -10;
              lake1_mc.x = randomValue();
          }
          lake2_mc.x +=  3;
          if (lake2_mc.x > 550 && lake2_mc.y > 0)
          {
              lake2_mc.x = -10;
              lake2_mc.x = randomValue();
          }
          mountain_mc.x +=  3;
          if (mountain_mc.x > 550 && mountain_mc.y > 0)
          {
              mountain_mc.x = -10;
              mountain_mc.x = randomValue();
          }
          if (keyDown1 && santa_mc.y > 351.10)
          {
              santa_mc.y -=  2;
          }
          //trace(keydown)
          if (keyUp1 && santa_mc.y < 25.10)
          {
              santa_mc.y +=  2;
          }
          //trace(keyup)
          if (santa_mc.hitTestObject(cane1_mc))
          {
              Santas--;
              cane1_mc.x = -550;
          }
          if (santa_mc.hitTestObject(cane2_mc))
          {
              Santas--;
              cane2_mc.x = -550;
          }
          if (santa_mc.hitTestObject(cane3_mc))
          {
              Santas--;
              cane3_mc.x = -550;
          }
          if (santa_mc.hitTestObject(lake1_mc))
          {
              Santas--;
              lake1_mc.x = -550;
          }
          if (santa_mc.hitTestObject(lake2_mc))
          {
              Santas--;
              lake2_mc.x = -550;
          }
          if (santa_mc.hitTestObject(mountain_mc))
          {
              Santas--;
              mountain_mc.x = -550;
          }
          if (Santas == 0)
          {
              gotoAndStop(3);
          }
      }