# Dynamic enhancement of my Code

**URL:** <https://forum.kirupa.com/t/dynamic-enhancement-of-my-code/176398>\
**Category:** flash\
**Created:** [January 23, 2006, 9:00pm UTC](https://forum.kirupa.com/t/dynamic-enhancement-of-my-code/176398 "2006-01-23T21:00:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soig](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Soig](https://forum.kirupa.com/u/Soig)\
**Post date:** [January 23, 2006, 9:00pm UTC](https://forum.kirupa.com/t/dynamic-enhancement-of-my-code/176398/1 "2006-01-23T21:00:11Z")

</div>

This code is to turn an MC into a button with actionscript. But I have about 10 buttons and it seems ridiculous to use this code 10 times. Can I declare it as a function and pass it the names I want?

**[SIZE=4][COLOR=Red]Original Code[/COLOR][/SIZE]**

> \_root.mainButton.onRollOver = function() {  
> if(\_root.currentlyLoaded != “main”){  
> \_root.mainButton.gotoAndPlay(“over”); } }  
> \_root.mainButton.onRollOut = function() {  
> if(\_root.currentlyLoaded != “main”){  
> \_root.mainButton.gotoAndPlay(“getBack”); } }  
> \_root.mainButton.onRelease = function() {  
> if(\_root.currentlyLoaded != “main”){  
> \_root.mainButton.gotoAndPlay(“down”);  
> \_root.currentlyLoaded = “main”;  
> \_root.clearButton();  
> } }

I understand that the following code is not correct as far as syntax goes but that is what I want to achieve. Does anyone know how to do it?

**[SIZE=4][COLOR=Red]Wanted code  
[/COLOR][/SIZE]**

> 

function buttonCode() {  
\_root. **enterHere** Buttontton.onRollOver = function() {  
if(\_root.currentlyLoaded != “ **enterHere** ”){  
\_root. **enterHere** Buttontton.gotoAndPlay(“over”); } }  
\_root. **enterHere** Buttontton.onRollOut = function() {  
if(\_root.currentlyLoaded != “ **enterHere** ”){  
\_root. **enterHere** Buttontton.gotoAndPlay(“getBack”); } }  
\_root. **enterHere** Buttontton.onRelease = function() {  
if(\_root.currentlyLoaded != “ **enterHere** ”){  
\_root. **enterHere** Buttontton.gotoAndPlay(“down”);  
\_root.currentlyLoaded = “ **enterHere** ”;  
\_root.clearButton();  
} }  
}

buttonCode(enterHere = main);  
buttonCode(enterHere = bio);  
buttonCode(enterHere = contact);  
// etc…
