Copy link to clipboard
Copied
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);
}
i should be an int, not a uint.
Copy link to clipboard
Copied
don't use the assignment operator (=). use the double equal to test for equality:
if (stop == true)
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
remove the water drops using a for-loop that loops from end to beginning of WatertropArray
Copy link to clipboard
Copied
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.MainTimeline::frame1:123]
at TitavannisinglisekeelneAS3_fla::MainTimeline/stoppTrue()[TitavannisinglisekeelneAS3_fla.MainTimeline::frame1:115]
at TitavannisinglisekeelneAS3_fla::MainTimeline/waterFlowEnd()[TitavannisinglisekeelneAS3_fla.MainTimeline::frame1:106]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
What is that error message trying to tell me?
Copy link to clipboard
Copied
i should be an int, not a uint.
Copy link to clipboard
Copied
Thank you a lot, kglad!
Its working now!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now