Even more brute force programming

Next, we'll add more event listeners for our objects. We'll add event listeners for MouseEvent.ROLL_OVER and MouseEvent.ROLL_OUT. Once again, carrying on with our brute force technique, we'll write separate functions for each object, and tailor each action inside each function to the specific object. Here's how the swf behaves now:

Here's the brutal code that makes all this magic happen:

import flash.events.MouseEvent;

square.buttonMode = true;
circle.buttonMode = true;
triangle.buttonMode = true;

square.addEventListener(MouseEvent.CLICK, square_onClick);
circle.addEventListener(MouseEvent.CLICK, circle_onClick);
triangle.addEventListener(MouseEvent.CLICK, triangle_onClick);

function square_onClick(event:MouseEvent):void {
	myText.text = "square was clicked";
}
function circle_onClick(event:MouseEvent):void {
	myText.text = "circle was clicked";
}
function triangle_onClick(event:MouseEvent):void {
	myText.text = "triangle was clicked";
}

square.addEventListener(MouseEvent.ROLL_OVER, square_onOver);
circle.addEventListener(MouseEvent.ROLL_OVER, circle_onOver);
triangle.addEventListener(MouseEvent.ROLL_OVER, triangle_onOver);

function square_onOver(event:MouseEvent):void {
	square.alpha = 0.5;
}
function circle_onOver(event:MouseEvent):void {
	circle.alpha = 0.5;
}
function triangle_onOver(event:MouseEvent):void {
	triangle.alpha = 0.5;
}

square.addEventListener(MouseEvent.ROLL_OUT, square_onOut);
circle.addEventListener(MouseEvent.ROLL_OUT, circle_onOut);
triangle.addEventListener(MouseEvent.ROLL_OUT, triangle_onOut);

function square_onOut(event:MouseEvent):void {
	square.alpha = 1;
}
function circle_onOut(event:MouseEvent):void {
	circle.alpha = 1;
}
function triangle_onOut(event:MouseEvent):void {
	triangle.alpha = 1;
}

It's very common to add event listeners to objects for OVER, OUT, and CLICK, you'll find yourself doing it all the time. Adding more objects to the party means writing those same three listeners again for each one you add (and the addEventListener line as well)! Unless one is a huge fan of cut, copy and paste (and edit!), at this point, anyone is bound to be crying out, "There must be a better way! All those functions are doing exactly the same thing, the only thing that varies is the object that they act upon!" Next, we'll see the first streamlining technique, combining listeners.