# Using a loop to create mc's and assign it a function

**URL:** <https://forum.kirupa.com/t/using-a-loop-to-create-mcs-and-assign-it-a-function/214543>\
**Category:** flash\
**Created:** [January 29, 2007, 11:45pm UTC](https://forum.kirupa.com/t/using-a-loop-to-create-mcs-and-assign-it-a-function/214543 "2007-01-29T23:45:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danjah](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@danjah](https://forum.kirupa.com/u/danjah)\
**Post date:** [January 29, 2007, 11:45pm UTC](https://forum.kirupa.com/t/using-a-loop-to-create-mcs-and-assign-it-a-function/214543/1 "2007-01-29T23:45:56Z")

</div>

Hi everyone/anyone

Even typing out the subject makes me think I have something intrinsically wrong…

I’m trying to create dragging functionality which starts onMouseDown and ceases onMouseUp, it’s part of a .fla that will be doing a number of other things so I won’t paste the whole lot as it will probably make what I need right now more convoluted.

So, this code…

[FONT=Courier New]parentPath = this;  
//x&y placement for number of items  
itemPos = [[10, 10], [40, 40], [70, 70], [110, 110], [50, 50], [60, 60], [70, 70], [80, 80], [90, 90], [100, 100], [110, 110], [120, 120], [130, 130], [140, 140], [150, 150], [160, 160], [170, 170], [180, 180]];

createItems = function () {  
for (i=0; i\<itemPos.length; i++) {  
parentPath.createEmptyMovieClip(“item\_”+i, parentPath.getNextHighestDepth());  
parentPath[“item\_”+i].attachMovie(“item”, “item”, this.getNextHighestDepth());  
parentPath[“item\_”+i]._x = itemPos\*[0];  
parentPath["item_"+i]._y = itemPos\*[1];  
//  
parentPath["item_"+i].onMouseDown = function() {  
trace("mouseDown, this = “+this);  
this.onEnterFrame = function() {  
this.\_x = parentPath.\_xmouse;  
this.\_y = parentPath._ymouse;  
};  
};  
parentPath["item_”+i].onMouseUp = function() {  
trace("mouseUp, this = "+this);  
this.onEnterFrame = null;  
};  
}  
}  
createItems();

[FONT=Verdana]…is what I have, when I run this (and have ‘item’ with ‘linkage’ set in the library) I get 18 items, with x,y co-ords as per the array, on the Stage. Then when i press down on any one item they all stack up under my mouse so that it appears i only have 1 item. All of the items move with my cursor and stop onMouseUp.

I figured that as I have [/FONT][/FONT][FONT=Courier New]parentPath[“item\_”+i].onMouseDown = function(){}[/FONT][FONT=Courier New][FONT=Verdana] I would have 18 different items with 18 seperate mouseDown functions, but they all appear to depend on one another.

So how can I have all item\_x’s have this functionality but not copy one another?

The only solution I can think of it to actually duplicate the onMouseUp and onMouseDown functions for _every single item\_x_!!! 🅰 This will work, but it cannot possibly be the most efficient way of executing this code…

Any help is appreciated and I swear I’ll buy you a drink if I ever meet you.  
[/FONT][/FONT]
