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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setInterval and clearInterval whoas 1

Status
Not open for further replies.

hovercraft

Technical User
Jun 19, 2006
236
US
It is my understanding that by using setInterval I can run a function every few miliseconds and then use clearInterval to reset the variable and allow the function to be ran again...and again and again.
However, I'm not having any luck with this.

I've included my script which is the first (and only) frame.
What it does is create a random number of enemys (tie fighters)and moves each enemy from the top to the bottom with a random yet constant speed.
and I want it to run the createTie function every couple of seconds, so that there is a steady supply of enemys.

Any help would be greatly appreciated.
Thanks,
Dave
Code:
//---- variables ----
var intervalId:Number;
var randTieNum:Number = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
var i:Number = 0;
//---- functions ----
function createTie()
{
	for ( i;i<randTieNum;i++)
		{ 
			var currenttimer:Number = getTimer();
			attachMovie("tiefighter", "myTie"+i+currenttimer, _root.getNextHighestDepth());
			myTie = _root["myTie"+i+currenttimer];
			placeTie(myTie);
						
			myTie.onEnterFrame = function()
				{
					if (this.hitTest(_root.mylaser01))
						{_root.myscore += 10;
						removeMovieClip(_root.mylaser01);
						removeMovieClip(this);
						}
					if (this._y > 400)
						{
							removeMovieClip(this);
						}
					this._y += this.Speed;
	clearInterval(intervalId);			}
		}
}

function placeTie(which)
{
	var randTiex:Number = Math.floor(Math.random() * (550 - 1 + 1)) + 1;
	var randTieSpeed:Number = Math.floor(Math.random() * (5 - 1 + 1)) + 1;
		which._x = randTiex;
		which._y = 70;
		which.Speed = randTieSpeed;
}
//---- start ----
intervalId = setInterval(_root, "createTie", 1000);
 
There are some issues with your code:

1. Remove "clearInterval(intervalId);", otherwise "createTie()" is called once only.

2. for (i=0; i<randTieNum; i++) //otherwise this loop won't run 2nd time round.

I think you probably need to change other area as well to achieve your goal, but at least these will get you started.

Kenneth Kawamoto
 
Oh I see. The clearInterval actually "Clears the Interval" and stops it from running. And the problem with the for loop is the "i>randTieNum" since the interval randTieNum is populated outside of the loop and only produces the same random number of enemys each time.
I have made your suggested corrections (including i=0) and it works great!

It's not the best looking thing in the world but I'm just doing it to learn Flash.

Thanks a ton and if you have any additional suggestions please feel free.

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top