-
A Timing Question
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...
gotoandplay IN randomNumber seconds (1);
:rolleyes: It's vaige... but you get the idea?
Thanks in advanced... Robb
-
It's part of getTimer() which returns the number of milliseconds since "the movie" loaded (I'm assuming that means since _level0 was started).
I'm not home and can't check this but I think it should work as is... or be pretty close.
NCD
strikeTime=5;
lastTime= Math.ceil((getTimer()/1000);
//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.
____________________________________________
onClipEvent (load) {
minTime = 100;
maxTime = 150;
}
onClipEvent (enterFrame) {
counter++;
if (counter > minTime) {
strike = random(50);
if (strike == 25) {
_root.lightning.gotoAndPlay(2);
counter = 0;
}
}
if (counter == maxTime) {
_root.lightning.gotoAndPlay(2);
counter = 0;
}
}
____________________________________________
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.
Enjoy!
-Tim
-
You can use this function:
Code:
function startFlashes(minMilliSec, maxMilliSec) {
if (flashInterval) {
trace("Flash"); // do something
clearInterval(flashInterval);
}
flashInterval = setInterval(startFlashes, (minMilliSec+random(maxMilliSec-minMilliSec+1)), minMilliSec, maxMilliSec);
}
Just insert your own code instead of the trace action. To start the flashes, and make them appear with 20 to 30 seconds delays, type:
Code:
startFlashes(20000, 30000);
As you can see, the arguments are in milliseconds. To stop the flashes, use:
Code:
function stopFlashes() {
if (flashInterval) {
clearInterval(flashInterval);
delete flashInterval;
}
}
I think this method is the least demanding on the CPU, since you never do any checks of the kind "is it time to show the flash yet?"
-
Thanks for the great feedback guys! You really are genius'!
I think i'll start with the simplest one... Strille's...
When you say eplace the trace action you mean replace
Code:
trace("Flash"); // do something
with
Code:
gotoandplay(" THE FRAME THAT HAS THE FLASH ")
?
-
robbmcaulay, yes, that's right. Just remove the trace and insert your own code. Just make sure you don't remove clearInterval(flashInterval);
-
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.
NCD
-
Yeh, I know what you mean... I'm definately impressed with the feedback... that's what flashkit is all about...
Thanks...
Robb
(will post the results here when i'm done)
-
If my hosting wasn't ****ed
-
Thanks guys! It works perfectly... you can view my results here
(I change the interval to 10-20 seconds)
Thanks again... Robb