# Using a loop to Create Buttons that play MC

**URL:** https://forum.kirupa.com/t/using-a-loop-to-create-buttons-that-play-mc/296992
**Category:** Uncategorized
**Created:** [September 23, 2009, 9:02pm UTC](https://forum.kirupa.com/t/using-a-loop-to-create-buttons-that-play-mc/296992 "2009-09-23T21:02:44Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![KiKaL](https://avatars.discourse-cdn.com/v4/letter/k/edb3f5/32.png) [@KiKaL](https://forum.kirupa.com/u/KiKaL)
#### Post date: [September 23, 2009, 9:02pm UTC](https://forum.kirupa.com/t/using-a-loop-to-create-buttons-that-play-mc/296992/1 "2009-09-23T21:02:44Z")

</div>

I am trying to basically have a loop that is based on a string length. It adds a movie clip for every letter and a hidden rollover button. All this seems to be working fine but I am having trouble wrapping my head around how to code the event listeners or it might be how I’m telling it which movie to play. As of now all the buttons play the last mc which is block6. What do I need to do differently?

```php
import flash.display.*;
import flash.events.MouseEvent;

var input:String;
var blockOut:String;
var strLength:int;
var pos:int = 0;

strLength = textBox.length;
var blockArray:Array = new Array();
var buttonArray:Array = new Array();

for(var i:int =1; i <= strLength; i++){
    //trace(i);
    var block:blockMove_mc = new blockMove_mc();
    block.id = "block"+i
    blockArray* = block;
    
    
    this.addChild(blockArray*);
    blockArray*.x = pos;
    blockArray*.y = 108;
    trace(blockArray*)
        
    var button:button_Mc = new button_Mc();
    button.id = "button"+i
    buttonArray* = button;
    blockArray*.addChild(button);
    //button2.z = 2000;
    //button2.y = -500;
    
    var blockOut:String = "blockOut" + i;
        
    buttonArray*.addEventListener(MouseEvent.MOUSE_OVER, blockOut);
    buttonArray*.addEventListener(MouseEvent.MOUSE_OUT, blockOt);
    
    function blockOut(event:MouseEvent):void {
        block.gotoAndPlay("turn");
        trace(block.id);
    
    }

function blockOt(event:MouseEvent):void {

        block.gotoAndPlay("return");    
        

    }
    
    

    pos = pos + 150;
}

```
