5 Replies Latest reply on Sep 4, 2006 9:06 AM by Newsgroup_User

Random Delay

Does anyone know an efficient way to play movie clips at random(different) intervals? I have tried using "if - else if" and the random function to go to different frames in the movie, but this produced unexpected results.

Here is one of my attempts:
var jump:Number;
jump = random(2);
trace (jump);
if (jumpr = 0) {gotoAndPlay(1);
}
else if (jump = 1) {gotoAndPlay(60);
}

Even though I can see the different numbers are actually generated (by way of the "trace") I suspect the movie clip is alway playing from line 60. I am new to scripting and this puzzles me.
Help much appreciated.
• 1. Re: Random Delay
xbingox,

> Does anyone know an efficient way to play movie clips
> at random(different) intervals?

I read your whole post and am now going through it line by line, because
I'm not sure I understand what you're asking. So far, you're asking how to
play movie clips at random intervals. Coupled with your subject line,
"Random Delay," I could see using the Math.random() method (or the random()
function, as you've done) in cahoots with setInterval().

> I have tried using "if - else if" and the random function to
> go to different frames in the movie, but this produced
> unexpected results.

This is where you lost me. You're talking about going to frames in the
main timeline, not "play[ing] movie clips" (unless you mean the main
timeline itself, which is a movie clip). So are you still talking about the
same thing?

> Here is one of my attempts:
> var jump:Number;
> jump = random(2);

So far, so good. The variable jump contains a random integer of either
0 or 1.

> trace (jump);

This puts the value of jump into the Output panel, for testing.

> if (jumpr = 0) {gotoAndPlay(1);
> }

There's a typo here, and I wonder if that's your problem. You haven't
declared a variable named jumpr.

> else if (jump = 1) {gotoAndPlay(60);
> }

David Stiller
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

• 2. Re: Random Delay
I'm also a little confused about your post, but if David hasn't fixed your problem, here's my take -
I'm wondering if you're talking about randomly jumping to a set of pre-defined points with the if.. else if bit.
In which case you could have an array of possible 'jumps' using an array, then gotoAndPlay a value from the array:

var myArr:Array = new Array();
myArr[0] = "framelabel or number";
myArr[1] = "framelabel or number";
myArr[2] = "framelabel or number";
myArr[3] = "framelabel or number";
var jump:Number = random( myArr.length - 1 );
gotoAndPlay( myArr[jump] );

I may be way off here, though. Apologies for typos and so on..
• 3. Re: Random Delay
David, thanks for your exhaustive answer. I should have been more specific. Basically the swf will be an image with an animated highlight behind a mask layer. (i.e a white bar moving behind a mask layer). It looks and plays well, but with the swf looping every 3 seconds it looks a bit calculated, so I want to vary the repetition.

The moving white bar is a movie clip, but I put the action script on a separate layer in the main timeline.

Actually I just realized the movie clip itself is not animated. I motion tweened the layer containing the white line movie clip in the main timeline. The tween is from frame 1 to 20. The entire scene is 80 frames.

The typo is only in the posting.

I will investigate setInterval as you suggest
• 4. Re: Random Delay
chewtoy, thanks. Yes basically that is what I am trying to achieve. See my answer to David.
• 5. Re: Random Delay
xbingox,

> It looks and plays well, but with the swf looping every 3
> seconds it looks a bit calculated, so I want to vary the
> repetition.

Okay. I suppose that means you might be sending a particular movie clip
to a certain frame, where it can loop from the beginning (or some other
point) -- but it should do this looping at random intervals.

The setInterval() function will do what you need. You'll just have to
use it, then kill it, then use it, then kill it, again and again, providing
a random number of milliseconds each time.

David Stiller