A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: another stupid timer question...sorry

  1. #1
    Member
    Join Date
    Oct 2002
    Location
    Tucson
    Posts
    97

    another stupid timer question...sorry

    I know I know...I've looked at 30 posts on this and still can't get it to do what I want. Sorry for posting it again, but I've been beating myself over the head, and I have to get this done.

    Now,

    I have a MC on stage named Fred. Fred has 4 frames. I want to change to a random frame in fred every 5 seconds.

    Fred has a stop() in his first frame.

    Here's what I have placed on fred:


    onClipEvent(load) {
    fred.gotoAndStop(random(4)+1);
    delay = 5000; // a 5 second delay
    resetTime = getTimer() + delay;
    }

    onClipEvent(enterFrame) {
    if (getTimer() > resetTime) {
    gotoAndStop(random(4)+1);
    }
    }


    When the movie starts fred waits 5 seconds then he just starts flippin through his 4 frames.

    Anybody?

    Thanks,

    -Lemorris

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    You've nearly got it, you just need to reset the timer whenever you you've determined it's time to jump frames, like so:

    Modifying your original code:
    (I wasn't sure what 'fred' was - if it's the clip this code is attached to, you don't need it).

    code:

    onClipEvent(load) {
    gotoAndStop(random(4)+1);
    delay = 5000; // a 5 second delay
    resetTime = getTimer() + delay;
    }

    onClipEvent(enterFrame) {
    if (getTimer() > resetTime) {
    resetTime = getTimer() + delay;
    gotoAndStop(random(4)+1);
    }
    }



    Here's a shorter rewrite:

    code:

    onClipEvent(load) {
    resetTime = 0; // force it to go off right away
    }

    onClipEvent(enterFrame) {
    if (getTimer() > resetTime) {
    resetTime = getTimer() + 5000;
    gotoAndStop(random(4)+1);
    }
    }



    Here's a more concise method that uses setInterval. Should be less of a drain on the CPU since you're not using an enterFrame handler.

    code:

    onClipEvent(load)
    {
    setInterval( function(mc) { mc.gotoAndStop(random(4)+1) }, 5000, this);
    }




    - Jim

  3. #3
    Member
    Join Date
    Oct 2002
    Location
    Tucson
    Posts
    97
    Great Scott Jim!!

    Who are you?!!

    Thanks much for the speedy reply. You pulled my hinder out of the fire again.

    -Lemorris

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    who am i?

    Glad to be of service.

  5. #5
    Junior Member
    Join Date
    Apr 2006
    Posts
    11

    tried #3 (setInterval) without success

    Hi,

    wondering if anyone has insights into the 3rd approach, with which I got a very different effect than with the other 2.

    I've been trying to understand setInterval better so I was happy to see Jim was giving an example. I've read some of his posts on other stuff, & looked at KrazyDad - all great.

    I'm not having any luck, though, with the setInterval example. I'm copying and pasting it to a mc containing 5 different frames - the same mc that works with the other approaches. With the setInterval approach, it races randomly through the frames for 5 seconds....

    Wondering if anyone can explain how to make setInterval behave the same as the other 2. I'm imagining it will give me a better understanding of what's going on than even just seeing a working one.

    cs

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