9 Replies Latest reply on Jan 28, 2010 10:10 AM by Ned Murphy

    Having to unpress spacebar and then press it again.

    TheEricBraun

      Hey. Im making a rock band 2 drum style game and i've come across a problem. Whenever theres 2+ notes together, all i have to do is hold down the button ( in this case space ) and it hits both of them without any penalty (which i havnt come across yet). But what i would liek it to do is that when u come across these notes you have to release space then press it again or else the note wont hit. Here is the code i am currently using.

       

      onClipEvent(enterFrame){
      if(_root.hitspace.hitTest(this)&&(Key.isDown(key.SPACE))){
        _root.score+=50;
        _root.streak+=1;
        unloadMovie(this)
      }
      if(_root.spacehitbad.hitTest(this)){
        _root.score-=50;
        _root.streak=0;
        unloadMovie(this)
      }
      }

       

      Where hitspace is the part where the note has to come in contact with when u hit the note. and spacehitbad is the back where if the note hits that it means you have missed the note. If anybody could please help me it would be much appreciated. Thanks

       

      Eric

        • 1. Re: Having to unpress spacebar and then press it again.
          Ned Murphy Adobe Community Professional & MVP

          I saw this the other day in your first attempt to get help and my brain ended up twisting a bit, so I moved on.  The concept seems good at the start, so I can't remember why my brain lost it--maybe I was thinking beyond what you showed, or maybe I am missing something this time around.

           

          My idea is that you need to have some variable that you include in your logic that indicates whether or not the space key is in use already.  Let's call it ' spaceInUse '. It would start off as a false value.  When you press the space key down, the code tests for !spaceInUse.  Passing that conditional, it sets the spaceInUse value to true.  Then you need to have a keyup detection that listens for the space key being released, and that would reset the spaceInUse to a false.

           

          So as long as you hold the space key down, the spaceInUse value should remain true, which when used for your conditional checkings, should help to recognize when it should have been released first.

           

          Something along the lines of...

           

          var spaceInUse = false;

           

          if(_root.hitspace.hitTest(this)&&(Key.isDown(key.SPACE)  && !spaceInUse)){
            _root.score+=50;
            _root.streak+=1;
            unloadMovie(this);

             spaceInUse = true;
          }

           

          var keyListener:Object = new Object();

          keyListener.onKeyUp = function() {
              if(Key.getCode() == 32){ // Space key
                 spaceInUse = false;
              }
          }
          Key.addListener(keyListener);

          1 person found this helpful
          • 2. Re: Having to unpress spacebar and then press it again.
            TheEricBraun Level 1

            Hmm... For some reason it would just do the exact same thing it was just doing. Do i have to set an actionscript for the keyframe too?

            • 3. Re: Having to unpress spacebar and then press it again.
              Ned Murphy Adobe Community Professional & MVP

              Actually, you should revise all your code so that it lives in the timeline and is not attached to any objects... makes it easier to find the code and prepares you for moving on to AS3--timeline coding is better practice.  For the code I offered, the new stuff should be on the timeline...

               

              var spaceInUse = false;

               

              var keyListener:Object = new Object();

              keyListener.onKeyUp = function() {
                  if(Key.getCode() == 32){ // Space key
                     spaceInUse = false;
                  }
              }
              Key.addListener(keyListener);

               

              I am not sure how that spaceInUse variable relates to code placed on objects, meaning you may have to use a _root reference to target it.

              • 4. Re: Having to unpress spacebar and then press it again.
                TheEricBraun Level 1

                Sorry but im still not quite understanding. So i keep the old code on the notes that u provided me with earlyer? And then this new stuff gets put onto the keyframe?

                • 5. Re: Having to unpress spacebar and then press it again.
                  Ned Murphy Adobe Community Professional & MVP

                  I don't think I provided you with any code earlier, but as I said, the code I just provided goes in the timeline.  Select a keyframe and enter the code in the Actions panel.

                  1 person found this helpful
                  • 6. Re: Having to unpress spacebar and then press it again.
                    TheEricBraun Level 1

                    So

                    var spaceInUse = false;

                     

                    if(_root.hitspace.hitTest(this)&&(Key.isDown(key.SPACE)  && !spaceInUse)){
                      _root.score+=50;
                      _root.streak+=1;
                      unloadMovie(this);

                       spaceInUse = true;
                    }

                     

                    var keyListener:Object = new Object();

                    keyListener.onKeyUp = function() {
                        if(Key.getCode() == 32){ // Space key
                           spaceInUse = false;
                        }
                    }
                    Key.addListener(keyListener);

                     

                    goes on the note?

                     

                    and

                    var spaceInUse = false;

                     

                    var keyListener:Object = new Object();

                    keyListener.onKeyUp = function() {
                        if(Key.getCode() == 32){ // Space key
                           spaceInUse = false;
                        }
                    }
                    Key.addListener(keyListener);

                     

                    goes on the keyframe? Or am i just not getting it?

                    • 7. Re: Having to unpress spacebar and then press it again.
                      Ned Murphy Adobe Community Professional & MVP

                      Yes, you are just not getting it.  The code you had on whatever you are calling notes will stay where it is, but it will be modified as I indicated earlier to add the _root.spaceInUse part of the if statement.  The new code I offered...(I'm copying it from your last message so it's hopefully what I offered)...

                       

                      var spaceInUse = false; // this is only declared once, here, but is used elsewhere

                       

                      var keyListener:Object = new Object();

                      keyListener.onKeyUp = function() {
                          if(Key.getCode() == 32){ // Space key
                             spaceInUse = false;
                          }
                      }
                      Key.addListener(keyListener);

                       

                       

                      goes on the main timeline on an actions layer that extends the full length of your timeline.

                      1 person found this helpful
                      • 8. Re: Having to unpress spacebar and then press it again.
                        TheEricBraun Level 1

                        Alright i got it. now what about this _root.spaceInUse part. Where do i add that and what does it really mean?

                        • 9. Re: Having to unpress spacebar and then press it again.
                          Ned Murphy Adobe Community Professional & MVP

                          I don't want to repeat myself, so go back thru my responses and you'll find how I showed what its purpose is.