To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-14-2004, 10:51 PM   #1
lemorris
Member
 
Join Date: Oct 2002
Location: Tucson
Posts: 92
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
lemorris is offline   Reply With Quote
Old 10-14-2004, 11:04 PM   #2
jbum
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
__________________
jbum is offline   Reply With Quote
Old 10-14-2004, 11:11 PM   #3
lemorris
Member
 
Join Date: Oct 2002
Location: Tucson
Posts: 92
Great Scott Jim!!

Who are you?!!

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

-Lemorris
lemorris is offline   Reply With Quote
Old 10-14-2004, 11:15 PM   #4
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
who am i?

Glad to be of service.
__________________
jbum is offline   Reply With Quote
Old 04-11-2006, 12:10 PM   #5
carbon_sprout
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
carbon_sprout is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:51 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.