Return to Snippet

Revision: 19334
at October 21, 2009 16:11 by MacEvelly


Initial Code
// Assumes that the following buttons with instance names are on the stage
// shape1, shape2, shape3, shape4, shape5
// bg1, bg2, bg3, bg4, bg5
// font1, font2, font3, font4, font5
// color1, color2, color3, color4, color5
//
// This goes thought all the children and cleans out the generic "instance" named children and assigns the rest a click listener
for (var i:int = 0; i < numChildren; i++) {
	var tempName:String = getChildAt(i).name.replace(/\d/g,'');
	if (tempName != "instance") {
		getChildAt(i).addEventListener(MouseEvent.CLICK, myButton);
	}
}
// Once clicked, this function uses the variables in the name to assign actions
function myButton(evt:MouseEvent):void {
	
	var goToFrame:int = evt.currentTarget.name.replace(/\D/g,'');
	var tempName:String = evt.currentTarget.name.replace(/\d/g,'');
	
	switch (tempName) {
		case "shape" :
			trace(1,tempName,goToFrame);
			//shapeMC.gotoAndStop(goToFrame)
			break;
		case "bg" :
			trace(2,tempName,goToFrame);
			//bgMC.gotoAndStop(goToFrame)
			break;
		case "font" :
			trace(3,tempName,goToFrame);
			//fontMC.gotoAndStop(goToFrame)
			break;
		case "color" :
			trace(4,tempName,goToFrame);
			//colorMC.gotoAndStop(goToFrame)
			break;
	}
}

Initial URL

                                

Initial Description
This uses the button's instance name to assign functions.  Instead of using a bunch of event listeners, you can use one function to delegate actions according to the button's instance name.

Initial Title
It's all in the name - Instance Name Function

Initial Tags
button

Initial Language
ActionScript 3