stop duplicating a movieclip

i want to know how to stop this after 15 seconds.

or delete it after 15 seconds


star_mc._visible = true;
var i:Number = 0;
var total:Number = 90;
for( ; i < total; i++ )
{
	star_mc.duplicateMovieClip("star"+i, i);
	this["star"+i]._x = Math.floor(Math.random() * 800);
	this["star"+i]._y = Math.floor(Math.random() * 600);
	this["star"+i].gotoAndPlay( Math.floor(Math.random() * star_mc._totalframes) );
	
}

Open in new window

TecServAsked:
Who is Participating?
 
gxp071Connect With a Mentor Commented:
hay have shown below,  and have tested it aswell,  added two traces so as can see whats happending in the output window but not required.

also had a typo on the clearInterval();

anyway should work now let me know if you have anymore problems.
star_mc._visible = true;
var i:Number = 0;
var total:Number = 90;
var breakFlag:Boolean = false;
 
function stopLoop(){
	breakFlag = true;
	trace('Stopped');
	clearInterval(timerLoop);
}
 
var timerLoop = setInterval(stopLoop,15000);
 
for( ; i < total; i++ )
{
        if(breakFlag){ break; }
 	trace(breakFlag + ' ' + i);
        star_mc.duplicateMovieClip("star"+i, i);
        this["star"+i]._x = Math.floor(Math.random() * 800);
        this["star"+i]._y = Math.floor(Math.random() * 600);
        this["star"+i].gotoAndPlay( Math.floor(Math.random() * star_mc._totalframes) );
}

Open in new window

0
 
gxp071Commented:
the quickest way is with an interval and a flag,  or you can put the loop into a function and clearing using an interval  The code below shows an interval with a boolean flag.
star_mc._visible = true;
var i:Number = 0;
var total:Number = 90;
var breakFlag:Boolean = false;
 
for( ; i < total; i++ )
{
        if(breakFlag){ break; }
 
        star_mc.duplicateMovieClip("star"+i, i);
        this["star"+i]._x = Math.floor(Math.random() * 800);
        this["star"+i]._y = Math.floor(Math.random() * 600);
        this["star"+i].gotoAndPlay( Math.floor(Math.random() * star_mc._totalframes) );
        
}
 
function stopLoop (){
 breakFlag = true;
 clearInerval(timerLoop)
}
 
var timerLoop = setInterval(stopLoop,15000);

Open in new window

0
 
gxp071Commented:
sorry you will need to put the function and interval above the for loop.
0
 
TecServAuthor Commented:
where exactly do i put it, i tried almost everywhere and it doesn't seem to make a difference.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.