5 Replies Latest reply on Dec 2, 2009 1:19 PM by kglad

    How to delete or set a timer on a function?

    GumpsterF

      Hello. I have a function that creates a snowfall effect in Flash as2.

       

      What I want is to be able to delete the two functions that create this effect after 30 seconds or remove the attachMovie after 5 seconds.

       

      Here is the actionscript code for the snow effect;

       

      snowblow = function () {
           width = 728;
           // pixels 
           height = 90;
           // pixels 
           max_snowsize = 10;
           // pixels 
           snowflakes = 50;
           // quantity 
           for (i=0; i<snowflakes; i++) {
                t = attachMovie("snow", "snow"+i, i);
                t._alpha = 20+Math.random()*60;
                t._x = -(width/2)+Math.random()*(1.5*width);
                t._y = -(height/2)+Math.random()*(1.5*height);
                t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
                t.k = 1+Math.random()*2;
                t.wind = -1.5+Math.random()*(1.4*3);
                t.onEnterFrame = mover;
           }
      };
      mover = function() {
           this._y += this.k;
           this._x += this.wind;
           if (this._y>height+10) {
                this._y = -20;
           }
           if (this._x>width+20) {
                this._x = -(width/2)+Math.random()*(1.5*width);
                this._y = -20;
           } else if (this._x<-20) {
                this._x = -(width/2)+Math.random()*(1.5*width);
                this._y = -20;
           }
      }
      snowblow();
      

       

      First I tried inserting a timeout;

       

      setTimeout(snowblow,5000);
      

       

      That did not work, so I tried a timer function to delete the two functions;

       

      time = 5000; 
      delay = setInterval(proceed, time); 
      function proceed() { 
          clearInterval(delay); 
          delete snowblow;
          delete mover;
      
      }
      

       

      Can anyone tell me how to stop these functions after a certain amount of time (I am using 5 seconds here)?

      Maybe tell me the proper way to delete the functions?

       

      Everything I have researched says this code should work.

      I have even tried deleting the MovieClip but that did not work either.

       

      Help

        • 1. Re: How to delete or set a timer on a function?
          kglad Adobe Community Professional & MVP


          stoptime=getTimer()+5000

           

           

          snowblow = function () {
               width = 728;
               // pixels
               height = 90;
               // pixels
               max_snowsize = 10;
               // pixels
               snowflakes = 50;
               // quantity
               for (i=0; i<snowflakes; i++) {
                    t = attachMovie("snow", "snow"+i, i);
                    t._alpha = 20+Math.random()*60;
                    t._x = -(width/2)+Math.random()*(1.5*width);
                    t._y = -(height/2)+Math.random()*(1.5*height);
                    t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
                    t.k = 1+Math.random()*2;
                    t.wind = -1.5+Math.random()*(1.4*3);
                    t.onEnterFrame = mover;
               }
          };
          mover = function() {
               this._y += this.k;
               this._x += this.wind;
               if (this._y>height+10) {
          if(getTimer()>stoptime){
          this.removeMovieClip();
          } else {
                    this._y = -20;

          }

               }
               if (this._x>width+20) {
                    this._x = -(width/2)+Math.random()*(1.5*width);
                    this._y = -20;
               } else if (this._x<-20) {
                    this._x = -(width/2)+Math.random()*(1.5*width);
                    this._y = -20;
               }
          }
          snowblow();
          • 2. Re: How to delete or set a timer on a function?
            GumpsterF Level 1

            Wow, above and beyond there kglad.

            Very creative answer.

            Just as an aside, how do you delete a function?

            Like, why did the code;

             

            delete snowblow;
            

             

            not work?

             

            Forrest

            • 3. Re: How to delete or set a timer on a function?
              kglad Adobe Community Professional & MVP

              snowblow() only executes once.  it's not repeatedly called.

               

              it creates the snowflakes and sets some parameters.  in move() the snowflakes are recycled.  so, if you want the snowflakes to stop, you have to stop recycling them.  that's what i added to the code.

               

              delete snowblow;

               

              will delete the snowblow() function.  if you execute that delete before calling snowblow(), no snowflakes will be created.

              • 4. Re: How to delete or set a timer on a function?
                GumpsterF Level 1

                Got it.

                Thanks again kglad.

                Always more to learn...

                • 5. Re: How to delete or set a timer on a function?
                  kglad Adobe Community Professional & MVP

                  you're welcome.