Programming an event to apply to multiple objects

I want to group some objects (movie clips) in a way allowing me program an event that applies to all of the objects.

To be specific, I want to group some buttons in a video player. If the mouse hovers over any one of the buttons, that button’s animation will jump to frame 2. How can I do this?

(I figured there is probably an easier way than programming each individual button to jump to frame 2 on mouse over.)

Are you programming in AS2 or AS3 ?

AS2 apparently. …I’m a complete noob to actionscript.

One way of doing this considering that you have not assigned the buttons instance name sequentially (btn1, btn2 etc), then you can have an array and push all the instance name of the buttons inside that array. After that you can do something like this:


for(var i:Number=0;i<arr.length;i++){
arr[i].onRollOver=function(){
this.gotoAndPlay(2);
}
}

That’s pretty clever.
I’m new to AS, but it seems this would auto-generate a separate function for each object listed in the array, just as if I had written the individual functions myself.

Thank you.

By the way, should I instead really be using AS3 for a video player?
I will be using it primarily to play live streams of pre-recorded videos with Adobe FMS. (like an internet TV network)
Does AS3 provide video-player-related features that would be impossible with AS2? I really don’t know the difference…

jim_morrison3’s method is perfectly valid.

However here is an alternative version…

var arr:Array = [btn1, mc1];

function mcRollOver():Void {
	trace("rollover");
}
function mcRollOut():Void {
	trace("rollout");
}
function mcPress():Void {
	trace("press");
}

for(var i:Number = 0; i < arr.length; i++) {
	var obj = arr[i];
	obj.onRollOver = mcRollOver;
	obj.onRollOut = mcRollOut;
	obj.onPress = mcPress;
}

I personally find this structure easier to update.

I agree with Ziggwies approach even i was thinking about something similar but changed my mind while writing my post.

AS3 is the latest version of Actionscript and has its advantages but do keep in mind that it might take you more time in mastering it as it has a higher learning curve.

What does “:Void” do?

What does ":Void" do? 

That means the function will not return anything.

I’ve been messing around with this code for a while, but I still can’t get it working…

Here is what I have right row:


	//Valid Buttons
	var validControls:Array = [infoButton, downloadButton, muteVolButton, unmuteVolButton, volumeSlider, volumeBG, seeker, progressBar, fullscreenButton];

	//On roll over
	function buttonRollOver() {
		gotoAndStop(2);
	}
	
	//On roll out
	function buttonRollOut() {
		gotoAndStop(1);
	}

	//Generate functions
	for(var i:Number = 0; i < validControls.length; i++) {
		var obj = validControls[i];
		obj.onRollOver = buttonRollOver;
		obj.onRollOut = buttonRollOut;
	}

If the elements in the array are instance names of the objects and not variables then they need to enclosed as strings (“”), also you would need to pass their full path if they are contained in another movieclip instead of the main timeline.

The objects are not contained within another movieclip. I placed quotes around the instance names in the array, but the objects still do not respond to the functions. Here is my updated code:

	//Valid Buttons
	var validControls:Array = ["infoButton", "downloadButton", "muteVolButton", "unmuteVolButton", "volumeSlider", "volumeBG", "seeker", "progressBar", "fullscreenButton"];

	//On roll over
	function buttonRollOver() {
		this.gotoAndStop(2);
	}
	
	//On roll out
	function buttonRollOut() {
		this.gotoAndStop(1);
	}

	//Generate functions
	for(var i:Number = 0; i < validControls.length; i++) {
		var obj = validControls[i];
		obj.onRollOver = buttonRollOver();
		obj.onRollOut = buttonRollOut();
	}

I tried hard-coding some functions for the infoButton object, and it works just fine…

	//On roll over
	infoButton.onRollOver = function() {
		this.gotoAndStop(2);
	}
	
	//On roll out
	infoButton.onRollOut = function() {
		this.gotoAndStop(1);
	}