# Passing parameters to event in a for loop

**URL:** <https://forum.kirupa.com/t/passing-parameters-to-event-in-a-for-loop/242292>\
**Category:** flash\
**Created:** [October 24, 2007, 1:46pm UTC](https://forum.kirupa.com/t/passing-parameters-to-event-in-a-for-loop/242292 "2007-10-24T13:46:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreasCS](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@AndreasCS](https://forum.kirupa.com/u/AndreasCS)\
**Post date:** [October 24, 2007, 1:46pm UTC](https://forum.kirupa.com/t/passing-parameters-to-event-in-a-for-loop/242292/1 "2007-10-24T13:46:22Z")

</div>

Hey all

I’m stuck at a problem when creating children in a for loop, then passing the i:int to a MouseEvent.CLICK event to each child. They all get the last i:int used in the loop.

So I made a custom event but passing that event the i:int doesn’t work either. I still get all the CLICKs to return the last “i” used. How do I fix this?

For loop:

```auto
for (var i:int=0; i < pics; i++) { // 10 pics for example
    plane = new Plane(cMaterials*,650,500,3,3);
    scene.addChild(plane);
                
    plane.container.addEventListener(MouseEvent.CLICK, onClick);
    function onClick(e:MouseEvent){
        trace(i) // all clicks return 9
        dispatchEvent( new CustomEvent(i, CustomEvent.CLICKED) ); // Tracing i from CustomEvent.CLICKED returns 9
    }
}

```

I’m guessing I have to make the first event on the container to be a custom one, but I don’t get how to make a custom event act as a click for example.

At the moment this is my custom event

```auto
package{
    import flash.events.*;

    public class CustomEvent extends Event {

        public static const CLICKED:String = "onClick";
        public var myProp:Object;

        public function CustomEvent(_myProp:Object=null, type:String=null, bubbles:Boolean=false, cancelable:Boolean=false) {
            super(type, bubbles, cancelable);
            myProp = _myProp;
            trace(myProp);
        }
        public override function clone ():Event
        {
            return new CustomEvent(myProp,type,bubbles,cancelable);
        }
    }
}

```

Any help appreciated!

Thanks

Edit: This same thread got a very nice answer so thought I’d link there if you need help with this aswell! : [http://www.actionscript.org/forums/showthread.php3?p=666096#post666096](http://www.actionscript.org/forums/showthread.php3?p=666096#post666096)
