Hey... I can't think of a technical way to describe this so here I go...
I have a scene with rain... and I want lightning to strike (an MC to play) randomly... I want the gap between flashes to be no shorter than 20 seconds and no longer then 30 seconds...
Now... I know how to generate a random number between 20 and 30 but how do I use this as some sort of timer like this...
//Set up the checker/trigger function
checkTime=new function(){
var now = Math.ceil((getTimer()/1000);
if(now==(lastTime+strikeTime)){
lastTime=now;
strikeAndNextStrike();
}
}
//Set up the strike and random function
strikeAndSetNextStrike = new function(){
//BOOM!
lightning_mc.play();
//New Random
strikeTime = Math.ceil(Math.random*20);
}
//Make it go(first strike is in 5 sec, then random)
onClipEvent(enterFrame){
checkTime();
}
This code is a lot simpler and I've tested it and can confirm that it works. I've set up a "dummy" MC name as "_root.lightning" Please change that name to match your lightning movie clip.
Basically, I've declared variables for a minimum time allowance and a maximum time allowance. I then start a counter. Once this counter reaches the minimum time allowance, a random number generator is triggered. If the "right" number is chosen, the lighting movie clip starts its animation on frame 2 and the counter is reset for the next cycle. If the "right" number is not chosen, the counter waits until it reaches the maximum time allowance and the lighting movie clip then starts its animation and the counter is reset.
Note: You'll need to modify the minTime and maxTime variables to your liking.
Pretty cool how we can all solve it in very different ways!
I think I may like strille's best too since it's the least CPU
intensive.
The use of multiple frames is a good way to control the timing too
but I'm in a mindset of always trying to do things using all
actionscript, including timing. That's not good or bad, it's just
that I've been trying to force myself to use actionscript to solve
everything... hoping it helps me to get better with the coding.