Playing MC's at random times?

This maybe a simple problem, but i’m quite new to action script…

I’m creating a portfolio style web site, and would like to have the image of a character’s head on it that occasionaly blinks, and possibly moves his eyes.

I know I could create a MC of the eyes blinking, every 20 frames or something, but i wondered if there is an action script solution, where by I could create a short MC of the blinking action and get it to play the MC every minute or so, or even better at random times?

Any Help is appreciated.

you could make an mc of the eyes blinking. then use this AS for the MC:[AS]onClipEvent(enterFrame){
blinking = Math.random()1*1000; // pick a random number between 1 and 1000
if((blinking>100) and (blinking<200)){ // if blinking is between 100 and 200
this.gotoAndPlay(2); // frame 2 is the blinking animation
}
}[/AS]you could of course change how often it blinks by decreasing the span (in this case 100-200) or changing the frame rate. or changing the range of numbers to draw a random # from.

for random times search for tutorial about random.whatever.
for fixed intervals of time look for tutorials about setInterval.

Thanks guys, ill give them a go.

bwh2,

Thanks for the help, i understand the principal, but i cant get the AS to work, Flash says there are errors like a missing ‘;’ on line 2 - i don’t understand!

onClipEvent(enterFrame){
blinking = Math.random()1*1000;
if((blinking>100) and (blinking<200)){
this.gotoAndPlay(2);
}
}

Am I applying the AS to the blinking MC?

In the MC on frame 1, do I place AS:

gotoAndPlay (1);

so that it loops, and as the blinking animation is from frame 2 onwards, it will not play it unless it meets the conditions in the top AS?

Hope that made sense?!:puzzled:

yeah, i made a typo in the AS. should be:[AS]onClipEvent(enterFrame){
blinking = Math.random()*1000+1;
if((blinking>100) and (blinking<200)){
this.gotoAndPlay(2);
}
}[/AS]
in the MC, on frame 1, place stop(); that way it will stop on frame 1 of the MC (not play the animation), until told to play the animation by the condition being met.

ok, i got it!

it works a treat

that’s fantastic.

bwh2, you truly are a genius!!!

thank you so much:beam:

yeah, i get that a lot.

sorry to be a pain in the a**e,

but…

how do get the whole process to start again, only once the animation has finished?

At the moment, the the animation may start from the begining again, before its finished playing all the way thru. Due to the AS conditions, i guess.

I’ve tried putting stops in after 40 frames or so, on a root layer, and in the MC animation itself, but no luck.#

Thanks again

this should work:[AS]onClipEvent(enterFrame){
blinking = Math.random()*1000+1;
if((blinking>100) and (blinking<200) and (this._currentframe==1)){
this.gotoAndPlay(2);
}
}[/AS]this way, it won’t gotoAndPlay(2) unless the mc is currently stopped in frame 1. when the conditions are met, the animation in the MC will gotoAndPlay(2) then will eventually stop; back at 1. then it will be able to play again when the blinking conditions are met.

Thanks again,

It kinda works…

It doesn’t play the animation again until the current one has finished, which is good, but it often resets the animation half way thru instead!!!

i’m not quite sure what this means. i’ve tried the AS and it works for me without anything weird. if you want to post your fla, i can look at it.

Thanks so much for your help bwh2, here’s the fla.

ok this is just a simple test animation, so i can get the mechanics working before I do it properly.

Basically the animation is complete when the red circle has moved back to its starting position. However, It does not always do this, sometimes it stops playing half way thru, ie: the screen goes back to white before the animation cycle has finished.

ok. i looked at your fla and i am just confused why you have 59 frames of nothing happening. i think you were trying to create the “loop” (so that the onEventClip(enterFrame) works. actually though, that “loop” will be set by your fps.

basically, you have 2 options.

  1. delete those first 59 frames and make the AS in the remaining frame stop();
  2. make the keyframe (frame 60) in the animation layer a regular frame. make the AS for it stop();

the keyframe (frame 60) is what’s causing the problem. the keyframe means that the mc reloads. so if it reloads in frame 60, but at that time is in the middle of an animation, the animation will stop mid-stride and restart. making it a regular frame will allow the animation to complete.

i would recommend doing option 1 because those other 59 frames are just unnecessary and you can clean things up by deleting them.

Thanks for that bwh2,

I tried option 1, and it works perfectly.

I thought that i needed the sixty frames for the animation to play out in, or otherwise it would just restart as soon as it hit frame 2, and unless it had a go to frame 1 at the end, then it would not play the AS again.

Hmmm… just when I thought I was getting to understand AS.

Thank you so much for your time and help, its much appreciated. Everyone here is so helpfull, what a great place!