-
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
-
Senior Member
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
-
Great Scott Jim!!
Who are you?!!
Thanks much for the speedy reply. You pulled my hinder out of the fire again.
-Lemorris
-
Senior Member
who am i?
Glad to be of service.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|