3 Replies Latest reply on Feb 19, 2007 7:52 AM by Newsgroup_User

    variable setInterval

    Level 7
      I have a button that when pressed moves a graphic on screen.

      I would like it so that it moves only 1 pixel if you click it once, but if
      you hold it down it moves at an increasing pace. I have used a setinterval
      to move it constantly when the button is held down, but would like to add
      this acceleration if possible.

      Any ideas?

      Thanks

      Dave


        • 1. Re: variable setInterval
          Level 7
          Dave,

          > I have a button that when pressed moves a graphic on
          > screen.

          Okay.

          > I would like it so that it moves only 1 pixel if you click it
          > once, but if you hold it down it moves at an increasing
          > pace.

          That should be two hard. You've got two goals here: a) responding to a
          click (or mouse down) event and b) increasing the value of a variable (or
          maybe two).

          > I have used a setinterval to move it constantly when the
          > button is held down, but would like to add this acceleration
          > if possible.

          You can almost certainly continue using your setInteval() approach. I
          imagine you have *something* like the following:

          var id:Number = setInterval(move, 50);
          function move():Void {
          some.path.to.a.clip._x++;
          }

          ... give or take, of course. Maybe you're changing the clip's _y property;
          maybe both. The ++ operator increases a value by one. It's the same as
          saying this ...

          _x = _x + 1;

          ... which, in turn, is the same as saying this ...

          _x += 1;

          ... and it's this last version that may make the most sense for you. Rather
          than using += with a hard-coded 1, you could use a variable instead.

          _x += acceleration;

          Use the MovieClip.onPress event or, say, the Mouse.onMouseDown event to
          set the value of acceleration to a higher number than 1, then listen for
          onRelease or onMouseUp to set acceleration back to 1. You can either set
          acceleration to something like 10, which would make a sudden jump to a
          faster animation ... or you could increase the value of acceleration in that
          same setInterval() loop. I could see, for example, using the mouse down/up
          handler to set an arbitrarily named isClicked variable that keeps track of
          the mouse's state as either clicked or not. Your setInterval() function
          could check that variable to see if it should increase the acceleration
          variable. Something like this ...

          var isClicked:Boolean = false;
          var acceleration:Number = 1;

          var id:Number = setInterval(moveBall, 50);

          function moveBall():Void {
          ball._x += acceleration;
          if (isClicked) {
          acceleration += 0.25;
          }
          }

          var listener:Object = new Object();
          listener.onMouseDown = function():Void {
          isClicked = true;
          }
          listener.onMouseUp = function():Void {
          isClicked = false;
          acceleration = 1
          }
          Mouse.addListener(listener);


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


          • 2. Re: variable setInterval
            kglad Adobe Community Professional & MVP
            use setInterval with a variable speed.

            in you onPress, move your graphic by 1 pix and define a setInterval() with your variable,

            in your called function that moves your graphic, clearInterval increase that variable and define a new setInterval with that variable.

            in your button's onRelease, use clearInterval.

            or, you can just use an easing equation to move your graphic.
            • 3. Re: variable setInterval
              Level 7
              Thanks for your replies guys, very useful

              "David Stiller" <stiller@quip-remove-.net> wrote in message
              news:ercepv$bva$1@forums.macromedia.com...
              > Dave,
              >
              >> I have a button that when pressed moves a graphic on
              >> screen.
              >
              > Okay.
              >
              >> I would like it so that it moves only 1 pixel if you click it
              >> once, but if you hold it down it moves at an increasing
              >> pace.
              >
              > That should be two hard. You've got two goals here: a) responding to
              > a click (or mouse down) event and b) increasing the value of a variable
              > (or maybe two).
              >
              >> I have used a setinterval to move it constantly when the
              >> button is held down, but would like to add this acceleration
              >> if possible.
              >
              > You can almost certainly continue using your setInteval() approach. I
              > imagine you have *something* like the following:
              >
              > var id:Number = setInterval(move, 50);
              > function move():Void {
              > some.path.to.a.clip._x++;
              > }
              >
              > ... give or take, of course. Maybe you're changing the clip's _y
              > property; maybe both. The ++ operator increases a value by one. It's the
              > same as saying this ...
              >
              > _x = _x + 1;
              >
              > ... which, in turn, is the same as saying this ...
              >
              > _x += 1;
              >
              > ... and it's this last version that may make the most sense for you.
              > Rather than using += with a hard-coded 1, you could use a variable
              > instead.
              >
              > _x += acceleration;
              >
              > Use the MovieClip.onPress event or, say, the Mouse.onMouseDown event to
              > set the value of acceleration to a higher number than 1, then listen for
              > onRelease or onMouseUp to set acceleration back to 1. You can either set
              > acceleration to something like 10, which would make a sudden jump to a
              > faster animation ... or you could increase the value of acceleration in
              > that same setInterval() loop. I could see, for example, using the mouse
              > down/up handler to set an arbitrarily named isClicked variable that keeps
              > track of the mouse's state as either clicked or not. Your setInterval()
              > function could check that variable to see if it should increase the
              > acceleration variable. Something like this ...
              >
              > var isClicked:Boolean = false;
              > var acceleration:Number = 1;
              >
              > var id:Number = setInterval(moveBall, 50);
              >
              > function moveBall():Void {
              > ball._x += acceleration;
              > if (isClicked) {
              > acceleration += 0.25;
              > }
              > }
              >
              > var listener:Object = new Object();
              > listener.onMouseDown = function():Void {
              > isClicked = true;
              > }
              > listener.onMouseUp = function():Void {
              > isClicked = false;
              > acceleration = 1
              > }
              > Mouse.addListener(listener);
              >
              >
              > David Stiller
              > Adobe Community Expert
              > Dev blog, http://www.quip.net/blog/
              > "Luck is the residue of good design."
              >