10 Replies Latest reply on Oct 5, 2010 5:19 PM by saratogacoach

    Pausing a function in AS3

    saratogacoach Level 1

      Hi,

       

      I have beginner's skills in AS3 and have been trying out a sample script in Flash CS5 to create an animation. The script works fine, but I am not sure how to give the user the ability to pause the animation by clicking on the screen and then resuming the animation by clicking again (if this toggling is even possible for a click event handler to do). Is there a way to pause/continue a function by clicking the player screen?

       

      Any suggestions where to look for this in the AS3 reference or sample files would be appreciated.

       

      Thanks for your help.

       

      Kind Regards,

        • 1. Re: Pausing a function in AS3
          Ned Murphy Adobe Community Professional & MVP

          Here is a scheme for the clicking interaction...

           

          var isPlaying:Boolean = true;

           

          stage.addEventListener(MouseEvent.MOUSE_UP, manageAnimation);

           

          function manageAnimation(evt:MouseEvent):void {
          if(isPlaying){
            // stop playing
          } else {
            // start playing
          }
          isPlaying = !isPlaying;
          }

           

          As far as the code for stopping/playing goes, that all depends on how you make the animations work.  You cannot pause a function in the middle of executing, but you can put in conditions that you can check for (as in if(condition).... ) to inhibit certain code from executing, or you can stop calling the function if it is repeatedly being called.

          1 person found this helpful
          • 2. Re: Pausing a function in AS3
            saratogacoach Level 1

            Hi Ned,

             

            Thanks for this helpful explanation. I can experiment with the concepts, try to get something to work for this particular function. Or maybe there is some general way to allow the user to click on the player screen and pause/continue?

             

            Kind Regards,

            • 3. Re: Pausing a function in AS3
              Ned Murphy Adobe Community Professional & MVP

              The pause/play functionality depends on the design approach.

              • 4. Re: Pausing a function in AS3
                saratogacoach Level 1

                Hi Ned,

                 

                Here's the sample script that is called for the animation:

                 

                package {
                    import flash.display.Bitmap;
                    import flash.display.BitmapData;
                    import flash.display.Sprite;
                    import flash.events.Event;
                    import flash.utils.getTimer;
                    [SWF(width=800, height=600, backgroundColor=0x000000)]   
                    public class NoiseTest extends Sprite {
                        private var _bitmapData:BitmapData;       
                        public function NoiseTest() {
                            _bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
                            makeNoise();
                            addChild(new Bitmap(_bitmapData));
                            addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);
                        }

                            private function makeNoise():void {

                 

                            _bitmapData.noise(getTimer(), 100, 255, 7, true);

                        }

                        private function onSpriteEnterFrame(event:Event):void {

                 

                            makeNoise();
                        }

                     }

                 

                }

                 

                In this example, is there a way to add a pause/continue into the script when the user clicks on the player screen?

                 

                Kind Regards,

                • 5. Re: Pausing a function in AS3
                  Ned Murphy Adobe Community Professional & MVP

                  You've hit me in one of my weak spots... classes.  I don't want to chance adding code to yours for lack of being sure I'm implementing it correctly, but maybe you can convert my non-class version of it below.  The solution is divided by the comment line, where if you click the movie anywhere it will stop or play depending on which mode it is in.  If you paste all the following into the timeline of a new file you can see it at work...

                   

                  var _bitmapData:BitmapData;       
                  _bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
                  addChild(new Bitmap(_bitmapData));

                  addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame);


                  function makeNoise():void {
                              _bitmapData.noise(getTimer(), 100, 255, 7, true);
                  }

                  function onSpriteEnterFrame(event:Event):void {
                              makeNoise();
                  }

                   

                  //  one way to control the playing.... ---------------------------------------------

                   

                  var itsNoisy:Boolean = true;

                   

                  stage.addEventListener(MouseEvent.MOUSE_UP, manageNoise);

                   

                  function manageNoise(evt:MouseEvent):void {
                       if(itsNoisy){
                           removeEventListener(Event.ENTER_FRAME, onSpriteEnterFrame)
                       } else {
                           addEventListener(Event.ENTER_FRAME, onSpriteEnterFrame)
                       }
                       itsNoisy = !itsNoisy;
                  }

                  • 6. Re: Pausing a function in AS3
                    saratogacoach Level 1

                    Hi Ned,

                     

                    Thanks. That works! I can also do some further studying to learn more.

                     

                    Your help is much appreciated.

                     

                    Kind Regards,

                    • 7. Re: Pausing a function in AS3
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome.

                      • 8. Re: Pausing a function in AS3
                        saratogacoach Level 1

                        Hi Ned,

                         

                        While the original question is successfully answered, I do have another question: can pixel size in the bitmapData.noise method be changed (made larger)? For the eLearning project that I am working on, it would be better for the noise to be constructed of 4 x 4 pixel black and white dots. From reading in the scripting reference, I cannot see any way to create this noise animation using multi-pixels in this function, but maybe there is another function to do this? Any suggestions would be welcome.

                         

                        Kind Regards,

                        • 9. Re: Pausing a function in AS3
                          Ned Murphy Adobe Community Professional & MVP

                          It's usually better to start a new posting so that the 0 replies stands out and gets attention more reliably. But here's one approach to a solution (assuming it normally outputs 1x1 pixel noise)...  just change the first few lines of what I offered earlier into...

                           

                          var _bitmapData:BitmapData;

                          var bDHolder:Sprite = new Sprite();


                          _bitmapData = new BitmapData(stage.stageWidth/4, stage.stageHeight/4);

                           

                          bDHolder.addChild(new Bitmap(_bitmapData));
                          bDHolder.scaleX = bDHolder.scaleY = 4;

                           

                          addChild(bDHolder);

                          1 person found this helpful
                          • 10. Re: Pausing a function in AS3
                            saratogacoach Level 1

                            Hi Ned,

                             

                            Thanks again for your excellent solution!

                             

                            I also appreciate your suggestion to start a new thread and plan to do so next time I post a changed topic.

                             

                            Kind Regards,