PDA

View Full Version : Function timer



Skribble
September 1st, 2010, 09:58 AM
Hey guys, just a quick question for a little shmup I'm working on.

I've set up a function so that I can spawn an enemy movieclip with a bunch of defined properties, like this:

//Enemy type, Spawn type, Spawn amount, Spawn delay
spawnEnemy(enemiesArray[drone], "singleWave", 6, 30);

So it spawns my enemy, gives it a formation type, says how many enemies should spawn, and the gives the delay between each spawned enemy. The only thing is I can't figure out how to set up the delay.

How can I set up a function that will run a certain amount of times every X frames/milliseconds? I know I can just set up a timer variable in an onEnterFrame but I'd rather not do it that way if possible.

Any help is greatly appreciated.

Thanks!


Brendan

glosrfc
September 1st, 2010, 11:07 AM
check the Help docs for setInterval:


duration = 30;
intervalID = setInterval(spawnEnemy, duration, enemiesArray[drone], "singleWave", 6);
//
function spawnEnemy(enemyType, spawnType, spawnAmount) {
// function code
}

Skribble
September 2nd, 2010, 01:32 AM
Ahhh setInterval! I had completely forgotten that even existed hahah.

I'm not at home atm, so I can't check this out: Is the duration variable in setInterval the duration of time between running the function again?

Also, I use clearInterval(intervalVar) to stop the setInterval, right?

Cheers again for the reply!

Krilnon
September 2nd, 2010, 01:39 AM
Yes for both. Those functions also exist in JavaScript, so you can test them wherever you are. :P

TheCanadian
September 2nd, 2010, 01:54 AM
There is also a setTimeout function which is like setInterval but only calls the function once.

If you're using AS3, there is a Timer class which you may prefer.

Just fyi :)

wcharlot
September 11th, 2010, 04:30 AM
yes he is right. there must be a setTimeout function that can call to the function once

TheCanadian
September 11th, 2010, 04:44 AM
Thanks for the consult

greensock
September 11th, 2010, 04:41 PM
If memory serves correctly, there are some bugs with setTimeout() at least on the Mac. If you're using TweenLite (http://www.TweenLite.com) or TweenMax (http://www.TweenMax.com) anywhere in your project, I'd recommend using the delayedCall() method like:


TweenLite.delayedCall(1, myFunction, ["param1", "param2"]);

That'll wait 1 second and then call myFunction("param1", "param2");