8 Replies Latest reply on Apr 23, 2006 3:33 AM by blemmo

    delaying execution of an onPress function

    Level 7
      mc.onPress=function(){interval=setInterval(myFunction(this),delay)};

      In this code myFunction(this) executes immediately irrespective the delay
      value.
      How do I set it to execute with a delay?


        • 1. Re: delaying execution of an onPress function
          Level 7
          aa wrote:

          > mc.onPress=function(){interval=setInterval(myFunction(this),delay)};
          >
          > In this code myFunction(this) executes immediately irrespective the delay
          > value.
          > How do I set it to execute with a delay?

          When you use the () operator, you are making a function call right away. So
          in your code, the function is called right away in the setInterval
          statement and the return value of "myFunction(this)" is passed to
          setInterval as its first argument, that's clearly NOT what you want to do.

          Try this instead:
          mc.onPress=function(){interval=setInterval(myFunction, delay, this)};

          In this case, the first argument to setInterval is a function reference to
          "myFunction", which will be called after "delay" milliseconds with an
          argument of "this". Check your actionscript dictionnary for more detail.

          Tim.

          PS: with a call like this, you may run into scope issues, beware (take a
          look at the Delegate class, it may help).


          • 2. Re: delaying execution of an onPress function
            blemmo Level 1
            Try this:
            mc.onPress=function(){
            interval=setInterval(myFunction,delay,this);
            };
            Parameters for the function are placed after the interval delay, otherwise the function gets executed directly (as you noticed).

            greets,
            blemmo
            • 3. Re: delaying execution of an onPress function
              Peter Lorent Level 2
              You call the function immediately. Pass the paremeter(s) to the function as the third parameter of the setInterval:
              mc.onPress=function(){interval=setInterval(myFunction(),delay,this)};
              • 4. Re: delaying execution of an onPress function
                Peter Lorent Level 2
                Wow! If that line of code doesn't work!
                • 5. Re: delaying execution of an onPress function
                  t-juice-t
                  Well, I'm not that great at ActionScript, so a bunch of this has gone right over my head, but if I wanted to delay the function from occuring, I would, rather than actually delaying it, delay all of my internal functions:

                  Button:
                  on(press) {
                  _root.my_movie_clip.switched = true;
                  }

                  Movie Clip:
                  onClipEvent(load) {
                  timer = 0;
                  }
                  onClipEvent(enterFrame) {
                  if(switched == true) {
                  timer = timer + 1;
                  if(timer >= 5) {
                  timer = 0;
                  (function that you would have put on button)
                  }
                  }
                  }

                  Again, this is but a simple way to do it, as I'm not very accomplished at ActionScript yet, and I assume that it will not hold up to some of the things that you need to do, but it's something
                  • 7. Re: delaying execution of an onPress function
                    Level 7
                    I guess having this on EnterFrame might slow down the game
                    setInterval should be less of a burden

                    "t-juice-t" <webforumsuser@macromedia.com> wrote in message
                    news:e2ei4a$bkj$1@forums.macromedia.com...
                    > Well, I'm not that great at ActionScript, so a bunch of this has gone
                    right
                    > over my head, but if I wanted to delay the function from occuring, I
                    would,
                    > rather than actually delaying it, delay all of my internal functions:
                    >
                    > Button:
                    > on(press) {
                    > _root.my_movie_clip.switched = true;
                    > }
                    >
                    > Movie Clip:
                    > onClipEvent(load) {
                    > timer = 0;
                    > }
                    > onClipEvent(enterFrame) {
                    > if(switched == true) {
                    > timer = timer + 1;
                    > if(timer >= 5) {
                    > timer = 0;
                    > (function that you would have put on button)
                    > }
                    > }
                    > }
                    >
                    > Again, this is but a simple way to do it, as I'm not very accomplished at
                    > ActionScript yet, and I assume that it will not hold up to some of the
                    things
                    > that you need to do, but it's something
                    >


                    • 8. Re: delaying execution of an onPress function
                      blemmo Level 1
                      Why not use the interval? It would only execute if the button was pressed. The other code would have to run constantly in the background, which is more work for the player than the interval executing when it's needed. Of course you had to stop it in the function that gets called.

                      greets,
                      blemmo