Set interval question

lets say i have a bunch of buttons and all the code i need is in the onRelease functions and the exact effect i wanted would be clicking the buttons every 5 seconds.

Basically what i want to do is use those onRelease functions on the buttons, in the button order(button1, button2, etc.) every 5 seconds, without clicking though. Is there a way i can tell it to use the onRelease functions? How would i go about setting this up?

Everything works perfectly i just want it to “auto-click” or “auto-call the onRelease’s” every 5 seconds. It would also be nice to be able to click the buttons at any given time too. So say the timer is on button4 and i click button2 and the timer starts from there.

my mind is telling me i should somehow be able to have something like the code below with a simple array or something maybe? But Murphy is telling me it just won’t be this easy :slight_smile:

     myInterval = setInterval("button* onRelease function here", 5000);

Something like this should work:

for(var i:Number = 0; i < 5; i++){
	var f:Function = function():Void {
		button[arguments.callee.i].onRelease();
	}
	f.i = i;
	myIntervals* = setInterval(f, 5000);
}

Also, I anticipated some other common errors that might have occurred in your code somewhere, which is why I included the loop structure and renamed myInterval in the above code.

arguments.callee refers to the function within button* right?

makes sense. i can’t seem to get it working though. The buttons still work fine but they don’t switch with the time.

Hmm. Does this example make more sense?:

// make buttons
var buttons:Array = new Array();
for(var i:Number = 0; i < 5; i++){
	var b:MovieClip = createEmptyMovieClip('button' + i, i);
	buttons.push(b);
	b.clear();
	b.beginFill(0x4ba3fe);
	b.lineTo(75, 0);
	b.lineTo(75, 30);
	b.lineTo(0, 30);
	b.lineTo(0, 0);
	b.endFill();
	b._x = i * 100;
	b.index = i;
	b.onRelease = handler;
}

var interval:Number = setInterval(timer, 1000);
var curButtonIndex:Number = 0;

function timer():Void {
	buttons[curButtonIndex++ % buttons.length].onRelease();
}

function handler():Void {
	curButtonIndex = this.index + 1;
	// lazy alpha reset
	for(var i:Number = 0; i < 5; i++){
		buttons*._alpha = 100;
	}
	this._alpha = 50;
	clearInterval(interval);
	interval = setInterval(timer, 1000);
}

Running that should set up the buttons and handlers and everything.

ooo…that works nicely!

A few things though, is it possible to instead of createEmptyMovieClip use the existing buttons? meaning, i have different onRelease code in each btn. i tried goofing with it and can’t seem to make that happen

Yes, it’s possible. The modification looks like this:

var buttons:Array = [btn1, btn2, btn3];
for(var i:Number = 0; i < buttons.length; i++){
    var b:MovieClip = buttons*
    b.index = i;
    b.onRelease = handler;
}

thanks kril that works GREAT!

i am running into another issue however. Everything works perfect, but the setinterval starts at 5000 right, so the first buttons actions start at 5000. How would i modify this to make the first button start right away, and then every 5000 it does the actions with the above code. Other than that this is exactly what i was trying to do!

Modify the relevant lines to:

var interval:Number = 0;
var curButtonIndex:Number = 0;
handler.call(buttons[0]);