2 Replies Latest reply on Jun 12, 2011 4:35 PM by killermelga2

    button pressed, not held

    killermelga2

      im currently making a game, and the game has some powerups, that when picked are stored.

       

      the problem is that, if i have, say, 3 shields, e press the matching button and the 3 shields are used instead of one.

      i found out its because i was checking the button pressing every frame, so i created a flag that checks if a button is corrently being pressed.

       

      here's the code

       

      class Ship extends MovieClip
      {
         
          function onEnterFrame()
          {
           
              if (Key.getAscii() == ord('1') && button_pressed == false && _root.menu_poderes.num_vida.text > 0)
              {
                  _root.menu_poderes.num_vida.text --;
                  _root.ship.update_vida(100-_root.ship.vida);
              }
                 
              if (Key.getAscii() == ord('2') && button_pressed == false && _root.menu_poderes.num_escudo.text > 0)
              {
                  _root.ship.escudo._alpha = 100;
                  _root.ship.escudo._visible = true;
                  _root.menu_poderes.num_escudo.text --;
              }
             
              if (Key.getAscii() == ord('3') && button_pressed == false && _root.menu_poderes.num_nuke.text > 0)
              {
                  _root.menu_poderes.num_nuke.text --;
                  for (var i in _root.ship.vec_inimigos)
                  {
                      _root.ship.vec_inimigos[i].explode();
                  }
              }
             
              if (Key.getAscii() == ord('1') || Key.getAscii() == ord('2') || Key.getAscii() == ord('3'))
              {
                  button_pressed = true;
              }
              else
              {
                  button_pressed = false;
              }
          }
      }

       

       

      with this code, when i press the button, its ok, it only uses 1 powerup, the thing is that sometimes (not always) when i release the button, it uses another powerup...

       

      also, supposedly, if i was pressing 1, I shouldnt be able to use powerup 2, but sometimes it happens (i press 1 and keep it pressed, then press 2, and the powerup 2 is used, and it shouldnt)

        • 1. Re: button pressed, not held
          Ned Murphy Adobe Community Professional & MVP

          I don't really follow what your problem is or what in your code represents what you are calling powerups, so if what I offer here makes no sense, then I can only say I tried to help.  If things are happening when you say the shouldn't, it might be because of the way you have the code arranged.

           

          What you might try for one of the problems is to take that last conditional you have and place all the first three within the first conditional.  That will help by only allowing the processing when the keys are detected.

           

          If the second problem you mention is saying that only one of those first three conditions shouyld be actve at a time, then you should probably tie them together with else controls.

           

          So what I'm saying in summary is along the lines of changing it to be...

           

              function onEnterFrame()
              {
                   if (Key.getAscii() == ord('1') || Key.getAscii() == ord('2') || Key.getAscii() == ord('3'))
                  {

                       if (Key.getAscii() == ord('1') && button_pressed == false && _root.menu_poderes.num_vida.text > 0)
                       {
                           _root.menu_poderes.num_vida.text --;
                           _root.ship.update_vida(100-_root.ship.vida);
                       }

                       else if (Key.getAscii() == ord('2') && button_pressed == false && _root.menu_poderes.num_escudo.text > 0)
                       {
                           _root.ship.escudo._alpha = 100;
                           _root.ship.escudo._visible = true;
                           _root.menu_poderes.num_escudo.text --;
                       }

                       else if (Key.getAscii() == ord('3') && button_pressed == false && _root.menu_poderes.num_nuke.text > 0)
                      {
                           _root.menu_poderes.num_nuke.text --;
                           for (var i in _root.ship.vec_inimigos)
                           {
                               _root.ship.vec_inimigos[i].explode();
                           }
                       }
                      

                      button_pressed = true;
                  }
                  else
                  {
                      button_pressed = false;
                  }
              }

          • 2. Re: button pressed, not held
            killermelga2 Level 1

            it didnt solve the problem :s

             

            now i found out this

             

            imagine i use powerup 1,

            i can only use another powerup after i move the ship.

             

            and, if i move the ship while pressing the 1 button, i can still use 2 powerups at the same time..