I m trying to create a simple loop that adds a random number of stars, fades them out and removes them.
The script I ve come up with does everything but remove them, and perhaps I need a less on adding children to a stage.
Here s what I come up with
import flash.display.*;
import com.greensock.*;
import com.greensock.easing.*;
// timer setup
var timer:Timer=new Timer(randomNumber(500,1000));
timer.addEventListener(TimerEvent.TIMER,run_stars);
timer.start();
// Random number generator
function randomNumber(low:Number=NaN, high:Number=NaN):Number {
var low:Number = low;
var high:Number = high;
if(isNaN(low)) { throw new Error("no low number"); }
if(isNaN(high)) { throw new Error("no high number"); }
return Math.round(Math.random() * (high - low)) + low;
}
// randomly adding stars on timer
function run_stars(event:TimerEvent):void {
// random num of stars
for (var i:Number=1; i<=randomNumber(2,7);i++) {
var star:m_star = new m_star();
addChild(star);
// This is where my problem starts, I m adding the same movie clip multiple times without any way to identify and remove.
star.x = randomNumber(0, stage.stageWidth);
star.y = randomNumber(0,stage.stageHeight/2);
TweenLite.to(star, randomNumber(0.5,1), {alpha:0.25, onComplete:removeStar()});
}
timer.delay = randomNumber(500,1000);
timer.start();
}
function removeStar() {
removeChild(star);
//this would be where I attempt to remove a star but because they aren t unique it will never work, and the star movie clip is called inside of the function so it cant even see it.
}
stop();
I need a way to make the movie clips unique so I can tell my oncomplete function to remove the property clip, if I don t do this the movie will eventually slow down and crash because of so many (invisible) movieclips.
Thanks!