# How to use function to generate buttons

**URL:** <https://forum.kirupa.com/t/how-to-use-function-to-generate-buttons/319466>\
**Category:** flash\
**Created:** [February 21, 2011, 8:09pm UTC](https://forum.kirupa.com/t/how-to-use-function-to-generate-buttons/319466 "2011-02-21T20:09:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jthomps123](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@jthomps123](https://forum.kirupa.com/u/jthomps123)\
**Post date:** [February 21, 2011, 8:09pm UTC](https://forum.kirupa.com/t/how-to-use-function-to-generate-buttons/319466/1 "2011-02-21T20:09:52Z")

</div>

Im very new to AS3 and trying to figure some things out. Im sure there is an easy way to accomplish what im doing.

I want to make generating the code for my movie clip buttons. This is what I have right now:

```auto
// Button 1 code
but1.buttonMode = true;
but1.useHandCursor = true;
but1.addEventListener(MouseEvent.ROLL_OVER, but1rollover);
but1.addEventListener(MouseEvent.ROLL_OUT, but1rollout);
but1.addEventListener(MouseEvent.CLICK, but1click);

function but1rollover (myevent:MouseEvent){
	but1.gotoAndPlay(2);
}
function but1rollout (myevent:MouseEvent){
	but1.gotoAndPlay('finish');
}
function but1click (myevent:MouseEvent){
	this.mystate="one";
	this.nextFrame();
}

// Button 2 code
but2.buttonMode = true;
but2.useHandCursor = true;
but2.addEventListener(MouseEvent.ROLL_OVER, but2rollover);
but2.addEventListener(MouseEvent.ROLL_OUT, but2rollout);
but2.addEventListener(MouseEvent.CLICK, but2click);

function but2rollover (myevent:MouseEvent){
	but2.gotoAndPlay(2);
}
function but2rollout (myevent:MouseEvent){
	but2.gotoAndPlay('finish');
}
function but2click (myevent:MouseEvent){
	this.mystate="two";
	this.nextFrame();
}

```

I’ve tried to create a function for this, it seems to work but I dont know how to create the mystate variable that is needed for a timeline action to know where to got when the button is clicked. This is the code I have generated but it needs help:

```auto
function createbut(var1) {
	var1.buttonMode = true;
	var1.useHandCursor = true;
	var1.addEventListener(MouseEvent.ROLL_OVER,
		function (myevent:MouseEvent){
		var1.gotoAndPlay(2);
		});
	var1.addEventListener(MouseEvent.ROLL_OUT,
		function (myevent:MouseEvent){
		var1.gotoAndPlay("finish");
		});
	var1.addEventListener(MouseEvent.CLICK,
		function (myevent:MouseEvent){
			var mystate=var1;
		nextFrame();
		});

}
createbut(but1);
createbut(but2);

```

Any help is much appreciated. Thanks,  
Jason
