A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: A Timing Question

  1. #1
    Hairy Member robbmcaulay's Avatar
    Join Date
    Dec 2001
    Location
    Edinboro, yo!
    Posts
    1,596

    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);

    It's vaige... but you get the idea?

    Thanks in advanced... Robb
    "Wah wah wah Dorothy Parker wah wah wah" - hanratty21

  2. #2
    Former Member now elsewhere
    Join Date
    Dec 2001
    Location
    Out of here
    Posts
    104
    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();
    }

  3. #3
    Junior Member
    Join Date
    Apr 2003
    Location
    Southern CA
    Posts
    10
    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

  4. #4
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    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?"

  5. #5
    Hairy Member robbmcaulay's Avatar
    Join Date
    Dec 2001
    Location
    Edinboro, yo!
    Posts
    1,596
    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 ")
    ?
    "Wah wah wah Dorothy Parker wah wah wah" - hanratty21

  6. #6
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    robbmcaulay, yes, that's right. Just remove the trace and insert your own code. Just make sure you don't remove clearInterval(flashInterval);

  7. #7
    Former Member now elsewhere
    Join Date
    Dec 2001
    Location
    Out of here
    Posts
    104
    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

  8. #8
    Hairy Member robbmcaulay's Avatar
    Join Date
    Dec 2001
    Location
    Edinboro, yo!
    Posts
    1,596
    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)
    "Wah wah wah Dorothy Parker wah wah wah" - hanratty21

  9. #9
    Hairy Member robbmcaulay's Avatar
    Join Date
    Dec 2001
    Location
    Edinboro, yo!
    Posts
    1,596
    If my hosting wasn't ****ed
    "Wah wah wah Dorothy Parker wah wah wah" - hanratty21

  10. #10
    Hairy Member robbmcaulay's Avatar
    Join Date
    Dec 2001
    Location
    Edinboro, yo!
    Posts
    1,596
    Thanks guys! It works perfectly... you can view my results here

    (I change the interval to 10-20 seconds)

    Thanks again... Robb
    "Wah wah wah Dorothy Parker wah wah wah" - hanratty21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center