# Efficient Coding for Multiple Buttons

**URL:** https://forum.kirupa.com/t/efficient-coding-for-multiple-buttons/311705
**Category:** flash
**Created:** [July 14, 2010, 1:49pm UTC](https://forum.kirupa.com/t/efficient-coding-for-multiple-buttons/311705 "2010-07-14T13:49:39Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![timmy666](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@timmy666](https://forum.kirupa.com/u/timmy666)
#### Post date: [July 14, 2010, 1:49pm UTC](https://forum.kirupa.com/t/efficient-coding-for-multiple-buttons/311705/1 "2010-07-14T13:49:39Z")

</div>

Hey guys,

This is my first post (probably of many to come).

I am currently making a portfolio in AS3 for myself, and am coding the actionscript for the buttons (of which there are 5), and i was wondering what the most efficient way to do this is?

I could just make an event listener and function for each button, which I can do and works fine, it just doesn’t seem like the “smoothest” way to code it.

So instead I tried a switch statement (below), this works and seems a bit more efficient coding-wise, however I’ve a discovered a bug in that when i click on the same button once it works, if i click it a second time it goes to the next frame in the switch statement, and clicking it a third time brings it back to the correct frame. But clicking ‘home’ should always go to ‘home’ instead of ‘flash’, no matter how many times i click it…

> menuMC.homeBTN.addEventListener(MouseEvent.CLICK, changeContent);  
> menuMC.flashBTN.addEventListener(MouseEvent.CLICK, changeContent);  
> menuMC.videoBTN.addEventListener(MouseEvent.CLICK, changeContent);  
> menuMC.threedBTN.addEventListener(MouseEvent.CLICK, changeContent);  
> menuMC.aboutBTN.addEventListener(MouseEvent.CLICK, changeContent);  
> menuMC.contactBTN.addEventListener(MouseEvent.CLICK, changeContent);

function changeContent(evt:MouseEvent):void {  
var changeCon:String=evt.target.name;  
switch (changeCon) {  
case “homeBTN” :  
contentMC.gotoAndPlay(“home”);  
break;  
case “flashBTN” :  
contentMC.gotoAndPlay(“flash”);  
break;  
case “videoBTN” :  
contentMC.gotoAndPlay(“video”);  
break;  
case “threedBTN” :  
contentMC.gotoAndPlay(“3d”);  
break;  
case “aboutBTN” :  
contentMC.gotoAndPlay(“about”);  
break;  
case “contactBTN” :  
contentMC.gotoAndPlay(“contact”);  
break;

```
    default :
        trace("content selection failed");
        break;
}

```

}

So what I am asking is, how can this “bug” be avoided, and is there an even better way to code this?\<br\>
