7 Replies Latest reply: Jun 4, 2014 10:00 AM by kglad RSS

    how to stop the waterflow?

    rahex Community Member

      Hi,

      I need to make the water coming from the shower, when shower is clicked.

      And the water should stop after 5 seconds.

       

      The code is below.

       

      The water is flowing nicely. But I do not manage to stop it.

      I added a timer, which starts when the shower is clicked. And should stop the waterflow after 5 seconds have passed.

      With the lines

      if (stop = true)

      {

           break;

           trace("break");

      }

      it looks like the for loop is breaking right from the start, from the first second. But not completely, somehow 1 or 2 drops are flowing.

      Without these lines nothing is happening when the time is over (5 seconds from clicking the shower). Water is just flowing.

       

      Can someone help? How to stop the water flowing after 5 seconds and clean of the array and stage from the waterdrops?

       

      Many thanks in advance!

       

       

      import flash.display.MovieClip;

      import flash.events.Event;

      import flash.utils.Timer;

      import flash.events.TimerEvent;

       

       

      var WatertropArray: Array = new Array();

      var WaterArea01: MovieClip = new WaterArea();

      var WaterBorder01: MovieClip = new WaterBorder();

      var timer: Timer = new Timer(5000, 1);

      var stopp: Boolean;

      var i: uint;

       

      Shower.addEventListener(MouseEvent.CLICK, waterFlowStart);

       

      function waterFlowStart(event: MouseEvent): void

      {

       

                      addChild(WaterArea01);

                      WaterArea01.x = WaterArea00.x;

                      WaterArea01.y = WaterArea00.y;

                      WaterArea01.alpha = 0;

       

                      addChild(WaterBorder01);

                      WaterBorder01.x = WaterBorder00.x;

                      WaterBorder01.y = WaterBorder00.y;

                      WaterBorder01.alpha = 0;

       

                      stopp = false;

       

                      timer.addEventListener(TimerEvent.TIMER, waterFlowEnd);

                      timer.start();

       

                      addWaterdrops();

       

                      Shower.removeEventListener(MouseEvent.CLICK, waterFlowStart);

       

      }

       

       

       

      function addWaterdrops(): void

      {

       

                      for(var i: uint = 0; i < 100; i++)

                      {

                                     var waterDrop: MovieClip = new Waterdrop();

                                     addChild(waterDrop);

                                     waterDrop.x = Math.round(Math.random() * stage.stageWidth / 1.5);

                                     waterDrop.y = Math.round(Math.random() * stage.stageHeight / 3);

                                     waterDrop.alpha = 0;

                                     waterDrop.rotation = -12;

                                     WatertropArray.push(waterDrop);

                                     trace("waterdrops added");

                                     moveWaterdrops();

                      }

      }

       

      function moveWaterdrops(): void

      {

                      waterDrop00.addEventListener(Event.ENTER_FRAME, waterFlow);

      }

       

      function waterFlow(event: Event): void

      {

       

                      for(var i: uint = 0; i < WatertropArray.length; i++)

                      {

       

                                     WatertropArray[i].y += 8;

                                     WatertropArray[i].x += 5;

       

                                     //trace(i);

       

                                     if(WatertropArray[i].hitTestObject(WaterArea01))

                                     {

                                                     WatertropArray[i].alpha = Math.random();

                                                     WatertropArray[i].scaleX = Math.random();

                                                     WatertropArray[i].scaleY = WatertropArray[i].scaleX;

       

                                     }

                                     if(WatertropArray[i].hitTestObject(WaterBorder01) || WatertropArray[i].x > stage.stageWidth || WatertropArray[i].y > stage.stageHeight / 2)

                                     {

                                                     WatertropArray[i].x = Math.round(Math.random() * stage.stageWidth / 1.5);

                                                     WatertropArray[i].y = Math.round(Math.random() * stage.stageHeight / 3);

                                                     WatertropArray[i].alpha = 0;

                                     }

                                     if(stopp = true)

                                     {

                                                     break;

                                                     trace("break");

                                     }

                      }

      }

       

       

       

      function waterFlowEnd(event: TimerEvent): void

      {

                      trace("TIME OVER");

                      stopp = true;

                      stoppTrue();

      }

       

       

      function stoppTrue(): void

      {

                      for(var i: uint = WatertropArray.length; i > WatertropArray.length; i--)

                      {

                                     remove(i);

                      }

      }

       

       

       

      function remove(idx: int)

      {

                      removeChild(WatertropArray[idx]);

                      WatertropArray.splice(idx, 1);

                      trace("REMOVED");

                      removeChild(waterDrop00);

                      trace(i);

      }

        • 1. Re: how to stop the waterflow?
          kglad CommunityMVP

          don't use the assignment operator (=).  use the double equal to test for equality:

           

          if (stop == true)

          • 2. Re: how to stop the waterflow?
            rahex Community Member

            Thanks klad!

            It eliminated part of the problem.

            Now the for loop really stops.

            The waterdrops stop on screen. But they are visible.

             

            Looks like to code after the "break" command is not processed any more.

            Functions stoppTrue and remove are not executed - I do not get the trace statements. (added a trace statement for the function stoppTrue as well).

             

            How the code should be written, so only the for loop is stopped but the following functions are still executed?

            • 3. Re: how to stop the waterflow?
              kglad CommunityMVP

              remove the water drops using a for-loop that loops from end to beginning of WatertropArray

              • 4. Re: how to stop the waterflow?
                rahex Community Member

                thanks again, kglad.

                changed the for-loop and it is reaching now the last functions as well.

                but there is still a but  ... an error message.

                 

                 

                function waterFlowEnd(event: TimerEvent): void

                {

                trace("TIME OVER");

                stopp = true;

                stoppTrue();                                                       // line 106

                }

                 

                function stoppTrue(): void

                {

                for(var i: uint = WatertropArray.length-1; i >= 0; i--)

                {

                trace("stoppTrue");

                remove(i);                                                         // line 115                                                    

                }

                }

                 

                function remove(idx: int)

                {

                removeChild(WatertropArray[idx]);                   // line 123

                WatertropArray.splice(idx, 1);

                trace("REMOVED");

                //removeChild(waterDrop00);

                trace(i);

                }



                and the output panel gives the following (tested with 5 water drops, 5 items in array):

                 

                TIME OVER

                stoppTrue

                REMOVED

                0

                stoppTrue

                REMOVED

                0

                stoppTrue

                REMOVED

                0

                stoppTrue

                REMOVED

                0

                stoppTrue

                REMOVED

                0

                stoppTrue

                TypeError: Error #2007: Parameter child must be non-null.

                at flash.display::DisplayObjectContainer/removeChild()

                at TitavannisinglisekeelneAS3_fla::MainTimeline/remove()[TitavannisinglisekeelneAS3_fla.Main Timeline::frame1:123]

                at TitavannisinglisekeelneAS3_fla::MainTimeline/stoppTrue()[TitavannisinglisekeelneAS3_fla.M ainTimeline::frame1:115]

                at TitavannisinglisekeelneAS3_fla::MainTimeline/waterFlowEnd()[TitavannisinglisekeelneAS3_fl a.MainTimeline::frame1:106]

                at flash.utils::Timer/_timerDispatch()

                at flash.utils::Timer/tick()

                 

                 

                What is that error message trying to tell me?   

                • 5. Re: how to stop the waterflow?
                  kglad CommunityMVP

                  i should be an int, not a uint.

                  • 6. Re: how to stop the waterflow?
                    rahex Community Member

                    Thank you a lot, kglad!

                    Its working now!

                    • 7. Re: how to stop the waterflow?
                      kglad CommunityMVP

                      you're welcome.