how to stop the waterflow?
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.y += 8;
WatertropArray.x += 5;
//trace(i);
if(WatertropArray.hitTestObject(WaterArea01))
{
WatertropArray.alpha = Math.random();
WatertropArray.scaleX = Math.random();
WatertropArray.scaleY = WatertropArray.scaleX;
}
if(WatertropArray.hitTestObject(WaterBorder01) || WatertropArray.x > stage.stageWidth || WatertropArray.y > stage.stageHeight / 2)
{
WatertropArray.x = Math.round(Math.random() * stage.stageWidth / 1.5);
WatertropArray.y = Math.round(Math.random() * stage.stageHeight / 3);
WatertropArray.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);
}
