Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting setinterval to work

Status
Not open for further replies.

orangeblue

Programmer
Nov 26, 2002
107
GB
Hi

I am trying to load a the same movieclip on to the stage every 5 secs. I am having problems with setinterval as it doesn't seem to work.

please can u help here is the code

//this function will get a loop of the number of daisys

getdaisys = function(){
i = 1 ;
//geting a the movieclip
//setInterval(duplicateMovieClip(test, "daisy"+i+"_mc", i), 5000);
_root.attachMovie("mc_daisy_ani", "daisy_mc"+i, i)
this._x+=(Math.random()*4)-2 ;
this._y+=(Math.random()*4)-2;
this.gotoAndPlay;
}

//call the function
for(i=1; i <= 5; i++){
setInterval(getdaisys(),5000);
};
 
Try this:

Code:
getdaisys = function(){

i = 1 ;
    //geting a the movieclip
//setInterval(duplicateMovieClip(test, "daisy"+i+"_mc", i), 5000);
_root.attachMovie("mc_daisy_ani", "daisy_mc"+i, i)
this._x+=(Math.random()*4)-2 ;
this._y+=(Math.random()*4)-2;
this.gotoAndPlay;
}

//call the function
/*for(i=1; i <= 5; i++){
  setInterval(getdaisys(),5000);
};*/ 
}

setInterval(getdaisys,5000);

This will not call the getdaisys the first time until 5 seconds after the initial load.

Hope it helps!

Wow JT that almost looked like you knew what you were doing!
 
Hi thanks for that

but i need to put a loop so that there are about 5 movieclips

how can i do that
 
You want the same function to be called 5 times at different intervals? (is that what you mean?)
 
well there are two problems

1) this code:

Code:
this._x+=(Math.random()*4)-2 ;
this._y+=(Math.random()*4)-2;

is not going to work. "this" is not going to refer to the daisy mc like you're thinking. Instead, create an object and assign its properties to the daisy mc.

2) to clear the interval after 5, use clear interval

both these are addressed in this code:

Code:
var a:Number = 0
var daisyObject:Object = new Object();

getdaisys = function () {
	i = 1;
	a ++
	// geting a the movieclip
	daisyObject._x = mc_daisy_ani._x + (Math.random()*4)-2;
	daisyObject._y = mc_daisy_ani._y + (Math.random()*4)-2;
	_root.attachMovie("mc_daisy_ani", "daisy_mc"+i, i, daisyObject);
	this.gotoAndPlay;
	//clear setInterval attached to intervalID after 5 rounds
	if (a == 5) {
		clearInterval(_root.intervalID)
	}
};

//attach ID to setinterval so it can be cleared
var intervalID:Number = setInterval(getdaisys, 5000);
for this to work, make sure you have a daisy mc already on the stage (with an instance name "mc_daisy_ani"), that way the random number between -2 and 2 will be added on to this mc's x and y coordinates.

one more thing:

this code:
Code:
this.gotoAndPlay;

what is it refering to? the main timeline or the daisy mc timeline? if its the daisy one, shouldn't it start anyway (making the line unnecessary)?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top