A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Where's the delay()? Please help!

  1. #1
    Junior Member
    Join Date
    May 2005
    Posts
    1

    Where's the delay()? Please help!

    Here's the setup....

    I have developed a silly little algorithm for simulating someone blinking their eyes. It's based on the average that a person blinks about 25 times per minute and have reduced it to that 60% of the time a person will blink once every 7-10 seconds and 40% of the time they'll blink every 1-6 seconds. However, I'm trying to figure out a few things and this it's driving me bonkers. It doesn't help, either, that I haven't touched ActionScript in a few years so I'm having to relearn almost everything.

    The first, does anyone have any suggestions or improvements on my algorithm?

    Secondly, what I've made depends on a sort of delay() function to pause a certain amount of time in between blinks. So when the algorithm chooses any number of seconds to "delay" when the figure in the drawing blinks his eyes, it will pause the execution of the code for that length of time. So far, I have not figured out a good way of doing this within a single frame and every single example I've found on the web to introduce delays assumes I'm executing more than one frame.

    Thirdly, the script also simulates "blinking" by simple setting the alpha of the eyelids to zero and then back to 100%. During my tests though, without my algorithm in place and just a linear flashing going on, the eyelids just appear on all the time. I have confirmed that it's indeed cycling, but it's just cycling too fast for anyone to notice the effect. So once again, another delay or something is needed between the "on" and "off" states so someone can see the flashing going on. Help? Pretty please?

    (If that jbum dude's around, I'd love to hear from you on this as I've seen some of the stellar answers you've already given in this stuff! )

    Thanks.

    - Victor

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I have always used getTimer() function for that. It gives you number of milliseconds measured from the start of your movie.

    First declare the variables:
    code:

    _root.onLoad = function () {
    //set time
    lastTime=getTimer();
    //set delay
    delay=int(60/25*1000);
    //time to hold eyes closed
    closedTime=50;
    //current state of eyes is opened
    eyesClosed=false;
    }


    Here the delay would be number of milliseconds between 2 blinks based on your "25 times per minute".

    I have also set the time to hold eyes closed to 50 ms, you can change it to whatever looks real.

    Make a function that will be called at each frame:
    code:

    _root.onEnterFrame = function () {
    //get time from last check
    timePassed=getTimer()-lastTime;
    //if eyes are closed
    if(eyesClosed){
    if(timePassed>closedTime){
    //hide eyelids mc
    eyelids_mc._visible=false;
    //get the time
    lastTime=getTimer();
    //set state of eyes
    eyesClosed=false;
    }
    }else{
    //see if it is time to close the eyes
    if(timePassed>delay){
    //set the state of eyes to closed
    eyesClosed=true;
    //show eyelids mc
    eyelids_mc._visible=true;
    //get the time
    lastTime=getTimer();
    }
    }
    }


    The function basically measures time between last event (either eyes were closed or they were opened) and current moment. It then checks if enough time has passed to either close the eyes (>delay) or open them (>closedTime). If time is gone enough, it changes the state of eyes, hides/shows the mc and saves current time.

  3. #3
    Junior Member
    Join Date
    Nov 2003
    Posts
    18
    Actually... setInterval would be better in this case:

    Code:
    var intervalID;
    
    //calls the "blink" function after numSeconds from this function call
    function blinkAfterSeconds(numSeconds)
    {
         intervalID = setInterval(blink, numSeconds*1000);
    }
    
    function blink()
    {
         clearInterval(intervalID);
         trace("blink")
         //blink again in 5 to 10 seconds
         blinkAfterSeconds(5 + Math.round(Math.rand(5));
    }

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