Ok, I’m having a little trouble with the setInterval function. What I have right now is code that just turns a movie clip from the first frame to the second, and back to the first like turning a light on and off. Here’s the code:
//initiate variables
init();
//initate function
function init():Void{
//initiate light array
_root.lights = new Array();
lights[1] = 0;
lights[2] = 0;
lights[3] = 0;
lights[4] = 0;
lights[5] = 0;
///number for random ratio
_root.rnd = 20
//setup time ratio
_root.li = 1000
}
//randomly turn on lights _root event
_root.onEnterFrame = function(){
//turn on lights
_root.turnon(lt1);
//lt1.turnon();
//lt2.turnon();
//lt3.turnon();
//lt4.turnon();
//lt5.turnon();
}
//turn on function
_root.turnon = function(Light){
//get array variable from light variable
if (Light==lt1){
ArN=1
}
if (Light==lt2){
ArN=2
}
if (Light==lt3){
ArN=3
}
if (Light==lt4){
ArN=4
}
if (Light==lt5){
ArN=5
}
//check if light is off
if (lights[ArN] == 0){
Light.rnd = Math.ceil(Math.random() * _root.rnd);
trace(Light.rnd);
}
//check if light should turn on
if (Light.rnd == 1){
lights[ArN]=1;
Light.gotoAndStop(2);
//set timer so light will go off
Light = setInterval(function(){Light.gotoAndStop(1);lights[ArN]=0;//timer turn off
clearInterval(Light);}, _root.li );
}
}
Now my main problem is in this part:
//check if light should turn on
if (Light.rnd == 1){
lights[ArN]=1;
Light.gotoAndStop(2);
//set timer so light will go off
Light = setInterval(function(){Light.gotoAndStop(1);lights[ArN]=0;//timer turn off
clearInterval(Light);}, _root.li );
}
What I want to happen is to have the light turn on when a random value turns one, set an interval, and when the time runs out turn off the light, and do it all over again. Now the first time the random value turns 1, the light turns on and never goes off. Now I’ve diagnosed the problem and the reason is because I named the interval “Light”, the same name as the movie clip. However, if i give the interval any other name, the light turns and off fine the first time, but the second time, the light just flickers as if the setInterval function isn’t even working. What am I doing wrong?