7 Replies Latest reply on Jul 20, 2009 12:26 AM by dalcde

    Join two If statements

    TezS56
      I want to join two if statements together and I'm unsure of the syntax.
      This is what I have but it is obviously wrong.
      if (Key.isDown(Key.RIGHT) or if (moveRight_btn.onPress){
        • 1. Re: Join two If statements
          Level 7
          Hi TezS56,

          Use a variable and the events onPress and onRelease to know that button
          is being pressed.

          var moveRight_btn_press:Boolean = false;

          moveRight_btn.onPress = function(){
          moveRight_btn_press = true;
          };

          moveRight_btn.onRelease = function(){
          moveRight_btn_press = false;
          };


          The condition is in the following way:

          if(Key.isDown(Key.SPACE) && moveRight_btn_press){};


          Full Script.

          var moveRight_btn_press:Boolean = false;

          moveRight_btn.onPress = function(){
          moveRight_btn_press = true;
          };

          moveRight_btn.onRelease = function(){
          moveRight_btn_press = false;
          };

          var objKey:Object = new Object();
          objKey.onKeyDown = function(){
          if(Key.isDown(Key.SPACE) && moveRight_btn_press){
          trace("PRESSED");
          };
          };
          Key.addListener(objKey);


          --
          Erick Souza
          www.ericksouza.com
          • 2. Re: Join two If statements
            pubchum Level 1
            if you want to see if two keys are being pressed at the same time you can use the && operator.

            if(rt_arrow.isDown && shift.isDown){
            trace("right arrow AND shift are down");
            }
            • 3. Re: Join two If statements
              pubchum Level 1
              the OR opperator is ||
              • 4. Re: Join two If statements
                TezS56 Level 1
                Thanks Erick for your quick reply. I see what you are aiming at here and I'm sure it's almost the answer but I'm being a little thick today and can't see it.
                What I am trying to do is when EITHER the right arrow key (you had space bar) OR the moveRight button is pushed an object moves across the screen. I had it working with just the right arrow key using an IF statement and I thought that if I could just add another condition (if the moveRight button was pushed) that would do the trick but the || (or) would not work to join the two statements.
                • 5. Re: Join two If statements
                  Level 7
                  I believe to have understood, see the script:


                  moveRight_btn.onPress = function(){
                  iMove = setInterval(funcMove, 50, mcInstanceName);
                  };

                  moveRight_btn.onRelease = function(){
                  clearInterval(iMove);
                  };

                  var objKey:Object = new Object();
                  objKey.onKeyDown = function(){
                  if(Key.isDown(Key.RIGHT)){
                  funcMove(mcInstanceName);
                  };
                  };
                  Key.addListener(objKey);

                  function funcMove(mc:MovieClip):Void{
                  mc._x += 20;
                  updateAfterEvent();
                  };


                  --
                  Erick Souza
                  www.ericksouza.com
                  • 6. Re: Join two If statements
                    TezS56 Level 1
                    Very elegant. It works great.....THANK YOU
                    • 7. Re: Join two If statements
                      dalcde Level 1

                      This is useful. If you want to use and, not or, you may use:

                      if(....){

                           if(...){

                           }

                      }

                       

                      But that is super complicated. You may use and: &&

                       

                      The code would be:

                      if(... && ...){

                           .....

                      }

                       

                      The code will be executed if all of the statements returns true.

                      You can put more than two statements inside, e.g.:

                      if(... && ... && ... &&...){

                           ....

                      }

                       

                      It is the same with or, which is || ( The key above the enter key X2):

                      if(...||...){

                           ...

                      }

                       

                      You can also put a lot like &&. The code will be executed once one of the statements returns true.