all groups > flash actionscript > august 2006 >
You're in the

flash actionscript

group:

Random Interval


Random Interval aniebel
8/23/2006 9:31:23 PM
flash actionscript: I've used setinterval before in projects but mostly just copied, pasted and
studied. I've studied them over and over but it's just not gelling with me. If
anyone could explain it in a way that sinks in, I'll be forever in your debt.

Here is what I've got so far but I know this won't work since I don't think
the property for the array is correct:


var intArray:Array = new Array("12", "14", "18", "20", "24", "30", "36");
var randomInterval:Number = setInterval(fRandomCall, intArray[i]);
function fRandomCall(mc:MovieClip) {
mc.gotoAndPlay(2);
}
Re: Random Interval kglad
8/23/2006 10:04:40 PM
you really shouldn't put strings (like "12" etc) in an array where you expect
numbers. setInterval() will use strings so it works in your current code, but
that's (accepting a string when a number is expected) a rarity in flash.

anyway, if you want to repeatedly execute fRandomCall() at random intervals of
12, 14 , 18 etc milliseconds use:



var intArray:Array = new Array("12", "14", "18", "20", "24", "30", "36");
// Math.floor(Math.random()*intArray.length) is one of
0,1,2,...,intArray.length-1
var randomInterval:Number = setInterval(fRandomCall,
intArray[Math.floor(Math.random()*intArray.length)]);
function fRandomCall(mc:MovieClip) {
clearInterval(randomInterval); // unless you want to continue executing this
function at the same interval first generated, you should clear your interval
and generate another. ie, the line below
var randomInterval:Number = setInterval(fRandomCall,
intArray[Math.floor(Math.random()*intArray.length)]);
mc.gotoAndPlay(2);
}
Re: Random Interval Rothrock
8/23/2006 10:42:33 PM
You don't seem to be sending the name of a movieclip instance to your function
with the setInterval. Could that be the problem. As for the other part of your
question?

So what is not to understand? setInterval(function, time delay, arguments)
means to start calling function ever time delay and pass the function those
arguments (if any). The interval will continue to call, over and over, until
you clearInterval().

Each time you setInterval, Flash keeps track of it internally. The first
interval that it tracks is 1, then 2, and so on. That number is "returned" by
the setInterval call so you can assign it to a variable is you want.

myInterval=setInterval(someFunction,100);
trace(myInterval);

Will keep track of the interval in the variable myInterval and show you that
it is just a number. Components and some other Flash things use intervals in
their code ? stuff you never see ? so you can't count on your intervals having
consecutive numbers. Sometimes they will be and sometimes they will skip. You
can count on the numbers always getting larger, but you won't know by how much.

So if you do something like this:

myInterval=setInterval(someFunction1,100);
trace(myInterval);
myInterval=setInterval(someFunction2,100);
trace(myInterval);

You will see that the value of myInterval will be set twice with different
values. But the variable isn't the interval, it is just its number. Like when
you go to a bakery and they give you a number. It isn't you or the delicious
baked goods, it is just a slip of paper with a number that says your place in
line. So both intervals will keep going and continually operating until
cleared. But here is the problem.

In the above example, you will "lose" the first value ? let's say in this
example the first value was 12 and the next value was 21. Since 12 was replaced
by 21, the following line:

clearInterval(myInterval);

will clear the interval that is identified by 21 ? the someFunction2. However
there is no record (available to you) that interval number 12 is doing
someFunction1 every 100 milliseconds. Now if you knew, you could do this:

clearInterval(12);

and that would stop it. But how are you going to know that it is 12 and not 74
or any other of a million possible values?


Re: Random Interval aniebel
8/24/2006 2:01:20 PM
Oops, so I should set up the array as numbers instead of strings. I see my
mistake there. Instead of calling that variable inside of the function
repeatedly, is there a way to step through the array to plug in the next
interval time? Also, I'm trying to set these intervals as "seconds" so I
realize I need to multiply it by 1000 at some point. I assume it would be as
follows:

var randomInterval:Number = setInterval(fRandomCall,
intArray[Math.floor(Math.random()*intArray.length)*1000]);
Re: Random Interval aniebel
8/24/2006 2:08:04 PM
I realize now why they told me I'd use math someday. I should have focused more
in school. :-S

Thanks, Rothrock, for your detailed explanation. I understand the basic
concept of setInterval but it gets crazy for me when I start defining it as a
variable and you've helped me understand that better. Now, if I were to take
your example and change the variable names to myInterval1 and myInterval2, does
the fact remain that one will "overwrite" the other? Will it still be an issue
to clear it? I would just "clearInterval(myInterval1);" and
"clearInterval(myInterval2);" correct? Was that the point you were trying to
make about that... that I need to establish various intervals with their own
variable names so I can clear them properly??

Thanks for your patience and help.
Re: Random Interval Rothrock
8/24/2006 5:32:31 PM
Remember you aren't really defining the interval as a variable. The interval is
defined and kept track of by the Flash player, you are just keep a record of
the intervals ID number as it were.

You seem to understand it. If the variables have different names then they
won't overwrite each other. Each variable can only hold one thing. So if you
try and put another ID number in it, the first is lost.

Yup that would more or less be my point.

And of course there is no problem of "reusing" a variable if you have already
cleared the interval.

If you are going to have many intervals, you might just want to push() them
all on to an array.
Re: Random Interval aniebel
8/24/2006 9:11:59 PM
kglad, this code sends the player into an endless loop. I have tried something
else so that another interval is used but my method has failed. My thought was
to create a new interval, clear the first one and start the new function. I
hope I can wrap my head around this soon but you guys are certainly helping me
get there.


var intArray:Array = new Array(2000, 4000, 8000);
var randomInterval:Number = setInterval(fSparkle,
intArray[Math.floor(Math.random()*intArray.length)]);
var newInterval:Number = setInterval(fSparkle2,
intArray[Math.floor(Math.random()*intArray.length)]);
function fSparkle(mc:MovieClip) {
clearInterval(randomInterval);
mc.gotoAndPlay(2);
fSparkle2(mc);
}
function fSparkle2(mc:MovieClip) {
clearInterval(newInterval);
mc.gotoAndPlay(2);
}
Re: Random Interval kglad
8/25/2006 1:55:10 AM
Re: Random Interval aniebel
8/25/2006 1:32:36 PM
Geez, I hope I don't post twice with this reply... the forum's acting up on me.
Anyway...

Sorry for not being more clear. What I want to do is call the fRandomCall
function at random intervals. Not a repeating interval from a list of random
numbers. For instance I want it to occur first after 10 seconds, then after 8
seconds, then after 30, then after 18... whatever the number (but pulled from
an array)... just so it appears to happen randomly instead of after the same
amount of time.

What I meant by the "endless loop" was that it locked up Flash Player in
testing mode and I had to force quit Flash to clear it.
Re: Random Interval Rothrock
8/25/2006 2:51:03 PM
Ah, well the point of each setInterval is that it will repeat at the rate (more
or less) set. But no problem, this just means you will use each interval once,
clear it, and then set another. Now without the array part.

myFunction(){
clearInterval(myInterval);
//do some other stuff
myInterval=setInterval(myFunction,Math.random()*8000+5000);
}

myInterval=setInterval(myFunction,Math.random()*8000+5000);

Re: Random Interval kglad
8/26/2006 12:25:40 AM
Re: Random Interval aniebel
8/26/2006 1:30:40 PM
Re: Random Interval kglad
8/26/2006 3:06:44 PM
AddThis Social Bookmark Button