7 Replies Latest reply on Sep 27, 2008 7:31 PM by Newsgroup_User

    Buttons Vs. Keylisten: Instant Vs. Constant?

    shintashi
      I don't get why I'm experiencing this. When I program a button to move a MC, each click moves it the indicated distance, but holding down doesn't. I've tried a variety of key listening methods but it won't do what keyboard keys, like space, or arrow keys - would do. If I hold an arrow key, My target MC continues to move at pix * frames/second.

      What's the deal? Can I fix it?
      and how so?

      P.S. I had the same effect but opposite problem with gravity... when I wanted an object to "jump" instantly at the push of a button while falling, the MC didn't recognize the button part at all and just kept moving up like a helium balloon.
        • 1. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
          kglad Adobe Community Professional & MVP
          create a loop using onEnterFrame or setInterval().
          • 2. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
            Level 7
            shintashi,

            > I don't get why I'm experiencing this. When I program a button to
            > move a MC, each click moves it the indicated distance, but holding
            > down doesn't.

            What even are you handling? In ActionScript 2.0, you could arguably
            describe a button click as Button.onPress (when the mouse button is
            pressed), Button.onRelease (when the mouse button is released), or, I
            suppose, a combination of the two. For sake of discussion, let's say you're
            handling the Button.onRelease event:

            myButton.onRelease = releaseHandler;
            function releaseHandler():Void {
            // movie clip motion code here
            };

            In this case, when you click the mouse -- then release -- the
            releaseHandler() function is executed. It doesn't matter how long you hold
            down the button. Makes sense, right? Nothing about that particular event
            suggests a repetitive action.

            > I've tried a variety of key listening methods but it won't do what
            > keyboard keys, like space, or arrow keys - would do. If I hold
            > an arrow key, My target MC continues to move at pix * frames/second.

            Now I'm confused. Isn't that what you want? You want the movie clip to
            keep moving, right?

            > What's the deal? Can I fix it?
            > and how so?

            Heh. Let's see some of your sample code (just a short snippet of the
            relevant button- and/or key-related events). Almost certainly, you can fix
            this. :)


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
              shintashi Level 1
              my buttons are pretty straight forward.

              on(press)
              {
              _root.disk._y -=6;
              _root.armor._y -=6;
              _root.engine._y -=6;
              }

              I've also tried other "on(...)" versions.

              I'll try to abbreviate the Action Script for the keyboard to avoid being spammy:
              ----------

              stop();
              var mySpeed:Number = 3;

              onEnterFrame = function() {
              move_key=(Key.isDown(Key.SPACE));
              up_arrow=(Key.isDown(Key.UP));
              move_north = (move_key) && (up_arrow);
              if (Key.isDown(Key.TAB)) {
              boost = 7;
              } else {
              boost = 2;
              }
              if (move_north){
              disk._y -=mySpeed * boost;
              armor._y -=mySpeed * boost;
              engine._y -=mySpeed * boost;
              }

              }

              --------
              • 4. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
                Level 7
                shintashi,

                > I'll try to abbreviate the Action Script for the keyboard to
                > avoid being spammy:

                Okay, seeing your code helps me a bit. Now, so I'm 100% clear, what are
                you aiming for? Here's what I think you're asking, and you correct me where
                I'm wrong:

                You would like to:

                a) click the button quickly and have the movie clips move only a bit
                b) click the button and hold it ... and have the movie clips move
                continuously
                c) repeat choices a and b for keystrokes

                Is that right?


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
                  shintashi Level 1
                  that is pretty much the gist of it. I've got one MC that's basically the "remote control" and another functioning as the "toy tank". The remote control is button based, and as you said, a & b are what I would like to see for the buttons. A & B already work for the keystrokes.
                  Right now, for the remote control buttons, only A works.
                  • 6. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
                    shintashi Level 1
                    w00t! figured it out by fiddling with something I learned from a tutorial with external actionscript files. It only took me 5 or 6 pages of errors to fix... its 2:37 am... dies X_X )

                    on(press)
                    {

                    function onEnterFrame ()
                    {
                    _root.disk._y -=6;
                    _root.armor._y -=6;
                    _root.engine._y -=6;
                    }

                    }



                    on(release)
                    {

                    onEnterFrame = null;
                    }
                    • 7. Re: Buttons Vs. Keylisten: Instant Vs. Constant?
                      Level 7
                      shintashi,

                      > w00t! figured it out

                      Great! :)

                      > It only took me 5 or 6 pages of errors to fix...
                      > its 2:37 am... dies X_X )

                      That's the way to learn, though. We've all been there (at least, the
                      ones who figure it out). Your solution is close to what I was going to
                      suggest, though I would have written it differently. The main point is that
                      a press (or release) event only happens once, which kinda makes sense when
                      you really think about it. Meanwhile, an onEnterFrame event happens quite
                      often, and there's where your repetition comes from.

                      For what it's worth -- not to suggest you should fix it if it ain't
                      broken -- you might want to dig a bit more into on() and onClipEvent(), and
                      how they've been replaced in more recent (still pre-AS3) versions of
                      ActionScript:

                      http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent


                      David Stiller
                      Adobe Community Expert
                      Dev blog, http://www.quip.net/blog/
                      "Luck is the residue of good design."